-
If command practice
This isnt much of a first post, but im learning and wanted to put together something off the top of my head a "small game"
The problem is that it allways gives me the command like if i pick the number 3, it gives me the words "Your too low, Congrats you win!!"
Am I doing else wrong, why does it display the win command when I didnt win. Also can anyone point me to a way to recycle it so if u dont get the number right it doesnt terminate.
Code:
#include <iostream>
using namespace std;
int main()
{
int number;
cout<<"Please input a number: \n";
cin>> number;
cin.ignore();
cout<<"You inputted: "<< number << "\n";
cin.ignore();
if ( number < 5 )
cout<< " You are too low\n";
else if ( number > 5 )
cout<< " You are too high\n";
else ( number == 5 );
cout<< " Congrats you win!!\n";
cin.get();
}
-
the problem is here
Code:
else ( number == 5 );
cout<< " Congrats you win!!\n";
change this to
Code:
else
cout<< " Congrats you win!!\n";
You don't need to specify a condition in an else statement, because else essentially means "do this if none of the previous conditions have been met". also, a semicolon at the end of an if or else statement will signal the end of that statement.
-
>Also can anyone point me to a way to recycle it so if u dont get the number right it doesnt terminate.
Code:
#include <iostream>
using namespace std;
int main()
{
int number;
do {
cout<<"Please input a number: " << endl;
cin>> number;
cin.ignore();
cout<<"You inputted: "<< number << "\n";
cin.ignore();
if ( number < 5 )
cout<< " You are too low\n";
else if ( number > 5 )
cout<< " You are too high\n";
} while (number != 5);
cout<< " Congrats you win!!\n";
cin.get();
}
Then you can add another loop to ask the player if they want to play again after they win.
-
-
thanks a bunch i got it :D
Edit: What I forgot to ask, is how I can make the number random instead of 5, and if anyone has any recommendations of game programing type tutorial sites or e-books to get started with.
-
>What I forgot to ask, is how I can make the number random instead of 5
You can use the rand function. Maybe something like:
Code:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
int number;
//Seed the pseudo random number generator. Do this only once.
srand(time(NULL));
//Generate a number between 1 and 100
int actual = 1 + (double) rand() * 100 / (RAND_MAX + 1);
do {
cout<<"Please input a number: " << endl;
cin>> number;
cin.ignore();
cout<<"You inputted: "<< number << "\n";
cin.ignore();
if ( number < actual )
cout<< " You are too low\n";
else if ( number > actual )
cout<< " You are too high\n";
} while (number != actual);
cout<< " Congrats you win!!\n";
cin.get();
}
See the FAQ for more information on using rand
-
Here's the FAQ entry (FAQ > How do I... (Level 1) > Generate random numbers?):
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
And here's Prelude's random number tutorial (FAQ > Prelude's Corner > Random Numbers):
http://faq.cprogramming.com/cgi-bin/...&id=1073086407
-
-
wow thanks! so many replies.
-
>And here's the updated and vastly improved versions:
I had a feeling you had made some changes to some of your tutorials, but was unable to get anyone here to insert them.
Also, I'm not sure I got the formula corrent on the code above to generate a number between 1 and 100, but hopefully it's close (didn't have time to test it).
-
>Also, I'm not sure I got the formula corrent on the code above to generate a
>number between 1 and 100, but hopefully it's close
Well, it works when RAND_MAX is less than INT_MAX. Otherwise you invoke undefined behavior with (RAND_MAX + 1). You can get around that by using 1.0 instead of 1, and that has the added benefit of forcing the entire expression to double so you can avoid the cast.
Of course, now you have potential overflow with (rand() * 100) when you ditch the cast. I would probably use rand as late in the expression as possible, which means dividing first and then multiplying the result. Then a cast to int to remove the warning couldn't hurt:
Code:
int actual = static_cast<int>(1 + rand() / (RAND_MAX + 1.0) * 100);
-
Main returns an int:
Code:
int main
{
return 0;
}
-
>Main returns an int:
Who posted otherwise?
-
It wasn't in his source, and nobody posted it.
-
>It wasn't in his source, and nobody posted it.
Every piece of code in this thread properly defines main as returning int.
-
>Main returns an int:
Are you in the right thread?
-
I made some minor changes to the example I posted (changed variable names, added an extra call to rand, since the first number is always low on my machine (probably due to the seed used for srand()).
Code:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
int guess;
//Seed the pseudo random number generator. Do this only once.
srand(time(NULL));
//Throw away first number generated (always low on my machine)
int number = rand();
//Generate a number between 1 and 100
number = static_cast<int>(1 + rand() / (RAND_MAX + 1.0) * 100);
do {
cout<<"Please input a number: " << flush;
cin>> guess;
cin.ignore();
cout<<"You inputted: "<< guess << "\n";
cin.ignore();
if ( guess < number )
cout<< " You are too low\n";
else if ( guess > number )
cout<< " You are too high\n";
} while (guess != number);
cout<< " Congrats you win!!\n";
cin.get();
}
-
>> Main returns an int
Perhaps you are referring to the lack of a return 0? That is not necessary for the main function. Zero is returned automatically if the function ends without a return statement.
-
>Perhaps you are referring to the lack of a return 0?
I hadn't thought of that, I bet that's to what Ideswa indeed was referrring. I normally add the return 0; also just for consistency.
-
>Perhaps you are referring to the lack of a return 0?
I was trying to get him to say it explicitly. Vague good advice is no better than bad advice. ;)
-
Yea thanks for the catch, i forget to return, but i should just start doing it and remembering it. Also anyone have any sites or tutorials on small games that are used in examples. Most books like my textbook C++ learning by deitel, gives me some stupid gradebook example which I hate.