cin issues [modified]
-
Harold_Wishes wrote:
cin >> buffer; // I had to enter 2 cin statements since the compiler for whatever reason cin.getline(buffer, 500, '\n'); // was not accepting one of them.
What compiler error were you receiving (when only one was present)?
Harold_Wishes wrote:
while (buffer == "\n")
What are you hoping to accomplish with this? If you are going to create a C++ program, quit messing around with
char[]
variables. Usestring
instead, especially since you are populating them withcin
.The compiler is not giving me an error per se. It's building the executable. But, when I run the executable, I am prompted to enter data in sequence with a series of multiple cout prompts and cin inputs. I have no problems when I am doing this in the first function call. But when main() makes a call to function 2 after function 1 terminates, the very first cin input in function 2 seems to get skipped (at least that is what the debugger is showing me). It does not recognize it). The debugger moves right to the next cout prompt. :( If I remember correctly, I took this out of functions and slapped it out all in main() without any troubles.
-
The compiler is not giving me an error per se. It's building the executable. But, when I run the executable, I am prompted to enter data in sequence with a series of multiple cout prompts and cin inputs. I have no problems when I am doing this in the first function call. But when main() makes a call to function 2 after function 1 terminates, the very first cin input in function 2 seems to get skipped (at least that is what the debugger is showing me). It does not recognize it). The debugger moves right to the next cout prompt. :( If I remember correctly, I took this out of functions and slapped it out all in main() without any troubles.
Sans the comments, and extraneous code, this should get you real close:
void add_number_of_subunits (int& data)
{
string buff;do { cout << endl; cout << "Please enter number of subunits: "; cin >> buff; } while (atoi(buff.c\_str()) < 1 || atoi(buff.c\_str()) > 12); data = atoi(buff.c\_str());
}
void add_node_at_end(_data& item, int& increment)
{
string buffer;cout << "Please enter the length of the protein sequence for Subunit " << increment << "> "; cin >> buffer; item.Length = buffer; cout << "Please enter the protein sequence for Subunit " << increment << "> "; cin >> buffer; item.Sequence = buffer; item.number = item.Sequence.size(); cout << "Enter brief status on the presence of a modified N\_Terminal: "; cin >> buffer; item.N\_Terminal = buffer; cout << "Enter brief status on the presence of a modified C\_Terminal: "; cin >> buffer; item.C\_Terminal = buffer; cout << endl; increment++;
}
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
Sans the comments, and extraneous code, this should get you real close:
void add_number_of_subunits (int& data)
{
string buff;do { cout << endl; cout << "Please enter number of subunits: "; cin >> buff; } while (atoi(buff.c\_str()) < 1 || atoi(buff.c\_str()) > 12); data = atoi(buff.c\_str());
}
void add_node_at_end(_data& item, int& increment)
{
string buffer;cout << "Please enter the length of the protein sequence for Subunit " << increment << "> "; cin >> buffer; item.Length = buffer; cout << "Please enter the protein sequence for Subunit " << increment << "> "; cin >> buffer; item.Sequence = buffer; item.number = item.Sequence.size(); cout << "Enter brief status on the presence of a modified N\_Terminal: "; cin >> buffer; item.N\_Terminal = buffer; cout << "Enter brief status on the presence of a modified C\_Terminal: "; cin >> buffer; item.C\_Terminal = buffer; cout << endl; increment++;
}
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
This code looks vaguely familiar ... someone else was asking about a similar problem a couple weeks ago. Anyway, the problem you are having is that when using cin, it ignores whitespace by default (that is, it sees it, but leaves it in the buffer). This is generally a problem only when reading in strings, but can happen in certain circumstances when reading other datatypes. Basically, this is what happens: Lets say that the following string is in the read buffer for cin: MyString\n cin will read "MyString" into the buffer and leave the \n on the read buffer. Now, the next cin call will see the \n and assume an empty string. An easy way to fix the problem would be to change:
DavidCrow wrote:
void add_number_of_subunits (int& data) { string buff; do { cout << endl; cout << "Please enter number of subunits: "; cin >> buff; } while (atoi(buff.c_str()) < 1 || atoi(buff.c_str()) > 12); data = atoi(buff.c_str()); }
to be this instead:
void add_number_of_subunits (int& data) { int check = 0; do { cout << endl; cout << "Please enter number of subunits: "; cin >> check; } while (check < 1 || check > 12); data = check; }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
This code looks vaguely familiar ... someone else was asking about a similar problem a couple weeks ago. Anyway, the problem you are having is that when using cin, it ignores whitespace by default (that is, it sees it, but leaves it in the buffer). This is generally a problem only when reading in strings, but can happen in certain circumstances when reading other datatypes. Basically, this is what happens: Lets say that the following string is in the read buffer for cin: MyString\n cin will read "MyString" into the buffer and leave the \n on the read buffer. Now, the next cin call will see the \n and assume an empty string. An easy way to fix the problem would be to change:
DavidCrow wrote:
void add_number_of_subunits (int& data) { string buff; do { cout << endl; cout << "Please enter number of subunits: "; cin >> buff; } while (atoi(buff.c_str()) < 1 || atoi(buff.c_str()) > 12); data = atoi(buff.c_str()); }
to be this instead:
void add_number_of_subunits (int& data) { int check = 0; do { cout << endl; cout << "Please enter number of subunits: "; cin >> check; } while (check < 1 || check > 12); data = check; }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
Zac Howland wrote:
This code looks vaguely familiar ... someone else was asking about a similar problem a couple weeks ago.
Same person. Still using
sscanf()
for validation, even though you provided him an alternative!Zac Howland wrote:
int check = 0; ... cin >> check;
I thought about this (i.e., using an
int
), but since he was storing the input string for later processing, I left it as astring
. -
Sans the comments, and extraneous code, this should get you real close:
void add_number_of_subunits (int& data)
{
string buff;do { cout << endl; cout << "Please enter number of subunits: "; cin >> buff; } while (atoi(buff.c\_str()) < 1 || atoi(buff.c\_str()) > 12); data = atoi(buff.c\_str());
}
void add_node_at_end(_data& item, int& increment)
{
string buffer;cout << "Please enter the length of the protein sequence for Subunit " << increment << "> "; cin >> buffer; item.Length = buffer; cout << "Please enter the protein sequence for Subunit " << increment << "> "; cin >> buffer; item.Sequence = buffer; item.number = item.Sequence.size(); cout << "Enter brief status on the presence of a modified N\_Terminal: "; cin >> buffer; item.N\_Terminal = buffer; cout << "Enter brief status on the presence of a modified C\_Terminal: "; cin >> buffer; item.C\_Terminal = buffer; cout << endl; increment++;
}
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
Although this did solve my original issue, it has caused 2 old problems to crop up again. 1) I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation. 2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.
-
This code looks vaguely familiar ... someone else was asking about a similar problem a couple weeks ago. Anyway, the problem you are having is that when using cin, it ignores whitespace by default (that is, it sees it, but leaves it in the buffer). This is generally a problem only when reading in strings, but can happen in certain circumstances when reading other datatypes. Basically, this is what happens: Lets say that the following string is in the read buffer for cin: MyString\n cin will read "MyString" into the buffer and leave the \n on the read buffer. Now, the next cin call will see the \n and assume an empty string. An easy way to fix the problem would be to change:
DavidCrow wrote:
void add_number_of_subunits (int& data) { string buff; do { cout << endl; cout << "Please enter number of subunits: "; cin >> buff; } while (atoi(buff.c_str()) < 1 || atoi(buff.c_str()) > 12); data = atoi(buff.c_str()); }
to be this instead:
void add_number_of_subunits (int& data) { int check = 0; do { cout << endl; cout << "Please enter number of subunits: "; cin >> check; } while (check < 1 || check > 12); data = check; }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
To Zac: My last response was just made to the other member (David_Crow) who provided some feedback, but I understand your point with the string datatype and will test this out. Thanks HRW. -- modified at 15:29 Monday 24th July, 2006
-
Zac Howland wrote:
This code looks vaguely familiar ... someone else was asking about a similar problem a couple weeks ago.
Same person. Still using
sscanf()
for validation, even though you provided him an alternative!Zac Howland wrote:
int check = 0; ... cin >> check;
I thought about this (i.e., using an
int
), but since he was storing the input string for later processing, I left it as astring
.DavidCrow wrote:
Same person. Still using sscanf() for validation, even though you provided him an alternative!
So it is. In C++, there are very few cases where using sscanf does not constitude a flaw in your implementation. This is not one of them.
DavidCrow wrote:
I thought about this (i.e., using an int), but since he was storing the input string for later processing, I left it as a string.
Since he needs it to set the iterations for a loop, it is easier (and more efficient) to read it as an
int
and to then callitoa
orsprintf
or use streambufs to store it for whatever purpose.If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Although this did solve my original issue, it has caused 2 old problems to crop up again. 1) I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation. 2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.
Harold_Wishes wrote:
- I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation.
That sounds familiar now. I don't agree with this behavior, but if you trully want it, the way to go about it is to do 2 consequetive getline calls (the first will get your data, the second will get you passed the '\n').
Harold_Wishes wrote:
2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.
That is the desired implementation for cin. You can override it, but generally, it is easier to just use getline if you need to get a string with spaces in it.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Although this did solve my original issue, it has caused 2 old problems to crop up again. 1) I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation. 2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.
Harold_Wishes wrote:
- I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation.
So what about something like:
do
{
cin.clear(); // reset cin, so more input can be done
cin.ignore(100, '\n'); // remove bad data, up to 100 characters
// or until the end of line is reached
cout << "Please enter number of subunits: ";
cin >> data;
} while (! cin && (data < 1 || data > 12));Harold_Wishes wrote:
2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.
Then you would indeed need to use:
string buffer;
cout << "Please enter the length of the protein sequence for Subunit " << increment << "> ";
getline(cin, buffer); -
Harold_Wishes wrote:
- I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation.
That sounds familiar now. I don't agree with this behavior, but if you trully want it, the way to go about it is to do 2 consequetive getline calls (the first will get your data, the second will get you passed the '\n').
Harold_Wishes wrote:
2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.
That is the desired implementation for cin. You can override it, but generally, it is easier to just use getline if you need to get a string with spaces in it.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
Regarding my first question, you're suggesting something like below:
char buffer[501] = {0}; // buffer for input
cout << "Please enter the length of the protein sequence for Subunit;
cin.getline(buffer, 500, '\n');
cin.getline(buffer, 500, '\n');I probably need to modify the above some since both getline functions are doing the same thing -- modified at 16:21 Monday 24th July, 2006
-
Harold_Wishes wrote:
- I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation.
That sounds familiar now. I don't agree with this behavior, but if you trully want it, the way to go about it is to do 2 consequetive getline calls (the first will get your data, the second will get you passed the '\n').
Harold_Wishes wrote:
2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.
That is the desired implementation for cin. You can override it, but generally, it is easier to just use getline if you need to get a string with spaces in it.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
For some reason, this seems to work although I do not know why it should. "cin" does not account for spaces. I added strings with spaces and no troubles much to my surprise :^)
char buffer[501] = {0}; // buffer for input
cout << "Please enter the length of the protein sequence;
cin >> buffer;
cin.getline(buffer, 500, '\n');-- modified at 17:56 Monday 24th July, 2006 UPDATE: I see from the output 2 getlines are needed.
-
Regarding my first question, you're suggesting something like below:
char buffer[501] = {0}; // buffer for input
cout << "Please enter the length of the protein sequence for Subunit;
cin.getline(buffer, 500, '\n');
cin.getline(buffer, 500, '\n');I probably need to modify the above some since both getline functions are doing the same thing -- modified at 16:21 Monday 24th July, 2006
No, buffer would get overwritten the second time.
char buffer[501] = {0}; // buffer for input char buffer1[501] = {0}; // buffer for clearing cout << "Please enter the length of the protein sequence for Subunit; cin.getline(buffer, 500, '\n'); cin.getline(buffer1, 500, '\n');
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
For some reason, this seems to work although I do not know why it should. "cin" does not account for spaces. I added strings with spaces and no troubles much to my surprise :^)
char buffer[501] = {0}; // buffer for input
cout << "Please enter the length of the protein sequence;
cin >> buffer;
cin.getline(buffer, 500, '\n');-- modified at 17:56 Monday 24th July, 2006 UPDATE: I see from the output 2 getlines are needed.
Why are you surpised that would work? That is pretty much what one of the examples I posted does. The reason it works is that the >> operator for cin will read until it hits whitespace, but not read the whitespace. getline reads until it gets to the number of characters you specificed, or the character you requested and does read it in. You really should look into the use case for "skipping" inputs in a console application. I can't think of any time where I would want the user to be able to hit enter and skip entering data. Now, entering a specific value to state they want to skip it, that is a different story ...
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac