-2

I'm writing a basic program, similar to ELIZA(online therapist) simple questions and answers but I'm stuck at the end. after cin >> answer; I'm not able to write anything.

int main () {
    short number;
    string color;
    string sport;
    int answer;
    string travel;


    // Greets user
    cout << "Hello, I'm Samantha" << endl;
    // Asks user for their favorite sport
    cout << "What's your favorite sport?";
    cin >> sport;
    cout << "I like " << sport << " too!" << endl;
    cout << "How about your favorite color?";
    cin >> color;
    cout << "Not my favorite color but it's nice!" << endl;
    cout << "Tell me something you've never told anyone before";
    cin >> answer;
    cout << "Don't worry, your secret is safe with me!" << endl;
    cout << "Hows your life going?";
    cin >> answer;

return 0;
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Zain N.
  • 1
  • 1
  • 4

1 Answers1

2

your variable named "answer" has a data type integer. the first time you prompt the user to enter something from the console (apparently to be a string) the cin object attempts to initialize "answer" with a string (probably because the prompts does not ask for a number) which kills the cin object which will not allow the object to take instructions...so the next time you want to use it there is no cin object.

Just change the data type for "answer" to string.

Dr t

Dr t
  • 247
  • 4
  • 11
  • Also, note that if you need to fix `std::cin` after breaking it like this, you use [`std::cin.clear()`](http://stackoverflow.com/a/10877539/5386374) to clear the error flag, then either attempt to read the data as a different type, or remove it with [`std::cin.ignore()`](http://stackoverflow.com/questions/25475384/when-and-why-do-i-need-to-use-cin-ignore-in-c). – Justin Time - Reinstate Monica Apr 26 '17 at 20:43