If statement problem

+1 Matthew Wigmore · December 31, 2014
So I've just started learning C from Bucky's tutorials and I was trying to write a basic program to practice embedded if statements (basically a slightly changed Bucky tutorial for the initial learning then I'll write my own program once I understand it) and it goes through the input process fine but always prints out "Your guess was too low" no matter what number I input. I'm sure there's a really simple explanation and I would be most grateful is someone could point it out! Many thanks! Code below.



#include <stdio.h>
#include <stdlib.h>

int main()
{
    char gender[2];
    int actual = 42;
    int guess;

    printf("What is your gender (m/f)?\n");
    scanf(" %c", gender);

    printf("Guess an integer between 0 and 100 inclusive\n");
    scanf(" &d", &guess);

    if (guess == actual)
    {
        printf("Well done you guessed correctly ");

        if (gender == 'm')
        {
            printf("bro");
        }
        if (gender == 'f')
        {
            printf("brodette");
        }
    }

    if (guess < actual)
    {
        printf("Your guess was too low\n");
    }

    if (guess > actual)
    {
        printf("Your guess was too high\n");
    }

    return 0;
}




Post a Reply

Replies

Oldest  Newest  Rating
0 Pere Garau Burguera · December 31, 2014
It's been some years since I left C, but I think it's %d instead of &d when you do scanf.
0 Bagel Cat · January 1, 2015
*FIXED(I think...)*
- I added some "else if" statements
- in the second "scanf" statement, you wrote "&d". it should be "%d"
- removed the [2] in the "gender" variable because it was causing some problems later in the program (which I don't want to type out because, lazyness...)
- Added other little things such as, adding the "bro" or "brodette" if the user's guess was "too high" or "too low"
- made the code a bit more compact

If there any problems please let me know and I will see what I can do...

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char gender;
    int actual = 42;
    int guess;

    printf("What is your gender (m/f)?\n");
    scanf(" %c", &gender);

printf("\n"); //this is just aesthetically pleasing in programm

    printf("Guess an integer between 0 and 100 inclusive\n");
    scanf(" %d", &guess);

printf("\n"); //this is just aesthetically pleasing in programm

    if (guess == actual)
      {
       printf("Well done you guessed correctly ");

      if (gender == 'm'){
          printf("bro");
        }else if (gender == 'f'){
          printf("brodette");
        }
      }

    if (guess < actual){
      printf("Your guess was too low ");

     if (gender == 'm'){
          printf("bro");
        }else if (gender == 'f'){
          printf("brodette");
        }

    }else if (guess > actual){
      printf("Your guess was too high ");

     if (gender == 'm'){
          printf("bro");
        }else if (gender == 'f'){
          printf("brodette");
        }
    }

    return 0;
0 Matthew Wigmore · January 1, 2015
Oh of course, how did I miss that *hits head on table*. Thank you very much!!
0 Bagel Cat · January 1, 2015
no problem
  • 1

C

107,149 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator