-
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...
-
its possible the first charcter going in is an /n character which would quit the cin.getline
-
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.
-
While we are on the subject, I have a question regarding the buffer. When using cin>> and cout<<, do they use the same buffer.
-
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).
-
Thanks...that works...and it doesnt look all weird ;P
-
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!!