简体   繁体   中英

Simpe if and else in C programming

I want to make this program say that if you enter 1 the screen will display "yes G is the 1st note in a G chord", if you enter anything else it will print "wrong!" and then loop back into the beginning, here is my attempt at it. Also, I know there are different ways to do this, but it is for a school assignment and the enum data-type is necessary, though this code is far reaching for just displaying use of enum, it bothers me I can't get this to work. Any pointers? (no pun intended).

enum Gchord{G=1,B,D,};
int main(){
    printf( "What interval is G in the G chord triad \nEnter 1 2 or 3\n" );  
    int note;
    scanf("%i",&note);    

    if (note = 1 ){                 
        printf ("Yes G is %ist note in the G-chord\n",G )};
    else(
        printf("no, wrong");     
    return(0):       
};

note = 1 is assigning note with the value 1 . You are looking to compare note with 1 and therefore you need the operator == . Read up on comparison operations here:

http://en.cppreference.com/w/cpp/language/operator_comparison

To be crystal clear:

note = 1;  // Assigning note to 1, note is now the value 1
note == 1; // Comparing note to 1, true if note is 1, false otherwise. 

You also have plenty of other problems:

  • In printf ("Yes G is %ist note in the G-chord\\n",G )}; Lines end with semicolons, if statements don't.
  • else( else doesnt take an argument and should use a curly brace. else {
  • return(0) Return is not a function.

Your compiler with warnings on full ( -Wall ) will tell you all of these things. Things in the list above should have been compiler errors.

There a lot of problems in your code, but the main one is because you try to assign 1 to note instead of the comparission == .

Another thing is that you never check scanf for errors.

There are parentheses and brackets used wrong.

int main(){} shoudl be at least int main(void){} .

The return statement should be not treated as a function, there are no need of those parentheses around (0) and should end with a semicol ; and not with : .

Now the following should explain you better what you probably tried to do:

#include<stdio.h>

enum Gchord{G=1,B,D,};

int main(void){
    printf( "What interval is G in the G chord triad \nEnter 1 2 or 3\n" );
    int note;

    if(scanf("%i",&note) == 1){
        if (note == 1 ){
            printf ("Yes G is %ist note in the G-chord\n",G );
        }else{
            printf("no, wrong");
        }
    }else{
        printf("Error, scanf\n");
    }
    return 0;
}
I don't know where to start. Your code has a lot of errors.
  • Code formatting: it is very important to learn how to format your code so it becomes easier to read it.
  • int note; variables should almost always be declared at the top and also initialized (in this case with int note = 0;
  • If you separate something with , enter a space behind it. not scanf("%i",&note); but scanf("%i", &note);
  • To compare if 2 values are equal, use == . A single = is used to assign values to a variable. Wrong: if (note = 1 ) Right: if (note == 1)
  • You are using a wrong bracket for the else that you do not even close.
  • And for your problem of looping, you should read up about while loops and ask again if you don't understand them.

     enum Gchord{G=1,B,D,}; int main() { int note = 0; printf("What interval is G in the G chord triad \\nEnter 1 2 or 3\\n"); scanf("%i", &note); if (note == 1) { printf ("Yes G is %ist note in the G-chord\\n", G); } else { printf("no, wrong"); } return 0; }; 

There are a myriad of syntax errors in here including the one everyone has pointed out that note = 1 assigns a value of 1 to the note variable instead of testing for equality. You want the == operator here.

Also you aren't really using the enum, your teacher probably wont pass you on this.

I modified your code a bit to make a little more use of the enum and to do the loop you are looking for.

enum Gchord { EXIT, G, B, D, };
int main()
{
    while (true)
    {
        printf("What interval is G in the G chord triad \nEnter 1, 2, 3, or 0 to exit\n");
        Gchord note;
        scanf("%i", &note);

        if( note == EXIT )
            break;

        if (note == G)
            printf("Yes G is %ist note in the G-chord\n", G);
        else
            printf("no, wrong\n");
    }

    return(0);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM