|
-
Feb 3rd, 2003, 04:16 PM
#1
Thread Starter
Fanatic Member
[Resolved (Sort of...)] cin.ignore() / cin.get()
Why do I have to hit Return an extra time to exit this program?
Code:
//char.cpp
#include <iostream>
using namespace std;
char control();
void main(void)
{
char ans;
ans = control();
cout << ans << endl;
cout << cin.get() << endl;
}
char control()
{
char c;
cout << "Y or N: ";
cin.get(c);
while (c!= 'Y' && c!= 'N' && c!='y' && c!='n')
{
cout << "Y or N: ";
cin.get(c);
cin.ignore(10, '\n');
}
cin.ignore(10,'\n');
return (c);
}
It seems to me that the program stops at cin.get() if there's nothing in the input buffer. Why does it do this?
Last edited by McCain; Feb 17th, 2003 at 07:04 PM.
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Feb 3rd, 2003, 04:54 PM
#2
Stuck in the 80s
Try this:
Code:
//char.cpp
#include <iostream>
using namespace std;
char control();
void main(void)
{
char ans;
ans = control();
cout << ans << endl;
cout << cin.get() << endl;
}
char control()
{
char c;
cout << "Y or N: ";
cin.get(c);
while (c!= 'Y' && c!= 'N' && c!='y' && c!='n')
{
cout << "Y or N: ";
cin.ignore();
cin.get(c);
}
return (c);
}
Edit: Nevermind, I still see problems.
Last edited by The Hobo; Feb 3rd, 2003 at 05:01 PM.
-
Feb 4th, 2003, 04:11 AM
#3
Thread Starter
Fanatic Member
What exactly does cin.ignore() do? I know that cin.ignore(10, '\n') deletes 10 characters from the input buffer or untill it finds a \n
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Feb 4th, 2003, 03:10 PM
#4
Stuck in the 80s
Originally posted by McCain
What exactly does cin.ignore() do? I know that cin.ignore(10, '\n') deletes 10 characters from the input buffer or untill it finds a \n
cin.ignore() will just clear out the first character in the buffer. I realize now that if someone enters something more than one character, the buffer is going to be larger than that and mess up your program.
This is how I'd do it:
Code:
#include <iostream>
#include <string>
using namespace std;
string control();
void main(void) {
string ans;
ans = control();
cout << ans << endl;
}
string control() {
string c;
cout << "Y or N: ";
getline(cin, c);
while (c != "Y" && c != "N" && c != "y" && c != "n") {
cout << "Y or N: ";
//cin.ignore(); //if you're using MSVC++, you may need this line...
getline(cin, c);
}
return (c);
}
-
Feb 4th, 2003, 04:12 PM
#5
Thread Starter
Fanatic Member
I know I can use getline, I can also use cin >> but the exersice was to use cin.ignore with cin.get (I'm doing the exercises in a book I've got...)
Thanks anyway for taking the time to help me
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
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
|