Jump to content
  • Advertisement
Sign in to follow this  
Servant of the Lord

Reading 'enter'

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

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.

Share this post


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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!