1

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(); } 
5
  • 3
    What input are you typing? (You don't need cin.clear() BTW - that clears any error conditions on the cin stream, and there's no reason to think you'd have any.) Commented Nov 26, 2020 at 23:24
  • 1
    Shouldn't this be a monkey see++ monkey do program? Commented Nov 26, 2020 at 23:27
  • 1
    micronag: The C++ Standard requires main must return int. Commented Nov 26, 2020 at 23:29
  • 1
    Are you giving more than one characters as input when it is asking for the first time? If yes, then it will not ask for the input the second time as the buffer of cin already has characters available. The next character that you typed will get assigned to monkey_2. Commented Nov 26, 2020 at 23:30
  • (More specifically, I'll point out that char accepts a single character, and >> skip whitespace, so if you've typed something like "jump" and pressed ENTER, it'll read j into monkey_1 and u into monkey_2 without waiting for more text or another press of ENTER. Same if you typed j SPACE u ENTER.) Commented Nov 26, 2020 at 23:32

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

Why would it be a problem for the monkey variables to be char?
Because there was no assumption monkey is just 1 character. :) With original code, if puts some characters as input, then it will be given to 2nd's monkey.
There's always assumptions about inputs - especially if you're trying to simplify so you remove sanitisation. It's a beginner problem, so having monkey A and monkey B seems like a simple and plausible problem. To me it's not obvious that using char is wrong, so I wouldn't assume that the OP "needs to [modify]" them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.