Jump to content
  • Advertisement
Sign in to follow this  
Pharaoh12

Need Help with simple "input program"?!?

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

Im using Visual C++ 2005 Express Edition and im trying out this input program thats looks simple but keeps messing up on me and i cant find the problem.Heres the code: #include <iostream> using namespace std; char main() { char yourname; cout<<"Hi, Whats your name? "; cin>> yourname; cin.ignore(); cout<<"Hi "<< yourname <<"How was your day today?\n"; cin.get(); return 1; } After the person inputs his\her name it shows quik text and then closes?!This really isnt that HUGE of a problem for me but i just get curious on whats the problem?

Share this post


Link to post
Share on other sites
Advertisement
Try removing the cin.ignore() call before the last cout statement. Also, doesn't the variable yourname need to be a char *? The way you've set it up now, it's only one character...

Share this post


Link to post
Share on other sites
what do you mean...do i have to put how many characters?I tried taking out the cin.ignore but its the same outcome.

Share this post


Link to post
Share on other sites
Quote:
Original post by Pharaoh12
I got it to accept 1 letter but when its more than 1 letter the program messes up? What wrong?


You need an array of characters... use std::string:

#include <string>

int main() {
stuff...

std::string name;
cin >> name;
}

Share this post


Link to post
Share on other sites
You are using a single character, so cin << yourname reads in a single character, and leaves any other characters on the input buffer. Then, your message is printed. Then, cin.get() reads in the next character and the program ends. Instead of declaring yourname as a single character, declare it as a string. That way, cin << yourname will read in more than one character. Then print your message, then cin.get() will be called and wait for you to enter something because there is nothing already on the input buffer. Keep in mind, that even if you declare yourname as a string, if you enter an spaces, it will mess up still because cin will stop reading in characters when it reaches whitespace.
Here's the code with changes I made:

#include <iostream>

using namespace std;

//main should return an int, why were you returning char?
int main()
{
// yourname is a string, can be used to read in more than one character
string yourname;

cout<<"Hi, Whats your name? ";
cin>> yourname;
cin.ignore();
cout<<"Hi "<< yourname << " How was your day today?\n";
cin.get();

return 0;
}

Share this post


Link to post
Share on other sites
Thank you very much Nicksterdomus and everyone else, thats the answer ive been waiting for!!You even explained everything instead of just telling me to change this or change that without me understanding what it was for...Thank YOU and everyone else who replied to help me out.

Share this post


Link to post
Share on other sites
Nothing so far that ive tried has worked!!! Ive tried your ideas, Nicksterdomus and agi_shi, but they havent worked?!?!

Share this post


Link to post
Share on other sites
Quote:
Original post by Pharaoh12
Nothing so far that ive tried has worked!!! Ive tried your ideas, Nicksterdomus and agi_shi, but they havent worked?!?!


Where is the problem? Does it not compile or does it not run as expected?
If it didn't compile, you probably needed to add #include <string> so that you can use the string class. Other than that, I need more information.

#include <iostream>
#include <string>

using namespace std;

//main should return an int, why were you returning char?
int main()
{
// yourname is a string, can be used to read in more than one character
string yourname;

cout<<"Hi, Whats your name? ";
cin>> yourname;
cin.ignore();
cout<<"Hi "<< yourname << " How was your day today?\n";
cin.get();

return 0;
}

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!