Having trouble with an if statement!

This is a discussion on Having trouble with an if statement! within the C Programming forums, part of the General Programming Boards category; This program is part of a large one and accessed via a switch statement. For some reason when I type ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Unhappy Having trouble with an if statement!

    This program is part of a large one and accessed via a switch statement. For some reason when I type in a Y or N at the prompt when running it, the program skips to the last else option and replies a 'not a valid option' when it should not. I have messed with it for ages and am not getting anywhere.



    case 'I':;
    {
    char Y[]="Y";
    char N[]="N";
    char choice1 = 'Z';
    k = 5 - j;


    printf("\nThere are currently %i spaces in the data base", k);
    if(j<5)
    {
    printf("\n\nEnter the Student's first name:\t\t ");
    scanf("%s", studalltemp[j].fNametemp);

    printf("\ndatabase. Please conform that these details are correct (Y/N)");
    choice1 = getchar();
    getchar( );
    k = 0;

    if (choice1 == 'Y')
    {
    printf("\n1 student record saved to file\n");
    strcpy(studall[j].fName, studalltemp[j].fNametemp);
    j++;
    }
    else
    {
    if (choice1 == 'N')
    printf("Here");
    else
    printf("Not a valid option");
    }
    }
    }
    break;

  2. #2
    Unregistered
    Guest
    Maybe you've already tried this, but put in some debugging code that prints out the value of choice1 at a couple of points... could be your getchar() is picking up some garbage...?

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Okay, let's pretend that youjust finished typing in the last name... Your program suspends execution and waits for you to type something in...
    Code:
    Guybrush<ENTER>
    When you press enter, your program resumes execution. It then reads in the word Guybrush into fNametemp. It does not read in the '\n' from you pressing enter, so you still have a '\n' in the buffer.

    Of your first call to getchar(), which happens to be
    Code:
    choice1 = getchar();
    The getchar() will get the '\n' that is still in the buffer. A side effect of this is that since your program doesn't have to refill the buffer, it won't prompt the user for input. The real problem, however, is that you have given choice1 the value of '\n', when you want a Y or N.

    On the line after that...
    Code:
    getchar( );
    Since you've read in the last character of the buffer, the buffer is empty, so it will wait for user input again. This next character will presumably be the 'Y' or 'N', but this isn't assined to anything, so the value is lost.

    A loop can take care of this... replace
    Code:
    choice1 = getchar();
    getchar( );
    with
    Code:
    for (; isspace (choice1 = getchar()); );
    After this line, choice1 will be the next non-whitespace character typed in.
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Nosepicker DrakkenKorin's Avatar
    Join Date
    Nov 2001
    Posts
    407
    case 'I':;
    .
    .
    .
    Question: why is there a ; after your case 'I':?

    I always thought a switch was as such:
    Code:
    switch(something)
    {
       case 'a':
       {
          blah blah;
          blah bleh;
          break;
       }
       case'b':
          break;
    }
    ::Just had my Sociolinguistic Anthropology final this afternoon and am trying not to think too much Goddess love those minors::
    DrakkenKorin

    Get off my Intarweb!!!!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Question: why is there a ; after your case 'I':?

    While it serves no purpose, the ; at the end does no harm.
    You can, in C, in most places, have ; by itself and it will have no effect. a ';' just denotes the end of a statement. This is valid code:

    int main ( void ) {;;;;;;;;;;;;;;;;;;;;;;;;;;; return 0;}

    A ';' by itself is simply considered a 'null statement', meaining it has no effect. It _DOES_ have an (usually unwanted) effect in places like:

    if( a < b );
    {
    printf("This will _always_ display!");
    }

    In places like:

    for(x=0;x<10;x++);
    {
    printf("This only displays once!");
    }

    But in this case:

    switch(x)
    case 't': ;
    {
    printf("this block still executes, the ; has no adverse effect");
    }

    We're ok. Anyway...

    Code:
    case 'I':
    { 
        /**
        *** What is the purpose of these next two?
        **/
        char Y[]="Y"; 
        char N[]="N"; 
    
        char choice1 = 'Z'; 
        k = 5 - j; 
    
    
        printf("\nThere are currently %i spaces in the data base", k); 
        if(j<5) 
        { 
            printf("\n\nEnter the Student's first name:\t\t "); 
            scanf("%s", studalltemp[j].fNametemp); 
    
            printf("\ndatabase. Please conform that these details are correct (Y/N)"); 
            choice1 = getchar(); 
            getchar( ); 
    
            k = 0; 
    
            /**
            *** You may want: if( upcase(choice1) == 'Y' )
            **/
            if (choice1 == 'Y')
            { 
                    printf("\n1 student record saved to file\n"); 
                    strcpy(studall[j].fName, studalltemp[j].fNametemp); 
                    j++; 
            } 
            else 
            /**
            *** You may want: if( upcase(choice1) == 'N' )
            **/
            if (choice1 == 'N') 
            {
                    printf("Here"); 
            }
            else 
            {
                    printf("%c is not a valid option.", choice1 ); 
            }
    } 
    break;
    Try that on for size.

    Quzah.
    Last edited by quzah; 12-14-2001 at 12:33 AM.
    Hope is the first step on the road to disappointment.

  6. #6
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859
    Try getche() instead of getchar()
    Yoshi

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Thanks

    Cheers for all the help lads, all working..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 06:19 AM
  2. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 09:13 AM
  3. If statement being ignored?
    By FincH in forum C Programming
    Replies: 3
    Last Post: 04-18-2007, 02:51 PM
  4. having trouble understanding this statement
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2003, 10:00 AM
  5. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 10:56 PM

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