Thread: value of char variable with loops

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    value of char variable with loops

    the first time around, the program works as expected. the integer x is set to 0 when a response is given other than 'y,Y,n,or N' so that the function will repeat. the second time around, even if 'y,Y,n,or N' is given as the reponse, the program seems to think that answer contains something else...perhaps the previous input? do i need to initialize answer each time around? thanks for the help if u can..extremely new here

    Code:
    void playgame()
    {
    char answer;
    int x = 0;
         while ( x == 0){
         printf( "Maybe we can play a guessing game instead? Y or  N\n");
         getchar();
         scanf( "%c", &answer );
                    if ( (answer == 121) || (answer == 89) )
                       {
                       printf( "Ok, but you will surely give up before you win!\n" );
                       x = 1;
                       }
                    else if ( (answer == 110) || (answer == 78) )
                       {
                       printf( "Well then, go find someone else with whom to play.\n" );
                       x = 1;
                       }
                    else
                       {
                       printf( "That was an invalid answer.  Please concentrate and try again.\n" );
                       x = 0;
                       getchar();
                       }
                        }
    
    }

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You're missing braces after your first else statement. Try putting those in and see if something new happens.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    (answer == 78)
    ->
    Code:
    (answer == 'N')
    is clearer.

    (tolower() in <ctype.h> will convert a letter to lowercase.)

    the program seems to think that answer contains something else...perhaps the previous input?
    Code:
         getchar();
         scanf( "%c", &answer );
    
        /* ... */
    
                    else
                       {
                       printf( "That was an invalid answer.  Please concentrate and try again.\n" );
                       x = 0;
                       getchar();
                       }
    You don't want two getchar()s. Get rid of one.

    You might want to use this code instead of getchar:
    Code:
    int c;
    while((c = getchar()) != '\n' && c != EOF) ;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're missing braces after your first else statement.
    No, the braces are fine, they're just not lined up.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Oups. He's right. Listen to what he says.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    k, i thought the braces were straight. thanks for the info guys, ill need some time to read abit about the other strategies and functions u mentions...thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM