#1
  1. Happy Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    UK (University of Kentucky)
    Posts
    1,809
    Rep Power
    48

    On execution, it skips first question...


    I am writing a program to help me study for my German class. It pulls the English word from a text file, then tabs over to get the German word. It displays the English word and asks the user to enter the German word. It compares what the user entered in with the German word.

    If they match, then it displays the next word, if not, then it says, "THAT IS INCORRECT" and makes the user type in the word correctly.

    The thing is, if I use

    cin>>user;

    It works nicely, but if there are words that require a space, like "der Amerikaner," cin by itself will not capture the whole line, so I changed it to:

    cin.getline(user, 50);

    But now, when you load up the program, it displays the first word, but then displays the "THAT IS INCORRECT" message before the user has a chance to enter it in. But, after that, everything works fine. Here is a snippet of my code:

    Code:
      while(!OpenFile.eof())
      {
        OpenFile.getline(ch,50,'\t');
    
        if ( !strcmp(ch, "") )
        {
          exit(1);
        }
    
        cout<<"| "<<ch<<": ";
        OpenFile.getline(chin,50,'\n');
    
        cin.getline(user, 50);
    
        if ( !strcmp(user, "quit") )
        {
          exit(1);
        }
    
        if( !strcmp(user, chin) )
        {
          cout<<"| Ja, Voll! Das ist richtig!               |\n";
        }
        else
        {
          cout<<"| NINE, NINE!   "<<chin<<"\n";
          ask_again(ch, chin);
        }
        cout<<"+------------------------------------------+\n";
      }
      OpenFile.close();
    It's just annoying to get the first answer right away every time I load it up. Any ideas? :)
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    I would have to run a full program to test it, the code looks fine by my eye.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Happy Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    UK (University of Kentucky)
    Posts
    1,809
    Rep Power
    48
    I didn't want to post the whole thing, but it's not too big, here it is:
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <cstdlib.h>
    #include <fstream.h>
    
    int ask_again(char question[50], char answer[50]);
    
    int main()
    {
      system("clear");  
    
      static char ch[50];
      static char chin[50];
      char user[50];
      int select_vocab;
      char vocab_file[50];
    
      // Set the default file name.
      strcpy(vocab_file, "german_vocab_char.txt");
    
      cout<<"+------------------------------------------+\n";
      cout<<"+---------- | Ich Lerne Deustch |----------+\n";
      cout<<"+------------ | By Sam Powers |------------+\n";
      cout<<"+------------------------------------------+\n";
    
      cout<<"| PLEASE SELECT A VOCAB FILE:              |\n";
      cout<<"|                                          |\n";
      cout<<"| 1) Characteristics                       |\n";
      cout<<"| 2) Nouns                                 |\n";
      cout<<"| 3) Verbs                                 |\n";
      cout<<"| 4) Personal/Interrogative Pronouns       |\n";
      cout<<"| 5) Other                                 |\n";
      cout<<"|                                          |\n";
      cout<<"| ()> ";
     
      cin>>select_vocab;
     
      cout<<"+------------------------------------------+\n";
     
      if (select_vocab == 1)
      {
        strcpy(vocab_file,"german_vocab_char.txt");
      }
      else if (select_vocab == 2)
      {
        strcpy(vocab_file,"german_vocab_nouns.txt");
      }
      else if (select_vocab == 5)
      {
        cout<<"| Please enter the text file name that     |\n"
            <<"| contains the vocab, i.e words.txt,       |\n"
            <<"| or vocab.txt: "; 
        cin>>vocab_file;
        cout<<"+------------------------------------------+\n";
      }
      else
      {
        strcpy(vocab_file, "german_vocab_char.txt");
      } 
     
      ifstream OpenFile(vocab_file);
     
      if (!OpenFile)
      {
        cout<<"| I was unable to open that vocab file.    |\n";
        cout<<"| Please re-run the program to try again.  |\n";
        cout<<"+------------------------------------------+\n";
        exit(1);
      }
     
      while(!OpenFile.eof())
      {
        OpenFile.getline(ch,50,'\t');
    
        if ( !strcmp(ch, "") )
        {
          exit(1);
        }
    
        cout<<"| "<<ch<<": ";
        OpenFile.getline(chin,50,'\n');
    
        cin.getline(user, 50);
    
        if ( !strcmp(user, "quit") )
        {
          exit(1);
        }
    
        if( !strcmp(user, chin) )
        {
          cout<<"| Ja, Voll! Das ist richtig!               |\n";
        }
        else
        {
          cout<<"| NINE, NINE!   "<<chin<<"\n";
          ask_again(ch, chin);
        }
        cout<<"+------------------------------------------+\n";
      }
      OpenFile.close();
    
      cout<<"\n\n\n";
    
      return 0;
    }
    
    int ask_again(char question[50], char answer[50])
    {
      static char user[50];
      
      cout<<"| "<<question<<": ";
    
      cin.getline(user, 50);
    
      if ( !question )
      {
        exit(1);
      }
    
      if ( !strcmp(user, "quit") )
      {
        exit(1);
      }
    
      if( !strcmp(user, answer) )
      {
        cout<<"| Ja, Voll! Das ist richtig!               |\n";
        return 0;
      }
      else
      {
        cout<<"| NINE, NINE!   "<<answer<<"\n";
        ask_again(question, answer);
      }
    }
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    I think what is happening is when you read in the menu selection (cin>>select_vocab;) there is a unconsumed linefeed from when the user hit enter. You will have to burn that off (you can just do a getc() or read in a dummy buffer using getline()). I imagine that will solve your problem.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. Happy Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    UK (University of Kentucky)
    Posts
    1,809
    Rep Power
    48
    Sweet, thanks! I figured that's what was causing it, but I didn't know to trap it. Does that happen often?

    I added this line under the cin>>select_vocab;

    temp = cin.get();
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    It always happens. You _can_ turn off input buffering (I don't remember how) and deal with each character as the user types it (you also have to deal with delete!). I usually read input into a string buffer, then parse it myself instead of using the built-in functions. I like the control. It is real easy to accidentally create a situation where you can create a buffer overflow (using a gets() is the most famous), so I am paranoid about that.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    Please tell me that the following is an intentional joke. :)

    Code:
    cout<<"| NINE, NINE!   "<<chin<<"\n";
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    "Ja, Voll!" stimmt auch nicht. Das soll "Jawohl!" heißen.
  16. #9
  17. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Originally posted by mitakeet
    You _can_ turn off input buffering (I don't remember how) and deal with each character as the user types it (you also have to deal with delete!).
    setbuf(stdxxx, NULL);
  18. #10
  19. Happy Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    UK (University of Kentucky)
    Posts
    1,809
    Rep Power
    48
    Originally posted by 3dfxMM
    Please tell me that the following is an intentional joke. :)

    Code:
    cout<<"| NINE, NINE!   "<<chin<<"\n";
    Why would it be a joke? It says, "NO, NO!" (only in German :D )

    "Ja, Voll!" stimmt auch nicht. Das soll "Jawohl!" heißen
    Danke für das. Ich bin nich so gut mit mein Deutsch. Aber, Ich müß lerene viel...

    I am not very good in German yet (hince, the purpose of that program I made :p)

    Bist du von Deutschland?

IMN logo majestic logo threadwatch logo seochat tools logo