Reading 'enter'

Started by
13 comments, last by raz0r 18 years, 1 month ago
My program won't read nothing in a string. How can I make it? For instance I have my 'cin' asking for std::string 'what' and I want to make it so that something happens if you don't type anything. Thanks.
Advertisement
Are you using 'what' or is that just how you were quoting it in your post?

To cin a character you use single quotes, to cin a string you use double quotes.

If that doesnt help than could you please be more specific?

So you are trying to cin a string, and if there is nothing there, then do something else.


string mystring;

cout << "Please enter a sentense\n";
getline(cin,mystring);

if (mystring == "whatever")
{
//do something
}

else
{
//do something else
}

The else statement is where you should put the message or whatever if they didnt type anything else

edit: Oh.. if you use cin for strings, it stops when ever it gets to a space, if you use getline it will take the entire line :)
I have:

(roughly)
std::string what;cin >> what;if(what == "keywords"){    cout << "blah blah\n";}else if(what == ""){    cout << "something else\n";}


I want to be able to read the nothing ("") is there some(simple) way to do such?

edit: oh, you added more to your post, one sec.
oh ok, I misunderstood...

wait a second, you want to read whats in the ("") you said, I dont think you could do that... the word in the "" is what you would have hard coded in...
Quote:Original post by cchase88
The else statement is where you should put the message or whatever if they didnt type anything else


Would that work if I have it like this:
if(what == "this"){    cout << "stuff\n";}else if(what == "that"){    cout << "more stuff\n";}else{    cout << "different stuff\n";}



Quote:edit: Oh.. if you use cin for strings, it stops when ever it gets to a space, if you use getline it will take the entire line :)


Will that read the "" nothing? I only am using one word, but I will change it to getline if it reads ""
Quote:Original post by cchase88
oh ok, I misunderstood...

wait a second, you want to read whats in the ("") you said, I dont think you could do that... the word in the "" is what you would have hard coded in...


Hmm, okay I will work around that particler problem then. thanks though.
ok... I made this, it works after they hit enter twice.. I dont know how to make it so Its only if they hit it once :S

#include <iostream>
#include <string>
using namespace std;

int main()
{
string mystring;

cout << "Please enter what you would like to do\n";
cout << "(type the word you would like to do)\n\n";
cout << "this\n";
cout << "that\n";

getline(cin, mystring);

if (mystring == "this")
{
cout << "You typed in this!\n";
}

else if (mystring == "that")
{
cout << "You typed in that!\n";
}

else
{
cout << "You didnt type anything!\n";
}

return 0;
}
Quote:Original post by cchase88
ok... I made this, it works after they hit enter twice.. I dont know how to make it so Its only if they hit it once :S

#include <iostream>
#include <string>
using namespace std;

int main()
{
string mystring;

cout << "Please enter what you would like to do\n";
cout << "(type the word you would like to do)\n\n";
cout << "this\n";
cout << "that\n";

getline(cin, mystring);

if (mystring == "this")
{
cout << "You typed in this!\n";
}

else if (mystring == "that")
{
cout << "You typed in that!\n";
}

else
{
cout << "You didnt type anything!\n";
}

return 0;
}


I don't see why you'd need to hit enter twice...

Edit: One more thing, if you're going to output "You didn't type anything", you should probably check if the string is empty...

i.e.:
else if(mystring.empty())	std::cout << "You didn't type anything!" << std::endl;else	std::cout << "You typed something that I'm unable to recognize!" << std::endl;
Quote:Original post by raz0r
Edit: One more thing, if you're going to output "You didn't type anything", you should probably check if the string is empty...

i.e.:

else if(mystring.empty())
{
std::cout << "You didn't type anything!" << std::endl;
}


Wait; what? You can check if it's empty?
Quote:Original post by Servant of the Lord
Quote:Original post by raz0r
Edit: One more thing, if you're going to output "You didn't type anything", you should probably check if the string is empty...

i.e.:

else if(mystring.empty())
{
std::cout << "You didn't type anything!" << std::endl;
}


Wait; what? You can check if it's empty?


Yes. There are a few ways to check if a string's empty...

This topic is closed to new replies.

Advertisement