Can you solve this for me in C++ Part 1 , Prompt for user input for an integer i
ID: 3746128 • Letter: C
Question
Can you solve this for me in C++
Part 1,
Prompt for user input for an integer in the range 0..7. Display the message that corresponds to this value.
Sample run:
Welcome, let Magic 8-Ball show you the way! Think of a question, then enter a number in the range 0..7: 6
6. No way, do not proceed with that plan.
Part 2
Comment out the prompt. Instead now generate a single random number using rand() in the range 0..7. Display the corresponding message from the messages array, and the program ends.
Sample run:
Think of a question, then hit enter key to get a message.
2. Yes, go ahead and make that phone call.
Part 3
Add a loop to your program, to continue selecting and displaying more numbers and random
messages until option 6 gets randomly selected, at which point the program ends.
Sample run:
Hit enter key to get more messages.
5. Absolutely, go ahead.
0. Get ready, you are about to learn something new.
1. New friends are on their way.
1. New friends are on their way.
2. Yes, go ahead and make that phone call.
6. No way, do not proceed with that plan.
Explanation / Answer
// Note: There are no messages given for number 3, 4 and 7. Please replace the text for them.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main() {
//cout << "Welcome, let Magic 8-Ball show you the way! Think of a question, then enter a number in the range 0..7: ";
int number;
srand ( time(NULL) );
//cin >> number;
while(1)
{
number = rand() % 10 + 1;
if(number == 0)
cout << "0. Get ready, you are about to learn something new." << endl;
else if(number == 1)
cout << "1. New friends are on their way." << endl;
else if(number == 2)
cout << "2. Yes, go ahead and make that phone call." << endl;
else if(number == 3)
cout << "Put message for 3 here" << endl;
else if(number == 4)
cout << "Put message for 4 here" << endl;
else if(number == 5)
cout << "5. Absolutely, go ahead." << endl;
else if(number == 6)
cout << "6. No way, do not proceed with that plan." << endl;
else if(number == 7)
cout << "Put message for 7 here" << endl;
if(number == 6)
break;
}
}
/* OUTPUT
2. Yes, go ahead and make that phone call.
Put message for 4 here
5. Absolutely, go ahead.
5. Absolutely, go ahead.
5. Absolutely, go ahead.
Put message for 3 here
Put message for 7 here
6. No way, do not proceed with that plan.
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.