Thread: Can Someone Tell Me What's Wrong With This?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Smile Can Someone Tell Me What's Wrong With This?

    Hey, I'm trying to run this code:

    Code:
    #include <stdlib.h>
    
    struct database {
      int id_number;
      int age;
      int salary;
    };
    int main()
    {
        struct database john;  
      john.age = 19;
      john.id_number = 33033;
      john.salary = 80000;
      
       struct database haley;  
      haley.age = 18;
      haley.id_number = 34921;
      haley.salary = 21000;
      
       int input;
        
        printf( "Please Enter The I.D. Number Of Employee To View Info: \n" );
        scanf( "&d", &input );
        
        switch ( input )
        {   
        case 33033:       
    printf( " \n" );
    printf( "Employee's name is John. \n");
    printf( "John's age is: %d \n", john.age );
    printf( "John's ID is is: %d \n", john.id_number );
    printf( "John's salary is: %d \n", john.salary );
    break;
     
     case 34921:
    printf( " \n" );
    printf( "Employee's name is haley. \n");
    printf( "haley's age is: %d \n", haley.age );
    printf( "haley's ID is is: %d \n", haley.id_number );
    printf( "haley's salary is: %d \n", haley.salary );
    break;
    }
    getch();
    }

    When I run it it opens the "Please enter ID" part just fine, but when I enter in the I.D nothing happens. I can't seem to find what is wrong with the code, any help would be great.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What input did you enter? What's the expected output, actual output?

    EDIT:
    scanf( "&d", &input );
    huh, read the doc. You want %d as format string.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    1) You should include <stdio.h>

    2) also it is getchar(); not getch();

    3) you need %d not &d as Bayint Naung said.

    ! Your code will "play" correctly only if you type 33033 or 34921.
    Last edited by brack; 09-07-2010 at 07:08 AM.

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    There is too such a function as getch()

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome, Oral B.

    Must be on Turbo C, since I see getch(), and no conio.h file being included. Sounds like TC's smart compiler/linker at work!

    As a practical matter, I believe you want to make an array of these structs, so you can work with them easier, yes?

    Why don't you make an array that will hold 5 - 10 of the structs, (records), and then we can set it up so that the program can read in the student data, from a file.

    How's that sound?

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by Syscal View Post
    There is too such a function as getch()
    Probably it depends from the compiler...i mean mine has an error...

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Syscal View Post
    There is too such a function as getch()
    Not in the C or C++ standards, there's not.

    Some compilers or their libraries support such a function, but not all do.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Ah your right I had a "&d" instead of "%d", I changed that and it runs great now! And thanks Adak, I would of made an array but that it is actually the next lesson in the tutorial I'm following, I just wanted to make sure I fully understand Switch Cases and Structures.

    Thank you all so much for the help! By the way, I'm not sure why I have to use getch(), every time I try getchar(), it will close after I hit enter. Getch() lets me hit enter until the program is done running for some reason. I'm using Bloodshed's Compiler, I think that command might differ with compilers I'm not too sure.
    Last edited by Oral B; 09-07-2010 at 11:18 AM.

  9. #9
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You need to include stdio.h and conio.h. Getch is a non-portable function, so I suggest you use getchar.

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    But when I use getchar, the program closes after I hit enter once. When I use getch, the program will let me hit enter how ever many times I need to and it runs until its finished. Is there a way around that without switching compilers?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM