• Advertisement

Archived

This topic is now archived and is closed to further replies.

Strange string behavoir

This topic is 5140 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement