Answer to Question #100805 in C++ for zahra

Question #100805
Write a code that will calculate randomly a number between 1 and 10 (without displaying it) and
will ask the user to guess it. If the user answers correctly,the program prints“Good” and add 1 to his score,if not,the program prints“sorry”,gives the correct answer,then selects another number.
After 10 consecutive games, the programs stops and prints the final score. Hint: use a random builtin function (rand) and write the #include <stdlib.h> at the beginning.
1
Expert's answer
2019-12-27T09:35:18-0500

#include <stdlib.h>

#include <iostream>


using namespace std;


int main()

{

int score = 0;

int user_num, num;

srand(time(0));


for (int i=0; i<10; i++) {

num = random() % 10 + 1;

cout << "Guess number: ";

cin >> user_num;

if (user_num == num) {

cout << "Good" << endl;

score++;

}

else {

cout << "sorry, correct answer is " << num << endl;

}

}


cout << "Final score: " << score << endl;


return 0;

}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS