C++ Help Why wont it print anything (functions)
\\this program is used for the user enter a number and guess the correct number where the int jackpot is the function and analyzes it.It debugs and all but it wont work help please.Im new to programming.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
using std::srand;
using std::time;
using std::rand;
int jackpot(int a){
srand((unsigned int)time(0));
int loop;
int rand1 =rand() % 100 +1;
for(loop=0;a==rand1;loop++){
cout<<"Enter a number:";
cin>>a;
if(a<rand1)
{cout<<"Its too low ";
}
else if(a>rand1){cout<<"Its to high";
}}
cout<<"You have tried"<<loop<<"\n";
cout<<"Congratulations";
}
int main ()
{
cout<<"Hello,Please enter your 3 bets\n";
int guess1;
cout<<"Please enter your 1st bet:\n";
cin>>guess1;
int jackpot(guess1);
return 0;
}
Re: C++ Help Why wont it print anything (functions)
Doesn't compile for me. jackpot needs to return a value.
Please use code tags.
Why are you prompting for a guess in main, passing that to jackpot, then asking again in jackpot and ignoring the value you got in main?
Your loop condition is incorrect. The loop will only execute while a == rand1. Typically you'll use a for loop for a known number of iterations. In your case, a while loop would be better.
Re: C++ Help Why wont it print anything (functions)
I found the problem by myself thanks anyways!
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
using std::srand;
using std::time;
using std::rand;
int jackpot(int a){
srand((unsigned int)time(0));
int loop;
int rand1 =rand() % 100 +1;
for(loop=0;a!=rand1;loop++){
cout<<"Enter a number:";
cin>>a;
if(a<rand1)
{cout<<"Its too low ";
}
else if(a>rand1){cout<<"Its to high";
}}
cout<<"You have tried"<<loop<<"times\n";
cout<<"Congratulations\n\n";
return a;
}
int main ()
{
cout<<"Hello,Please enter your 3 bets\n";
int guess1=0;
cout<<"Please enter your 1st bet:\n";
cin>>guess1;
cout<<jackpot(guess1)<<"\n";
}
Re: C++ Help Why wont it print anything (functions)
You still have the other problems I mentioned.
You're prompting for a number in main and not using it, and a while loop is the loop to use here. This isn't a case for a for loop.
Re: C++ Help Why wont it print anything (functions)
Quote:
You're prompting for a number in main and not using it
It is being used for the first condition test of the for loop.
Code:
if(a<rand1)
{cout<<"Its too low ";
}
else if(a>rand1){cout<<"Its to high";
}
This code is only executed if a is not equal rand1. If a is not equal, then it must either be less than or greater than. As the first test is for less than, the second test for greater than is not needed.
Code:
if (a < rand1)
cout << "Its too low";
else
cout << "Its too high";
srand() is used to seed the random number generator is is usually called just once at the start of a program in main().