Jackson Pike
Jackson Pike

Reputation: 53

C++ not waiting for an input

so I have been learning C++ and was working on a monkey see monkey do program and i managed to get the first input working but the second input it just skips straight over it, i have no clue why and any help would be apreciated.

#include <iostream>
#include <string>
using namespace std;
char monkey_1;
char monkey_2;

int msmd()
{
    cout << "monkey one:"; cout << endl;
    cin >> monkey_1;
    system("cls");
    cout << "monkey two:"; cout << endl;
    cin.clear();
    cin >> monkey_2;
    cout << "waiting"; cout << endl;
    if (monkey_1 == monkey_2)
    {
        cout << "both monkeys are happy."; cout << endl;
    }
    else
    {
        cout << "the monkeys are upest."; cout << endl;
    }
    return 0;
}

void main()
{
    msmd();
}

Upvotes: 1

Views: 163

Answers (2)

UnderDog
UnderDog

Reputation: 1

There are two point needs to be modified.

  1. char monkey_1 and mokeny_2 should be declared as string for get more than 1 character.
  2. void main need to be changed as int main.

Upvotes: 0

Luis
Luis

Reputation: 373

Do you intent to only get a single character from the input as the monkeys are of type char? If not, change them to string, otherwise it will only assign a single character per cin.

If you want to input a sentence, cin also splits on spaces, so if you enter "something else", the first cin will assign something to monkey_1, and the second cin will automatically assign else to monkey_2. To get around this you can use getLine(cin,monkey_x).

Upvotes: 2

Related Questions