Thread: cin.getline....

  1. #1
    Inez
    Guest

    cin.getline....

    ok i dont understand this...maybe someone can help me...

    Code:
    #include <iostream>
    #include <windows.h>
    #include <time.h>
    #include <iomanip>
    using namespace std;
    
    
    
    
    int main()
    {
    int choice;
    int reuse(0);
    
    const int MAX=80;
    
    char Intro[]("THE BATTLE WITHIN");
    
    char name1[MAX];
    
    cout <<setw(45)<<Intro;
    
    /*Sleep(1000*2);*/
    
    BEGIN:
    
    /*Sleep(1000*2);*/
    
    
    system("cls");
    
    cout <<"Declare your choosing:\n"
         <<"1)Begin a new journey.\n"
    	 <<"2)Continue your journey.\n"
    	 <<"3)Exit.\n";
    cin  >>choice;
    
    if(choice==1)
    {goto Start;}
    if(choice==2 || choice==3)
    {goto END;}
    else
    {
    reuse++;
    if(reuse==2)
    {
    cout <<"You have angered me...\n";
    Sleep(1000);
    cout <<"LEAVE!\n";
    return 0;
    }
    else
    cout <<"Maybe your finger slipped...\n"
         <<"Or maybe you are just an idiot...\n"
    	 <<"Whatever the case...dont do it again...\n";
    goto BEGIN;
    }
    
    Start:
    reuse=0;                                     //Right here...
    cout <<"Welcome\n";                 //i had to rig it all weird like this...
    do                                               //im just wondering why...
    {                                                 
    cin.getline(name1,MAX,'\n');
    reuse++; 
    }while(no<2);
    
    cout <<name1;
    
    
    END:
    return 0;
    
    }
    Alrite that is the version that works...but now if you take out the 'do while' statement....it suddenly doesnt work...it just skips over to the 'return 0;' i dont understand what my loop is doing...i was like all ........ed off...determined not to go to bed till i fixed it...heh...yea im not the bestest programmer...but im learning...

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    its possible the first charcter going in is an /n character which would quit the cin.getline
    Monday - what a way to spend a seventh of your life

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    When you use operator >>, it leaves a '\n' in the buffer. cin.getline() does not ignore '\n''s when obtaining input from the buffer. The '\n' left by operator >> is being picked up by getline() in your code. Put cin.ignore(80,'\n') before your call to getline to remove the '\n' and other characters that may be left in the buffer from your previous input.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    While we are on the subject, I have a question regarding the buffer. When using cin>> and cout<<, do they use the same buffer.

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You can answer that by doing -

    cout << (long*)cin.rdbuf()<< '\n'<< (long*)cout.rdbuf();

    Which will display the addresses of cin and cout's streambuf object. I'm not sure if it would be possible to use the same buffer but they are linked, so that a call that enters data into cin's streambuf will flush the data in cout's streambuf (this isn't true of the reverse).

  6. #6
    Inez
    Guest
    Thanks...that works...and it doesnt look all weird ;P

  7. #7
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158
    someone please explain this further, I am having this trouble too.None of your replies work ! (lol so it seems).I tried using ignore etc-it actually increases the amount of times you need to press enter-from 2 to 3 !!!!..........Maybe this will jog someones memory to work it out!!
    Such is life.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 06:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  3. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM