[code]
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int r;
int main(){
srand(time(0)); /// Random number generator based on the cpu's time.
r = 1+rand()% 10; /// It can only go up to ten, At the moment.
if(r == 1){
cout << "You awake in a humid forest.\nYou see two wolves fighting over a piece of meat,\nthey look angry.\nYou know that wolves may attack if you make sudden movements.\nYou slowly arise to see both of the wolves are staring at you,\ntheir eyes burning holes through your skull.\n" << endl;
system("pause");
}
if(r == 2){
cout << "'Pull over NOW!' you hear through the megaphone out of the police officers car.\nYou look down to see you are going 95 mph.\nPffff, you hear the tires of your brand new camero pump from turning sharply onto duglas ave.\nYou look behind you to see if the officer is still back there.\nHe isn't.\n" << endl;
system("pause");
}
if(r == 3){
cout << "'Honey wake up!' your husband is yell at you to get up.\nYou get up and look out the window with him,\n'whelp this is the end isn't it?'\nYou respond with a slightly loader than you wanted sigh.\n'The news channel says that people are going insane and kill others'.\nYou slowly put some clothes on and walk down the stairs into your newly redesigned kitchen.\n'too bad it had to go to waste to install the sucker, that was loads of work'\n" << endl;
system("pause");
}
if(r == 4){
cout << "'Boom... Boom' The music surrounds your ears in the most magnificent way.\nYou don't know why but you feel like youre,\n swimming in a pool of the purest of water.\n'Crack' you think you heard a gun go off but you don't know for sure.\n'Crack... Crack, Crack' Youre sure its a gun,\nYou start sprint for the concert doors.\nYou think to yourself as you run 'Wow, I need to lay off some of those tacos'.\n" << endl;
system("pause");
}
if(r == 5){
cout << "5" << endl;
system("pause");
}
if(r == 6){
cout << "6" << endl;
system("pause");
}
if(r == 7){
cout << "7" << endl;
system("pause");
}
if(r == 8){
cout << "8" << endl;
system("pause");
}
if(r == 9){
cout << "9" << endl;
system("pause");
}
if(r == 10){
cout << "10" << endl;
system("pause");
}
return 0;
}
[end code]
I'm new to c++ but I would prefer switch statements and also allow them to choose which story they would like to go with by getting the input from them and I would make the variable local instead of global(unless you are using it somewhere else).
well, i would use switch statements but i want it to be different every time they play. Also, i'm looking for a decent 'cheap' programming laptop. any suggestions?
constchar* messages[10] = {
"You awake in a humid forest...",
"Whatever string #2",
"Whatever string 3",
"etc"
};
int ret = rand() % 10; // ditch the +1 garbage. Get used to 0-based numbers
cout << messages[ret];
Generally the more you can separate code from data, the better. Here, your code does one thing: it prints text. The text it prints is the data.
Each of your if statements does not have different code, it only has different data. Therefore it should all use the same code and reference different data.
Not only does this simplify the code, it also makes it much easier to make changes and additions.
Hard coding is fine, but that sort of thing is better to delegate to a scripting language. Though, I don't know how comfortable you are with that sort of thing, so I'd say stick to hardcode, but slowly start learning and using scripting languages where possible.