really bizarre.. compiler skipping gets()...

This is a discussion on really bizarre.. compiler skipping gets()... within the C++ Programming forums, part of the General Programming Boards category; I really have no clue of what's going on, so here's part of the code Code: void Queue::showMenu(){ int choice; ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    9

    really bizarre.. compiler skipping gets()...

    I really have no clue of what's going on, so here's part of the code

    Code:
    void Queue::showMenu(){
         int choice;
         do{
         cout <<n<<"This program will add people into a line and then retrieve them in the first-in first-out order."<<n;
         cout <<"If you want to check the line, type 1.\nIf you want to add someone into the line, type 2.\nIf you want to take the first person out of the line, type 3.\nIf you want to delete someome from the line, type 4(-1 to quit):\n-> ";
         cin >> choice;
              if(choice==-1){
                         break;
                         }
              else if(choice==1){
                             cout <<n;
                             q1.showLine();
                             break;
                             }                      
              else if(choice==2){
                             cout<<n<<"Please, type in the person\'s name:";
                             gets(str);
                             break;
                                                
                             }
              else if(choice==3){
                             q1.moveLine(*Line);
                             break;
                             }
              else if(choice==4){
                           
                             q1.deleteFromLine(str);
                             break;
                             }
                             
         }while(!*str);
    }
    Well... I have not finished writing the programme yet, but look right here...

    Code:
              else if(choice==2){
                             cout<<n<<"Please, type in the person\'s name:";
                             gets(str);
                             break;
                                                
                             }
    when the programme is actually being run it skips gets(str); and finishes... but if I wrote gets(str);gets(str);... or do{gets(str);}while(!*str); it would work...
    but even though, this is really bugging me, could someone explain me why it is doing that?

    thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because it's doing what you told it to do. gets reads up to the first enter-key/carriage return/new line/what have you. The first enter-key that it sees is the one directly after the 2 that you typed in, and so it's mission has been accomplished.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    The reason is that this leaves a newline in the input buffer:
    Code:
    cin >> choice;
    This newline is then read by the call to gets().

    Now, you should not be using gets(). Rather, use a std::string object with std::getline(). This would still result in the same problem, which you can solve by using cin.ignore() to ignore the newline left in the buffer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    oh, I got it ... thank you both!.... I just couldn't figure that out :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  2. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 05:04 AM
  3. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 12:07 PM
  4. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 06:42 AM
  5. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 11:28 AM

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