Here is my text based world generator system.

Is this hard-coding?

[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]

as you could probaley tell its not done yet.
Last edited on
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?
It would still work the same. Here is an example:
1
2
3
4
5
6
7
8
9
10
11
int ret = 1+rand() % 10;

switch(ret){
  case 1:
    cout << "You awake in a humid forest.\nYou see...";
    system("pause");
    break;
  case 2:
  case 3:
ect..
}
yes i get that, but why do it as a switch? Also, i'm looking for a decent 'cheap' programming laptop. any suggestions?
You'd do it as a switch because it's less code to type and more concise.
closed account (Dy7SLyTq)
i like my dell latitude
lookup table.

1
2
3
4
5
6
7
8
9
10
const char* 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.
Ok, i still have questions which ill try and figure out but do i put if statements in there if so how?
@CrazyKane:
do i put if statements in there if so how?


if you're talking about the example Disch provided, then no, this method doesn't require any if, it is also the good way of going about this problem.

for info on using code tags, check this:

http://www.cplusplus.com/articles/jEywvCM9/
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.
Ive been trying to learn more about things of that sort and im just now getting into classes and such.
Topic archived. No new replies allowed.