|
-
June 11th, 2008, 01:00 AM
#1
Second part of my program repeats and...
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.
Code:
#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;
}
Last edited by Artitatt; June 11th, 2008 at 01:03 AM.
-
June 11th, 2008, 01:30 AM
#2
Re: Second part of my program repeats and...
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:
Code:
#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.
-
June 11th, 2008, 01:34 AM
#3
Re: Second part of my program repeats and...
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.
Last edited by Artitatt; June 11th, 2008 at 01:37 AM.
-
June 11th, 2008, 10:29 AM
#4
Re: Second part of my program repeats and...
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.
Code:
cout << "Do you wish to quit? y/n ";
cin >> quitKey;
string extraChar;
cin>>extraChar;
-
June 11th, 2008, 11:33 AM
#5
Re: Second part of my program repeats and...
 Originally Posted by Mybowlcut
}[/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.
-
June 11th, 2008, 08:55 PM
#6
Re: Second part of my program repeats and...
 Originally Posted by pipa
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.
Code:
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?
-
June 11th, 2008, 09:58 PM
#7
Re: Second part of my program repeats and...
I added cin.ignore()
to
Code:
#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. :-)
Last edited by Artitatt; June 12th, 2008 at 12:33 AM.
-
June 11th, 2008, 11:15 PM
#8
Re: Second part of my program repeats and...
 Originally Posted by Artitatt
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!
-
June 13th, 2008, 04:35 PM
#9
Re: Second part of my program repeats and...
 Originally Posted by pipa
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|