#include <iostream>
#include <stdlib.h>
using namespace std;
void Guess(int Answer,int guess)
{
if(guess!=Answer)
{
if(guess<Answer && guess!=Answer)
{
cout<<"The number is higher, try again"<<endl;
cin>>guess;
}
else
if(guess>Answer && guess!=Answer)
{
cout<<"The number is lower, try again"<<endl;
cin>>guess;
}
}
}
int main()
{
int Answer,guess;
Answer=rand()%100;
cout<<Answer<<endl;
cout<<"Guess the number from 1 to 100"<<endl;
cin>>guess;
while(Answer!=guess)
{
Guess(Answer,guess);
if(Answer==guess)
break;
}
if(guess==Answer)
cout<<endl<<"Congratulations, you guessed the number, it was "<<Answer<<" !";
}
So, this is the code I made after I learned some basic functions, you can probably tell that it is supposed to generate a random number, and then you have to guess what the number is, but I have a few problems:
it generates the same random number
If you don't guess it right the first time, the code keeps going forever even if at some point u guess the number.
If you guess a higher number, let's say 50 and the answer if 40, even if u then guess 30 it still says the number is lower when it's supposed to say higher.
What did I do wrong?
Changed void Guess(int Answer,int guess) to void Guess(int& Answer,int& guess) and added srand ( time(NULL) ); at the start of int main to prevent the same number to be generated
Related
So, I have to write a program from a random guessing game. The program needs to ask the player to guess a number between 1-100. At least one function must be used. It needs to tell the player if they are too low/high, ask them to try again, or if they guess it, ask them to play again.
I have a few errors that I can not figure out.
44: error: ‘int winlose’ redeclared as different kind of symbol
9: error: previous declaration of ‘int winlose(int)’
44: error: ‘g’ was not declared in this scope
Code
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int winlose(int);
int main()
{
int g, n, x;
while (x!=0)
{
do
{
srand(time(NULL));
n = 1 + rand()%100;
cout<<"Welcome to the guessing game. I have a number between 1-100.
Can you guess it?"<<endl;
cout<<"Please enter your guess"<<endl;
cin>>g;
winlose(g);
} while (n!=0);
cout<<"Play again? Enter 0 for no, any other number for yes."<<endl;
cin>>x;
}
return 0;
}
int winlose(g)
{
if (g<n)
{
cout<<"Your guess is too low. Try again."<<endl;
cin>>g;
}
else if (g>n)
{
cout<<"Your guess is too high. Try again."<<endl;
cin>>g;
}
else (g=n)
{
cout<<"Congrats! You win. The number was "<<n<<endl;
n=0;
}
return g;
return n;
}
You made a few mistakes in addition to the function declaration.
Function declarations must contain the type of each parameter, so the correct way is:
int winlose(int g);
You cannot use a condition in an else statement: (else (g=n)). An else statement is a catch-all, if none of the previous conditions (the ones in the if and else if()'s) were met. If you only want this to trigger on a specific condition, use another else if(). You are not required to have an else at the end of every if statement; it is perfectly correct to end with an else if(){...}.
You also need to compare with '==' rather than '='. = is the assignment operator and g=n will set the value of g to n. If you want to check if g is equal to n, you will have to use g==n
You should call srand() in the outer loop, otherwise after every guess, value changes.
The rest is corrected and sometimes slightly changed for correct performance:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
bool winlose(int number, int givenNumber);
int main(){
int g, n, x;
bool guessed;
do {
srand(time(NULL));
n = 1 + rand()%100;
cout<<"Welcome to the guessing game. I have a number between 1-100. Can you guess it?"<<endl;
do {
cout<<"Please enter your guess"<<endl;
cin>>g;
guessed = winlose(g, n);
} while (!guessed);
cout<<"Play again? Enter 0 for no, any other number for yes."<<endl;
cin>>x;
} while (x!=0);
return 0;
}
bool winlose(int g, int n) {
if (g<n) {
cout<<"Your guess is too low. Try again."<<endl;
return false;
}
else if (g>n) {
cout<<"Your guess is too high. Try again."<<endl;
return false;
}
else {
cout<<"Congrats! You win. The number was "<<n<<endl;
return true;
}
}
I need to make a simulator for a college homework. In this simulator there are 3 computers, 2 of which send messages to computer 1 which then decides to either send the message or reject it. The rejection is random chance with 20% of rejection on computer 2 and 50% on computer 3. I use the rand()%100+1 function with the srand(time(NULL)) seed. It makes a random number however I need to run this multiple times and every time the same random number is used. For example if I run the simulation 12 times and the number generated is 45, 45 is used 12 times. I've both placed the random number generator inside the code and made a function outside.
How can you make a random number generator that generates a random number every time?
#include <iostream>
#include <new>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
int randomgen()
{
int rndnum=0;
srand (time(NULL));
rndnum=rand()%100+1;
cout<< rndnum<<endl;
return rndnum;
}
struct comp1
{
int rejected=0;
int received=0;
int sent=0;
int onhold=0;
bool comp2reception()
{
received++;
bool result=false;
int rndnum=0;
srand (time(NULL));
rndnum=rand()%100+1;
if(rndnum<=20)
{
rejected++;
result=false;
}
if(rndnum>=21)
{
onhold++;
result=true;
}
return result;
}
bool comp3reception()
{
received++;
bool result=false;
int rndnum=randomgen;
if(rndnum<=50)
{
rejected++;
result=false;
}
if(rndnum>=51)
{
onhold++;
result=true;
}
return result;
}
};
Use the C++11 header <random>.
#include <random>
static std::random_device rd;
static std::mt19937 gen(rd());
int randomgen()
{
std::uniform_int_distribution<> dis(0,100);
int rndnum = dis(gen);
std::cout << rndnum << std::endl;
return rndnum;
}
Call srand(time(NULL)) just once at the beginning of the program.
Since time() return number of seconds from 1970 and you program probably takes less than that to finish, you esentially reset the generator with same value before each call. Which of course returns same (first) value each call.
I'm a begineer in C++ programming but I know the basics.
I recently started writing a simple game. The program chooses a random number
(1-100) and you have to guess it. There are 2 modes:
Normal - whenever you enter a number program tells you if it's bigger than the random or smaller.
hard - no clues, just pure luck.
Everything was running ok but when I added some fixes to the displayed text the program won't compile. I use CODE::BLOCKS.
Screenshot: http://scr.hu/81tw/m6cm0
I really apreciate your help.
Full code below:
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int number_normal;
int number_hard;
int guess_normal;
int guess_hard;
int tries_normal=0;
int tries_hard=0;
int mode;
int main()
{
{
cout<<"Choose your mode..."<<endl;
cout<<"Normal (Press 1) or Hard (Press 2)"<<endl;
cin>>mode;
if(mode=1)
cout<<"Normal mode chosen."<<endl;
goto normal;
if(mode=2)
cout<<"Hard mode chosen!"<<endl;
goto hard;
return 0;
}
{
hard:
cout<<"I chose a random number in a range from 1 to 100, can you guess it?"<<endl;
srand(time(NULL));
number_hard = rand()%100+1;
while(guess_hard!=number_hard)
tries_hard++;
cout<<"Enter your guess!(Try "<<tries_hard<<"): ";
cin>>guess_hard;
if(guess_hard=number_normal)
cout<<"Respect! You guessed it in "<<tries_hard<<" tries!"<<endl;
}
{
normal:
cout<<"I chose a random number from 1 to 100. I will give you some clues! Try to guess it."<<endl;
srand(time(NULL));
number_normal = rand()%100+1;
while(guess_normal!=number_normal)
tries_normal++;
cout<<"Enter your guess!(Try "<<tries_normal<<"): ";
cin>>guess_normal;
if(guess_normal==number_normal)
cout<<"Congrats! You're lucky. (Won in "<<tries_normal<<" tries!)"<<endl;
if(guess_normal<number_normal)
cout<<"Too low."<<endl;
else if(guess_normal>number_normal)
cout<<"That's too much!"<<endl;
system("pause");
return 0;
}
The code that works is here
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int number_normal;
int number_hard;
int guess_normal;
int guess_hard;
int tries_normal=0;
int tries_hard=0;
int mode;
int main()
{
{
cout<<"Choose your mode..."<<endl;
cout<<"Normal (Press 1) or Hard (Press 2)"<<endl;
cin>>mode;
if(mode=1)
cout<<"Normal mode chosen."<<endl;
goto normal;
if(mode=2)
cout<<"Hard mode chosen!"<<endl;
goto hard;
return 0;
}
{
hard:
cout<<"I chose a random number in a range from 1 to 100, can you guess it?"<<endl;
srand(time(NULL));
number_hard = rand()%100+1;
while(guess_hard!=number_hard)
{
tries_hard++;
cout<<"Enter your guess!(Try "<<tries_hard<<"): ";
cin>>guess_hard;
if(guess_hard==number_normal)
cout<<"Respect! You guessed it in "<<tries_hard<<" tries!"<<endl;
}
}
{
normal:
cout<<"I chose a random number from 1 to 100. I will give you some clues! Try to guess it."<<endl;
srand(time(NULL));
number_normal = rand()%100+1;
while(guess_normal!=number_normal)
{
tries_normal++;
cout<<"Enter your guess!(Try "<<tries_normal<<"): ";
cin>>guess_normal;
if(guess_normal==number_normal)
cout<<"Congrats! You're lucky. (Won in "<<tries_normal<<" tries!)"<<endl;
if(guess_normal<number_normal)
cout<<"Too low."<<endl;
else if(guess_normal>number_normal)
cout<<"That's too much!"<<endl;
//system("pause");
}
}
return 0;
}
Well, you didn't used braces across your while loops, and as #cowls suggested, there was a closing brace missing after main. Everything else was fine. Also you used a = for comparison between to variables, instead of ==, = is a assignment operator, while == is used for comparison.
You are missing a close brace at the end } that will close your main method.
I would also pay attention to the comments against the question addressing the other issues in your code.
Note: this could not be seen from the screenshot, showing why that was a bad format to post your code in.
I used the following code for solving problem#14 but for some strange reason it gives no output.Maybe its taking too long to run???
P.S.I know that max is not supposed to be the answer but still there is no output anyways whereas for smaller values like i<100 I get the output.
#include <iostream>
long collatz(long);
int main()
{
using namespace std;
long i=3,max;
for(i=3;i<1000000;i++)
{
max=collatz(i-1);
if(collatz(i)>collatz(i-1))
{
max=collatz(i);
}
else
{
max=collatz(i-1);
}
}
cout<<max<<endl;
cin.clear();
cin.get();
}
long collatz(long n)
{
int count=0;
while(n!=1)
{
if(n%2==0)
{
n=n/2;
count+=1;
}
else
{
n=3*n+1;
}
}
return count;
}
If you call collatz with n = 113383, you get overflow and n becomes negative from which it never recovers. So you have an infinite loop as it will never be 1. You need to use a long long inside collatz.
However, your collatz functions has other problems as pointed out by others. Also, your logic for the loop in main is not correct. You are resetting max each time through the loop. So, the result you report would be either collatz(999999) or collatz(999998). But that is not be the correct answer.
I'm creating a BlackJack Program. I want the cards to be randomly dealt. if i use the rand() function and initialize srand() to time(NULL) then all the cards turn out to be same. Any idea what the problem might be ?
//Cards.h
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
class Cards
{
private:
int val_seed,suit_seed;
public:
int val[10];
string card[10];
int dealcard(int x)
{
srand();
val_seed=rand()%13 + 2;
suit_seed=rand()%4 + 1;
if(val_seed>=1 && val_seed<=10)
{
if(val_seed==10)
{
card[x]="10";
}
else
{
card[x]=val_seed+48;
}
}
else if(val_seed>10)
{
switch(val_seed)
{
case 11: card[x]="J"; val_seed=10; break;
case 12: card[x]="Q"; val_seed=10; break;
case 13: card[x]="K"; val_seed=10; break;
case 14: card[x]="A"; val_seed=11; break;
}
}
switch(suit_seed)
{
case 1: card[x]+='\3'; break;
case 2: card[x]+='\4'; break;
case 3: card[x]+='\5'; break;
case 4: card[x]+='\6'; break;
}
val[x]=val_seed;
}
};
//main.cpp
#include <cstdlib>
#include <iostream>
#include "Cards.h"
using namespace std;
int main(int argc, char *argv[])
{
Cards k;
for(int a=1; a<=9; a++)
{
k.dealcard(a);
}
for(int e=1; e<=9; e++)
{
cout<<k.card[e]<<"("<<k.val[e]<<")"<<" ";
}
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Seed the number generator ONCE. If you run your function in a loop they will be executed faster than the time resolution interval (so all the seeds are the same and therefore the numbers).
While developing the app and debugging it it will help if you seed the generator with the same, known value. This means successive debugging sessions will use the same values from the generator.
Don't forget to switch back to a time() based seed in your release build (or use the pre-processor to detect if it's a Debug or Release build and do it for you).
See http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
srand is best initialised with a unique value, eg, the time, as computers dont generate random numbers but in effect work from a predone list, this sets the start point to be less determinable.
You do not want to call srand every time you use rand, just call it once at the start of the program and you will be fine.
Calling it with the time as parameter will also make sure you will get a different random sequence each time you run the program.
Problems using srand() notwithstanding, your card generation algorithm is wrong. Even the possibility of it generating two of the same card in a row means it is wrong.
You should take a deck of cards (52 or some multiple thereof), shuffle it using a randomly generated seed, and deal cards from the shuffled deck.
You are seeding srand() with null; seed with current clock time and then retry it should solve your problem.
time_t now;
time(&now);
srand((unsigned int)now);