Strange string behavoir
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]
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;
}
#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;
}
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?
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?
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement