Input use in Output

Write your question here.
Whenever I put in a number other than 42 it doesn't input the number after the sentence like I want it to. For example, if you put in 12 I want it to say "Wrong! The answer to life, the universe, and everything is not 12" I don't understand why it's not working.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <iostream>
using namespace std;

int main() {
	int hitchhikers;
	hitchhikers = 40;

	int guide;
	guide = 2;

	int the_answer;
	the_answer = hitchhikers + guide;

	int what_is;

	cout << "What is the answer to life, the universe, and everything?" << endl;
	cin >> what_is;

	if (what_is == 42) {
		cout << "The answer to life, the universe, and everything is 42. What does 42 mean though?" << endl;
	}
	else {
		cout << "Wrong! The answer to life, the universe, and everything is not" + (int)what_is << endl;
	}

	system("pause");
	return 0;
}

change your line 23 to this:

cout << "Wrong! The answer to life, the universe, and everything is not "<<what_is << endl;

also, when you declare an integer, you can also initialize it in the same line, so rather than
1
2
int hitchhikers;
hitchhikers = 40;

you can just write:
int hitchhikers=40;
and you could also have simply initialized "what_is" to 42 but maybe that's what you wanted to do anyway.
Last edited on
Topic archived. No new replies allowed.