Hi I'm learning C++ on my own here and am experiencing a problem with an introductory program I've created.

My book provided me with a bit of code near the end to prevent my program from closing on me immediately, however it is closing on me as soon as I finish entering my first name (first cin):

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

char name;
int age;
char favoritecolor;
    
cout << "Please enter your name: " << endl;
cin >> name;
cout << "Please enter your age: " << endl;
cin >> age;
cout << "Please enter your favorite color: " << endl;
cin >> favoritecolor;
cout << name << age << favoritecolor << endl;

cout << "CCCCCCCCC      ++               ++      " << endl;
cout << "CC             ++               ++      " << endl;
cout << "CC       ++++++++++++++   ++++++++++++++" << endl;
cout << "CC       ++++++++++++++   ++++++++++++++" << endl;
cout << "CC             ++               ++      " << endl;
cout << "CCCCCCCCC      ++               ++      " << endl;

// The following 6 lines of code delays termination of the program until
// the user presses the enter key. This is a convenience for those using
// a windows environment but it will work on every platform.  
char any_keystroke;
//reads and ignores up to 200 characters, or '\n' is read and ignored
cin.ignore(200, '\n');                                                                
cout << "\n\n";
cout << "Program execution has ended normally.\n";
cout << "Press the enter key to exit.";
cin.get(any_keystroke);

return 0;
}

Edited 5 Years Ago by sethwb: n/a

What's endl supposed to do when applied to cin?

I think it may have been a typo. The post has been edited since you posted.

@OP:
The main thing I see is that you are trying to input a string of characters to a variable declared as a single char. You can't do that. You must declare "name" and "favoritecolor" as either arrays of char or std::string objects. If you use these prompts:

cout << "Please enter your name: " << endl;
cin >> name;
cout << "Please enter your age: " << endl;
cin >> age;
cout << "Please enter your favorite color: " << endl;
cin >> favoritecolor;

With this input, for example:

Fbody
97
BloodRed

The variable "name" will have the single character 'F' in it, not the string "Fbody" with "body" remaining/lingering in the input stream. Then, when "age" tries to read from the stream, it reads an invalid value which causes the input stream to become corrupted. The corrupted input stream then causes "age" and "favoritecolor" to not have any values in them because they can't read from the input stream. You'll never even get the chance to put in the "97" and the "BloodRed" because the input statements will be completely ignored/bypassed while the program tries to recover from the stream corruption.

Fix your declarations.

Edited 5 Years Ago by Fbody: n/a

I think it may have been a typo. The post has been edited since you posted.

It seems the question that was asked changed entirely. So much for thread continuity, I guess I can ignore this guy from now on. :icon_rolleyes:

This article has been dead for over six months. Start a new discussion instead.