begginner and I need help, switch statement

This is a discussion on begginner and I need help, switch statement within the C Programming forums, part of the General Programming Boards category; this is my first post and I have looked around on the board and havn't really found anything to help ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    Question begginner and I need help, switch statement

    this is my first post and I have looked around on the board and havn't really found anything to help me
    I understand the very basics of c and I have this program but when it goes into the switch statement under case 1 it jumps past a line to take the user input and I can't figure it out

    Code:
    menu();       -   -   -       /* calls menu function, displays menu */
        scanf("%d", &opt);  -  -  -        /* scans for input choice */
        while (opt != 3)    -  -  -        
           { switch(opt)     -       /* sets up the switch for options */
               { case 1:          -      /* option 1 if customer deposits */
                    printf("Now Please Enter Your User ID: ");
                    gets(cid);  <----------
                    i = search(cid);
                    if(i == -1)
                       puts("\nWe don't show that ID, please try again\n");
                    puts("\nPlease enter amount you want to deposit\n"); 
                    scanf("%f",&amount);
                    c[i].balance = c[i].balance + amount;
                    printf("\n Your new account balance is %f", c[i].balance);
                    break;
                case 2:
    .
    .
    .
    int search(char cid[]) 
       { int i;
        for(i = 0; i < 5; i++)
          if(strcmp(c[i].id, cid) == 0)
                    return i;}
    I have a function search but it jumps past the gets line and goes right to please enter amount to deposit. I need it to be able to retrieve a certain user ID and it jumps over that gets statement

    Please help
    Last edited by trkpony; 04-24-2003 at 01:49 AM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    That is due to you made a call to
    Code:
    scanf("%d", &opt);
    When you key in a value and hit Enter, the value is taken into opt but scanf leave a '\n' character in the stdin, and when gets comes along the '\n' character in the stream makes gets thinks that you've hit Enter, that's why you've not been able to enter a value and it seems 'skips' away while it really does passed by.

    A fix to the problem is to consume the '\n' character after the scanf.
    Code:
    char  garbages[255] = {0}; <-- Declare a array to collect garbage
    
    scanf("%d", &opt);               /* scans for input choice */
    gets(garbages);            <---- Make a call to gets here to eat away the '\n' character
    
    ...
    Merc

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    that worked but another problem

    thanks, I went with the first approach, but now I have a new error

    Code:
    while (opt != 3)      /* while option is not 3 it goes to a switch */
           switch (opt)        /* sets up the switch for options */
              case 1:           /* option 1 if customer deposits */
               puts("Now Please Enter Your User ID: ");
               gets(cid);
               i = search(cid);
               puts("\nPlease enter amount you want to deposit\n"); 
               scanf("%f",&amount);
               c[i].balance = c[i].balance + amount;
               printf("\n Your new account balance is %.2f\n", c[i].balance);
               break;
              
           case 2:
                  puts("blah");
                  break;
    the compiler now says that the break in case 1 is not part of a case and that case 2 isn't in the switch statement
    what is terminating the switch ?

    thanks again

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    Thumbs up got it

    thanks alot, you guys have been a real help

    If I have more problems I will try to read the FAQ more slowly

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21