Thread: Help with easy beginner program

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    26

    Help with easy beginner program

    Hi, I just made a dumb program to test my skills. My options aren't registering correctly, can anyone spot a mistake in my code? Thank you!

    Code:
    #include <stdio.h>
    
    int main()
    {
        int x;
        printf("This is my first real program. Please select an option below: \n");
        printf("1. Enter age\n");
        printf("2. Enter heigt\n");
        printf("3. Enter birth year\n");
        printf("4. Enter salary\n");
        scanf("&d",&x);
        if(x == 1)
        {
            printf("Please enter your age: \n");
        }
        else if(x == 2)
        {
            printf("Please enter your height: \n");
        }
        else if(x == 3)
        {
            printf("Please enter your birth year: \n");
        }
        else if(x == 4)
        {
            printf("Please enter your salary: \n");
        }
        else printf("Invalid option! Try again. \n");
        return(0);
    }

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    26
    Also, why is there so many lines of space beneath my code I posted?!

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What exactly is the problem you are seeing?

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    26
    Quote Originally Posted by Matticus View Post
    What exactly is the problem you are seeing?
    I didn't point that out, I'm sorry. It goes straight to "Invalid option! Try again."

    If I type "1" I want it to printf the first statement and for the next three options as well...

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    scanf("&d",&x);
    I think you mean to use "%d" in lieu of "&d"

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    26
    Quote Originally Posted by Matticus View Post
    Code:
    scanf("&d",&x);
    I think you mean to use "%d" in lieu of "&d"
    Oh! Of course, thank you! :3

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Simple troubleshooting technique - put a simple "printf()" statement after your "scanf()" statement to see what the variable holds, so you can help isolate where the problem is occurring.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, why is there so many lines of space beneath my code I posted?!
    I get that problem with Google Chrome - I just hit the "to plain text" button.
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    26
    Ah okay, thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy beginner's question about pointers
    By kyldn6 in forum C Programming
    Replies: 4
    Last Post: 10-26-2010, 05:50 PM
  2. Help me ! easy c program
    By DINORS in forum C Programming
    Replies: 21
    Last Post: 06-27-2008, 10:59 PM
  3. Help with very easy program
    By plshelpme in forum C Programming
    Replies: 9
    Last Post: 11-02-2005, 12:11 PM
  4. Problems with easy program
    By chuckster in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2004, 11:08 AM
  5. Easy Beginner Question
    By Prog.Patterson in forum Windows Programming
    Replies: 2
    Last Post: 05-16-2002, 12:52 AM