If statement overusage?
Hi, I'm a n00b and have been doing the tutorials and I have many questions and you'll rpbably find me annoying because I really ask alot of questions daily. At the moment I have 3, I'm just gonna quote them cause I posted them on a pretty much dead board.
I was making a rock paper scissors game and suddenly realised I had about 15 if statements and realised that this can't be the way your meant to do it. Is there another way to do it? or is it all done with if and else statements?
When I was making all those if statements I wondered how you close a program? I know that it stops when you get to the bottom but how do you tell it to close after the users presses enter at a certain point, say in a if statment?
Someone told me that this is how you make x a random number srand(time(0));
int x = rand();
But I have no idea where I'm mean to put it? Where do I put it?
By the way, I'm up to lesson 9 on the tutorials if that means anything?
>this can't be the way your meant to do it
Well, there's nothing wrong with it, and by definition the game will require several choices.
>how do you tell it to close after the users presses enter at a certain point, say in a if statment?
If you want to return from the current function, use a return statement:
If you want to terminate the program and you're in main, you can use the exit function from <cstdlib>:
>Where do I put it?
srand is called once, as soon as you can manage it. For example:
rand is called whenever you need a random number after that.
// The rest of your program here
if what you compare in those if-statements are integertype values you can use switch and case instead. Example of this:
What is meant with fall through:
// do something
break; // This is needed otherwise it will "fall through", explenation below.
// do something else
Consider this code:
If you in this piece of code input 1 output will be:
std::cin >> a;
std::cout << "a = 1" << std::endl;
std::cout << "a = 2" << std::endl;
// notice: no break statement here.
std::cout << "a = 0" << std::endl;
std::cout << "Default action taken." << std::endl;
a = 1
If you input 2 ouput will be:
a = 2
a = 0
If you input something else output will be:
Default action taken.
Basicly the program will just start at the first case that matches and then it will go through all the other cases (even if they arent correct) and do what is in those until it hits a break or until no more cases are given.
1) If you're testing the same integerer value a lot, you can use a siwtch / case statement. Chars can also be tested. Other than that, if / else statements are fine.
2) If you're in your main function, just return a value (make sure you're using int main and not void main) and that'll be it.
3) Look in the FAQ, it shows you how to do random numbers. You need to seed the generator once before you assign random numbers. AS for WHEN you use rand(), just the same place as you would if you assigning some other value to it. Also make sure you do it after you use srand(). See the FAQ...
Ok, I think I got it all. Thanks! :D