create a random number with 1 to 3 and disply a message

hi i am trying to get this assignment done for school. i have to create random number with 1 to 3 and display a message. heres what i got so far but im stuck.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std; 

int main()
{
	int number;
	srand(time(0));
	int randomNum = rand() %3+1;
	number=3;
	cout <<"Enter a number between 1 and 3:";
	cin >> number; 
	cout <<endl;
	if(number == randomNum){
	cout<<"You guessed the right number"<<endl;
}
	else
	switch(number){
		case 1:
			cout<<"You entered 1"<<endl;
			if(number == randomNum)
			cout<<"You guessed the right number"<<endl;
			cout<<"Sorry not the right number"<<endl;
			break;
		case 2:
			cout<<"You entered 2"<<endl;
			if(number == randomNum)
			cout<<"You guessed the right number"<<endl;
			cout<<"Sorry not the right number"<<endl;
			break;
		case 3:
			cout<<"You entered 3"<<endl;
			if(number == randomNum)
			cout<<"You guessed the right number"<<endl;
			cout<<"Sorry not the right number"<<endl;
			break;
		}
return 0;
}
You test on line 15 so i'm not sure why you need to complicate things with a switch statement?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
	int number;
	srand(time(0));
	int randomNum = rand() % 3 + 1;
	number = 3;
	cout << "Enter a number between 1 and 3:";
	cin >> number;
	cout << endl;
	if (number == randomNum)
	{
		cout << "You guessed the right number" << endl;
	}
	else
	{
		cout << "Sorry not the right number" << endl;
	}
	return 0;
}
I appreciate your response.
is this the best way to have a response generated for each number guessed?
Topic archived. No new replies allowed.