Sign in to follow this  
Pharaoh12

Need Help with simple "input program"?!?

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
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
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
Heres the code:


#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;
cout << "Hi "<< yourname << " How was your day today?\n";

system("PAUSE"); // Pauses program until a key is pressed.
return 0; //Shuts program down
}




[Edit:] Didn't see 'cin.get();' Oh well, try code anyway. Just copy it all so as help us understand your errors. If it doesn't work, tell us what the errors say.

Share this post


Link to post
Share on other sites

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