I'm writing a simple High-Low Game for my Fundamentals of Programming I class but in Visual Studios it keeps saying that my two different functions are undeclared I don't know where the problem is could you help? This is what I have so far:
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>
using namespace std;
int main();
int rndmNum();
int rndmNum() {
int num, n;
srand(time(0));
num = rand() % 100 + 1;
return num;
}
int userGuess(int num) {
int n, num, i;
for (int i = 0;i < 10;i++)
{
cout << "Enter a number
between 1 to 100:";
cin >> n;
if (n < num)
cout <<
"it''s too low\n";
else if (n > num)
cout <<
"it's too high\n";
else
cout <<
"Guessed number is correct";
break;
}
}
int main() {
int rndmNum;
int userGuess;
return 0;
}
There were some syntax errors as well as some logical mistakes in the code, here is the fixed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main();
int rndmNum();
//method that generates and returns a random number between 1 and 100
int rndmNum()
{
int num, n;
num = rand() % 100 + 1;
return num;
}
//this method takes a num, loops for maximum 10 times, read user input to see if it is equal to num
//returns the number of guesses it took for the user to guess the answer correctly.
//returns -1 if user is unable to guess it even after 10 attempts
int userGuess(int num)
{
int n, i;
//looping for 10 times
for (int i = 1; i <= 10; i++) {
cout << "Enter a number between 1 to 100: ";
cin >> n;
if (n < num)
cout << "it''s too low\n";
else if (n > num)
cout << "it's too high\n";
else {
//user guess is correct
cout << "Guessed number is correct" << endl;
return i; //returning the number of attempts took
}
}
return -1; //user could not guess in 10 attempts
}
int main()
{
//seeding random number generator. this should be done ONLY once.
srand(time(0));
//generating a random number, storing it in rand_number, previously you were trying to
//initialize a number rndmNum, it can't be allowed as it has the same name as rndmNum method
int rand_number = rndmNum();
//calling userGuess method, getting the number of attempts it took
int counts = userGuess(rand_number);
//if counts is -1, user is unable to guess
if (counts == -1) {
cout << "Sorry! You could not win. The number was: " << rand_number << endl;
}
//otherwise displaying the attempts took
else {
cout << "You guessed it in " << counts << " attempts" << endl;
}
return 0;
}
I'm writing a simple High-Low Game for my Fundamentals of Programming I class but in Visual...
Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN = 1; const int MAX = 10; int getRandom(int low, int high); int main() { int random_num = 0; int player_num; int tries; int seed = static_cast<int>(time(0)); bool guessed = false; srand(seed); // call the getRandom function below tries = 4; while ( tries > 0 && !guessed ) { cout << "Enter a number within the range 1 to...
Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...
#include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....
I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() { srand(time(0)); int guess,a,ans,b, k;...
Can someone help me make the output of this code more spaced out? #include<iostream> #include<cstdlib> using namespace std; int main() { int amount=1000,guess,answer,bet,count=0,win=0; float per; char ch; cout<<"Welcome to the high-low betting game.\nYou have $1000 to begin the game.\nValid guesses are numbers between 1 and 100.\n"; do { cout<<"Please enter a bet:\n"; cin>>bet; answer=rand()%100; for (int i=0;i<6;i++) { cout<<"Guess "<<i+1<<":"; ...
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...
i have created a program for a game of rock, paper, scissors but i must make it run more than once in some kind of loop. i want to add a function named runGame that will control the flow of a single game. The main function will be used to determine which game mode to initiate or exit program in Player vs. Computer, the user will be asked to enter their name and specify the number of rounds for this...
Please do a flowchart which will be on the computer not on paper so i can read it and be able to take screenshot for this c++ code this code for MasterMind //System Libraries #include <iostream> //Input/Output Library #include <cstdlib> #include <ctime> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //main int main(int argc, char** argv) { //Set the random number seed srand(time(0)); int randomint = (rand()%5)+1;...
Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...
Getting this error. [Error] expected ';' after class definition #include <iostream> #include <cstdlib> #include <ctime> #include <conio.h> using namespace std; int sum=0; class Coin { int sideUp; public: Coin(int value) { toss(value); }; void toss(int value); } void Coin::toss(int value) { if(rand() %100<=50) { sum+=value; cout<<"\n You got a head. Value "<<value<<" added!!"; } else { cout<<"\n You got a tail. No value added"; } } int main() { int temp; cout<<"\n Quarter = 25 cents\n Dime = 10 cents...