Question

please give an answer in c ++. Question 3: assign a secret value in your program...

please give an answer in c
++.
Question 3:

assign a secret value in your program and ask the user to enter a guess. 
if the guess is greater than the secret value, print too large;
if the guess is less than the secret value, print too less;
if the guess is same as the secret value, print you found it. 
the user has unlimited attempts or terminated by entering a non-number.

Question 4:

use rand() to simulate a double-dice game (you play two die at the same time). 
You vs Computer. You roll a number and computer will roll one as well(automatically). 
If your number is greater or equal than the Computer's, you win. 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have given an explanation in comments.

Q-3) Code:-

//code starts here

#include<iostream>
#include <stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int n,flag=1,number;
srand(time(0));
//assign random number so every time new number
//will be guessed
//only number between 1 to 10 will be assigned
n=rand()%10+1;
cout<<"Guess the number: ";
while(flag)
{
char x[100];
int flagdig=1;
cin>>x;
//check if whether user entered non-number or not
for (int i=0; x[i]!= '\0'; i++)
{
if (isdigit(x[i])==0)
flagdig=0;
}
if (flagdig==1){
//atoi will covert char to number
number=atoi(x);
if(n==number){
//if guess is right then flag will change and break
cout<<"You corrected right number\n";
flag=0;
}
else if(n<number){
cout<<"\nToo large\n";
cout<<"Tray again\n";
cout<<"\nGuess the number again: ";
}
else{
cout<<"\nToo Small\n";
cout<<"Tray again\n";
cout<<"\nGuess the number again: ";
}
}
}
return 0;
}

//code ends here

Scrrenshot of Code:-


Screenshot of output:-

Q-4) Code:-

//code starts here

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int user_dice1,user_dice2;
int comp_dice1,comp_dice2;
while(1)
{
//for every time random number is generated
srand(time(0));
string x;
cout<<"\nEnter y to roll the dice n to quit : ";
cin>>x;
//if user entered n then will exit from game
if(x=="n") break;

//Every dice will be given random number every-time
int user_dice1=rand()%6+1;
int user_dice2=rand()%6+1;
int comp_dice1=rand()%6+1;
int comp_dice2=rand()%6+1;

cout<<"You got "<<user_dice1<<" & "<<user_dice2;
cout<<"\nComputer got "<<comp_dice1<<" & "<<comp_dice2;
//print accordingly who won
if(user_dice1+user_dice2>=comp_dice1+comp_dice2)
cout<<"\nYou won\n";
else if(user_dice1+user_dice2<comp_dice1+comp_dice2)
cout<<"\nComputer won\n";
}

cout<<"\nThank you for playing\n";
return 0;
}

//code ends here
Screenshot of Code:-

Screenshot of output:-

I hope it helped you,

Please do like...

Add a comment
Know the answer?
Add Answer to:
please give an answer in c ++. Question 3: assign a secret value in your program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then...

    please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then write each user guess and answer to the file. (on separate lines) Write the number of guesses to the file at the end of the game. After the game is finished, ask the user if they want to play again. If 'n' or 'N' don't play again, otherwise play again! NOTE: your file should have more than one game in it if the user...

  • python question Question 1 Write a Python program to play two games. The program should be...

    python question Question 1 Write a Python program to play two games. The program should be menu driven and should use different functions to play each game. Call the file containing your program fun games.py. Your program should include the following functions: • function guess The Number which implements the number guessing game. This game is played against the computer and outputs the result of the game (win/lose) as well as number of guesses the user made to during the...

  • in C++ This assignment is to use your knowledge of Chap 1-5 to write a simulation...

    in C++ This assignment is to use your knowledge of Chap 1-5 to write a simulation program to estimate the winning chance of the Craps game. Craps is a popular dice game played in casinos. Here is how to play: Roll two dice. Each die has 6 faces representing values 1,2,3,4,5,and 6, respectively. Check the sum of 2 dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural),...

  • Use C++ 11 to write the program War Game Requirement Setting This is a 2-player game....

    Use C++ 11 to write the program War Game Requirement Setting This is a 2-player game. It is played through dice. Rule for scoring The player who rolls higher number gets one point. If both players roll the same number, it is considered a draw and no one gets a point. Dice Specification There are two kinds of dice: normal die, represented by Die class. loaded die, represented by the loadedDie class. Classes Die class Die class has a member...

  • Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will...

    Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will pick a move (Rock Paper Scissors) from 3 radio buttons and click the play button to play the game. The application will use random number generator to pick a move for the computer. Then, the application display the computer's move and also display whether you won or lost the game, or the game is tie if both moves are the same. The application must...

  • 15. Your friend challenges you to a game. She says that she is going to roll...

    15. Your friend challenges you to a game. She says that she is going to roll 2 6-sided dice. If the first die rolls at least 5, you win if the second die rolls less than 5. If the first die rolls less than 5, you win if the second die rolls at least 5. This seems fair. Is it? (a) What is the probability that you win this game? To incentivize you, your friend says that she will pay...

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the...

    Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the user to guess the number. If the user guesses the number correctly, the program outputs an appropriate message such as "You win!" and terminates. Otherwise, the program checks and tell the user whether the guessed number is less or greater than the target number and then prompts him for another try. However, the program gives the user five tries only to guess the correct...

  • Complete each problem separately and perform in python. 1. Create a script that will roll five...

    Complete each problem separately and perform in python. 1. Create a script that will roll five dice and print out their values. Allow the user to roll the dice three times. Prompt them to hit enter to roll. Ex: Hit enter to roll 1,4,3,6,6 Hit enter to roll 3,5,1,2,1 Hit enter to roll 4,3,4,2,6 2. Write a script that will roll five dice (just one time). The user's score will be the sum of the values on the dice. Print...

  • using python in eclipses Question I am attempting to solve My attempt... 1. Add a new...

    using python in eclipses Question I am attempting to solve My attempt... 1. Add a new module named twenty_questions_class to the package library. 2. Define a class named TwentyQuestions in the module with an instance constructor and a method. a. The instance constructor _init is used to create an instance of the TwentyQuestions class with a value for the RANGE. b. The method named play lets the user play the game. For example, I am thinking of a secret number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT