Thread: Help with "if" and "else"

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    21

    Help with "if" and "else"

    Hey all, I'm having a bit of trouble with if and else. I am supposed to make this program that asks the user to enter an even number. If the number is even, it is supposed to reply, "Thanks!" If the number is odd, it is supposed to ask for an even number again. If the user enters an even number this time, the reply is once again, "Thanks!" If it is an odd number again, it is supposed to reply, "You dummy!" I am supposed to ask for 3 separated numbers not including the re prompts because the user entered in an odd number. Here's what I got:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    //Prototype
    void getEven(int number);
    
    
    
    
    int main()
    {
        int number;
        getEven(number);
        getEven(number);
        getEven(number);
    
    
        return 0;
    }
    
    
    void getEven(int number)
    
    
    {
        printf("Enter an even number => ");
        scanf("%d", number);
    
    
    if ( number%2 == 0 ){
          printf("Thanks!\n");
    }
       else{
          printf("Enter an even number => ");
          scanf ("%d", number);
           }
    
    
    if ( number%2 == 0 ){
          printf ("Thanks!\n");
    }
        else{
          printf("You dummy!");
        }
    }
    As soon as I enter in the number, the program would freeze. I thought I had it right, but I'm a bit lost on what I can do. Any suggestions? Thank you guys!

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Code:
    int number;
    scanf("%d", &number); // note "address of" operator
    scanf wants a place to store the value it reads, so you pass the address of your variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-12-2012, 06:17 AM
  2. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM