Click to See Complete Forum and Search --> : Second part of my program repeats and...
Artitatt
June 11th, 2008, 01:00 AM
Why? I added a function to test things and it's now repeating once and automatically assumes I entered something other than y, or n.
#include <iostream>
#include <string>
using namespace std;
int squared(int s)
{
return s * 2;
}
int main()
{
top:
system("CLS");
cout << "What integer do you want squared? ";
int x;
cin >> x;
cout << squared(x) << endl;
string quitKey = "y";
do
{
topOfLoop:
cout << "Do you wish to quit? y/n ";
getline(cin, quitKey);
if (quitKey == "y") return 1;
if (quitKey == "n") goto top;
else
cout << "What? ";
goto topOfLoop;
} while (quitKey != "y");
return 0;
}
Mybowlcut
June 11th, 2008, 01:30 AM
Um you shouldn't use gotos.
I don't know why you're using getline when your desired input is only one character? For a quick fix, use cin instead of getline:#include <iostream>
#include <string>
using namespace std;
int squared(int s)
{
return s * 2;
}
int main()
{
top:
system("CLS");
cout << "What integer do you want squared? ";
int x;
cin >> x;
cout << squared(x) << endl;
string quitKey = "y";
do
{
topOfLoop:
cout << "Do you wish to quit? y/n ";
cin >> quitKey;
if (quitKey == "y") return 1;
if (quitKey == "n") goto top;
else
cout << "What? ";
goto topOfLoop;
} while (quitKey != "y");
return 0;
}I think your problem has to do with the stream having stuff in it from the first cin statement and it affecting the getline statement but someone else can clarify that.
Perhaps consider using a char for input if that's all you need.
Artitatt
June 11th, 2008, 01:34 AM
I'm using getline because the user might enter something like this: pouh reqh pourfh poieuh prfho (*%RT, heh... and I want to counter that.
pipa
June 11th, 2008, 10:29 AM
A very crude way is to use two "cin". One for the string (y or n) and other for the "enter" ('/n') character.
try it and see if it works.
cout << "Do you wish to quit? y/n ";
cin >> quitKey;
string extraChar;
cin>>extraChar;
Lindley
June 11th, 2008, 11:33 AM
}[/code]I think your problem has to do with the stream having stuff in it from the first cin statement and it affecting the getline statement but someone else can clarify that.
Put simply, cin >> var will only read until it encounters whitespace; and those whitespace characters will remain in the stream.
getline will read until it encounters a \n, and remove characters from the stream up to and including that \n.
So if you do a cin >> var and then a getline, the getline just grabs the \n left in the stream by the cin, and returns immediately.
Artitatt
June 11th, 2008, 08:55 PM
A very crude way is to use two "cin". One for the string (y or n) and other for the "enter" ('/n') character.
try it and see if it works.
cout << "Do you wish to quit? y/n ";
cin >> quitKey;
string extraChar;
cin>>extraChar;
I tried your example and it didn't quite work then changed it to:
string extraChar;
getline(cin, extraChar);
but now I have to enter in y or n twice as if to be validated.
It looks better, but it's not quite fixed. Thanks for the idea. Can you think of anything else?
Artitatt
June 11th, 2008, 09:58 PM
I added cin.ignore()
to
#include <iostream>
#include <string>
using namespace std;
int squared(int s)
{
return s * 2;
}
int main()
{
top:
system("CLS");
cout << "What integer do you want squared? ";
int x;
cin >> x;
cout << squared(x) << endl;
cin.ignore(); // newly added
string quitKey = "y";
do
{
topOfLoop:
cout << "Do you wish to quit? y/n ";
cin >> quitKey;
if (quitKey == "y") return 1;
if (quitKey == "n") goto top;
else
cout << "What? ";
goto topOfLoop;
} while (quitKey != "y");
return 0;
}
and it works now. :-)
pipa
June 11th, 2008, 11:15 PM
I tried your example and it didn't quite work then changed it to:
string extraChar;
getline(cin, extraChar);
but now I have to enter in y or n twice as if to be validated.
It looks better, but it's not quite fixed. Thanks for the idea. Can you think of anything else?
I did a similar kind of program in C long time back. I remember that I had to do the "reading in" operation twice as I had to compensate for the '/n' character.
But I like ur cin.ignore() function. thats new to me.
Good Luck!
Artitatt
June 13th, 2008, 04:35 PM
I did a similar kind of program in C long time back. I remember that I had to do the "reading in" operation twice as I had to compensate for the '/n' character.
But I like ur cin.ignore() function. thats new to me.
Good Luck!
Cool. Thanks.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.