while error ?
-
this program does not halt why ?
//les02for.cpp
#include
#include
using namespace std ;
int main()
{
string in = " ";
while ( in != "" )
{
cout << "What is your name ? \n" ;
cout << "[ Pres enter to end this .]\n" ;
cin >> in ;
cout << "Hello " << in << " .\n" ;
}
cout << "n\n\n\[ GAME OVER ]\n" ;
cin.get() ;
return 0;
} -
this program does not halt why ?
//les02for.cpp
#include
#include
using namespace std ;
int main()
{
string in = " ";
while ( in != "" )
{
cout << "What is your name ? \n" ;
cout << "[ Pres enter to end this .]\n" ;
cin >> in ;
cout << "Hello " << in << " .\n" ;
}
cout << "n\n\n\[ GAME OVER ]\n" ;
cin.get() ;
return 0;
}bluatigro wrote:
why ?
Have you stepped through it using the debugger (to see what value
in
has when you think it should be 'empty')? Thestring
class may not have a!=
operator so what you may be comparing is the object itself against a string literal (rather than the contents of the object)."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
bluatigro wrote:
why ?
Have you stepped through it using the debugger (to see what value
in
has when you think it should be 'empty')? Thestring
class may not have a!=
operator so what you may be comparing is the object itself against a string literal (rather than the contents of the object)."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
std::string have != operator. The problem is with cin, it does not takes new line char or leading space chars by default.
-
this program does not halt why ?
//les02for.cpp
#include
#include
using namespace std ;
int main()
{
string in = " ";
while ( in != "" )
{
cout << "What is your name ? \n" ;
cout << "[ Pres enter to end this .]\n" ;
cin >> in ;
cout << "Hello " << in << " .\n" ;
}
cout << "n\n\n\[ GAME OVER ]\n" ;
cin.get() ;
return 0;
}To make it working, change as following:
#include
#include
using namespace std ;
int main()
{
string in = " ";
while ( in != "" )
{
cout << "What is your name ? \n" ;
cout << "[ Pres enter to end this .]\n" ;
getline(cin, in );
cout << "Hello " << in << " .\n" ;
}
cout << "n\n\n\[ GAME OVER ]\n" ;
cin.get() ;
return 0;
}