Strange string behavoir

Started by
5 comments, last by Krypto night 20 years, 3 months ago
While using strings either my code or my compiler (Visual c++ 6) is causing my program to have strange behavoir. #include <iostream> #include <string> using namespace std; int main() { string name; while (name.empty()) { cout << "please enter your name: "; getline(cin, name); } cout << "Thanks " << name << endl; return 0; } thats the code but the strange thing is that when it is compiled i get this sort of thing. Please enter your name: Space <-- Why? It requires you to press return twice Please enter your name: Please enter your name: Please enter your name: Please enter your name: Chris Please enter your name: <-- Why? Thanks chris Press any key to continue. (The program is supposed to test for empty strings so that it why there are unfilled spaces) any help would be appreciated. [edited by - krypto night on January 22, 2004 2:07:03 PM]
--------------------------------MSDN
Begginers
Advertisement
Not sure why, and it has been a while since I coded in C. Anyway, here is a fix that should solve you problems.

#include <iostream>
#include <string>

using namespace std;

int main()
{
string name;
while (name.empty())
{
cout << "please enter your name: ";
cin >> name;
}
cout << "Thanks " << name << endl;
return 0;
}

Here''s a fix.
Ok thanks youve been very helpfull.
--------------------------------MSDN
Begginers
How do you apply the fixes for libraries?

I looked in string.h and cannot find the lines
else if (_Tr::eq(_C, _D))
{_Chg = true;
_I.rdbuf()->snextc(); // replace snextc with sbumpc
break; }

as indicated on http://www.dinkumware.com/vc_fixes.html so what am i doing wrong and how do i apply the fixes?
--------------------------------MSDN
Begginers
quote:Original post by Krypto night
How do you apply the fixes for libraries?

I looked in string.h and cannot find the lines ...

Did you look in string.h or string? It''s supposed to be in string, not string.h.

.bas
.basprintf ( "And for the %dth time, I'm not American!", ++lIdiots );My homepage
Ah ok it works now.
Thanks every one it works perfect now.
--------------------------------MSDN
Begginers

This topic is closed to new replies.

Advertisement