Sign in to follow this  

Reading 'enter'

This topic is 4306 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

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 :)

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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


Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Dang, to late now. I already found and coded an alternate method; oh well, I will use that next time.

New question:

Ename < GreenSlime.Name;

Does this copy the string .Name from object GreenSlime into string Ename?

It is coming up empty, but isn't reporting an error.

Share this post


Link to post
Share on other sites
Quote:
Original post by Servant of the Lord
Dang, to late now. I already found and coded an alternate method; oh well, I will use that next time.

New question:

Ename < GreenSlime.Name;

Does this copy the string .Name from object GreenSlime into string Ename?

It is coming up empty, but isn't reporting an error.


Ename = GreenSlime.Name;

Ename will now be whatever Name from GreenSlime is...

Share this post


Link to post
Share on other sites
Quote:
Original post by cchase88
wow, I didnt know about the mystring.empty() !!!!! I just learned something!! yay!


std::string::empty just returns true if the string's size is 0, so you can either check the size, or call empty. :|

Share this post


Link to post
Share on other sites

This topic is 4306 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this