C++ Question.
-
Then simply use: cin.getline( line, 100) for the example given. The DEFAULT will be '\n' (carriage return) added as the final character when the return (enter) key is pressed.
In case you haven't been reading the other posts, 'getline()' has a bug in it, and is unfit to use (at least for VC++ 6). Even the fix that MS has issued, DOES NOT WORK!! Take my word for it, I am very acquainted with the various iostream member functions (including 'getline()') and the only reason why I posted the question on this forum, was in hope that somebody may have found a workaround for the problems this I/O function has been plagued with. I understand VC++ 7 does not have this problem, and that is good news for those who might want to use it. :suss: William Fortes in fide et opere!
-
In the use of
string str;
getline(cin, str); // data gets entered here
if(str=="???" || str=="")
{
// invalid string
}
else
{
// VALID string
}The logic is always executing the invalid string segment, even when valid data is entered. Why is this happening? Thanks for any insight. :) William Fortes in fide et opere!
I have 6.00 and works fine for me, try this out: #include "stdafx.h" #include "iostream.h" #include "string.h" int main(int argc, char* argv[]) { char line[100]; cout << "Type a line" << endl; cin.getline( line, 100); if(strcmp(line, "xxx") == 0) cout << "You entered xxx"; else cout << "You did not enter xxx"; return 0; }
-
In the use of
string str;
getline(cin, str); // data gets entered here
if(str=="???" || str=="")
{
// invalid string
}
else
{
// VALID string
}The logic is always executing the invalid string segment, even when valid data is entered. Why is this happening? Thanks for any insight. :) William Fortes in fide et opere!
Doing it the way you have you have to press the enter key twice after getline to get a response (the bug you were referring to). See http://support.microsoft.com/default.aspx?scid=kb;en-us;Q240015 for the fix ( the fix does work if you make the changes in the right file. The file referred to is in two different locations. You might as well change the code in both: MicrosoftVisualStudio\VC98\Include\String and MicrosoftVisualStudio\VC98\CRT\SRC\String I did and the enter key only has to be pressed once now and the code below which is like your way of doing it works fine. #include "stdafx.h" #include "string" #include "iostream" using namespace std ; int main(int argc, char* argv[]) { string s1; cout << "Enter a sentence:"; getline(cin,s1); if(s1 == "???" || s1 == "") cout << "First case"; else cout << "Second case"; return 0; }