Need Help with simple "input program"?!?

Started by
9 comments, last by Servant of the Lord 18 years, 1 month ago
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?
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...
what do you mean...do i have to put how many characters?I tried taking out the cin.ignore but its the same outcome.
I got it to accept 1 letter but when its more than 1 letter the program messes up? What wrong?
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;}
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 characterstring yourname;cout<<"Hi, Whats your name? ";cin>> yourname;cin.ignore();cout<<"Hi "<< yourname << " How was your day today?\n";cin.get();return 0;}
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.
Nothing so far that ive tried has worked!!! Ive tried your ideas, Nicksterdomus and agi_shi, but they havent worked?!?!
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 characterstring yourname;cout<<"Hi, Whats your name? ";cin>> yourname;cin.ignore();cout<<"Hi "<< yourname << " How was your day today?\n";cin.get();return 0;}
Lol; add the following line:

system("PAUSE");

before the:

return 0;

in nicksterdomus' code

What is happening is that your program is exiting to fast for you to see.(I think)

This topic is closed to new replies.

Advertisement