Problems with a (relatively) simple program

I have recently decided to get properly into c++, and my main method of learning so far has been problem solving.

My latest project is a would-be game of sorts, very alike to text adventures, but with a randomly generated environment.
The simplest way to approach this seems to be random generation of surroundings at every move. The game isn't supposed to make sense, so the complete random nature of what you'll encounter, even when going to areas already explored, isn't an issue.

What is, though, is the fact that the random generation simply doesn't work.
I must have some mental impairment related to using If statements, because I can`t get them to work for the life of me.

When the following program is run, it should pick one of the possible strings detailing the environment and go on to the next direction, but it instead lists all possible environments for every direction.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int x = 25, y = 25; //character position at start - middle
int up;
int down;
int l;
int r;
string desert = "a dry, dead desert. You can feel the heat emanating from the sand, and there is no water in sight. \n";
string valley = "a lush, green valley, with a few fresh ponds and the ocassional tree. Some critters are roaming about. \n";
string mountain = "a mighhty mountain, with a few icy peaks, and sharp rocks all around. There are a few patches of grass here and there, and a few rivers flowing nearby. \n";
string river = "a great river, storming through the ground at an alarming speed. There are a few fish in the water, though it does not look potable. \n";
string hill = "hills coverig the horizon, one after the other. There is a fresh-looking river running through them, and there bust be plenty of food within the neverending forests. \n";
string v = "a darkness you have never witnessed before in your life. There are a few chunks of a weird-looking material whih seem to be just floating in space. \n";
int main()
{srand(time(0));
up = rand() %5;
down = rand() %6;
l = rand() %5;
r = rand() %5;
if (up=0) {cout << "You see in front of you "<< desert;}
if (up=1) {cout << "You see in front of you "<< valley;}
if (up=2) {cout << "You see in front of you "<< mountain;}
if (up=3) {cout << "You see in front of you "<< river;}
if (up=4) {cout << "You see in front of you "<< hill;}
if (l=0) {cout << "You spot to your left "<< desert;}
if (l=1) {cout << "You spot to your left "<< valley;}
if (l=2) {cout << "You spot to your left "<< mountain;}
if (l=3) {cout << "You spot to your left "<< river;}
if (l=4) {cout << "You spot to your left "<< hill;}
if (r=0) {cout << "Looking to the right, you notice "<< desert;}
if (r=1) {cout << "Looking to the right, you notice "<< valley;}
if (r=2) {cout << "Looking to the right, you notice "<< mountain;}
if (r=3) {cout << "Looking to the right, you notice "<< river;}
if (r=4) {cout << "Looking to the right, you notice "<< hill;}
if (down=0) {cout << "Behind you there is "<< desert;}
if (down=1) {cout << "Behind you there is "<< valley;}
if (down=2) {cout << "Behind you there is "<< mountain;}
if (down=3) {cout << "Behind you there is "<< river;}
if (down=4) {cout << "Behind you there is "<< hill;}
if (down=5) {cout << "Behind you there is "<< v;}
}


If anybody will have the mercy to point out where my stupidity lies, it would be greatly appreciated.
if (up=0) = is assignment == is equality
Ah, well, I completely missed that. Thanks.
Not sure if you've previously taken a C++ class, course, etc. or are just teaching yourself as you go, but thought I would provide a tip. For every loop, if statement, comparison, etc. you will only need braces "{}" if the control statement has more than one line of code attributed to it. Now, be careful with this rule, because classes and functions always need braces. Do not get them confused with each other.

For example:

1
2
3
4
5
if (r = 4)
{
     cout << "Looking to the right, you notice "<< hill;
     cout << "You spot to your left " << river;
}

Also, here are some useful tutorials for you:
http://mrbool.com/importance-of-code-indentation/29079
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
For every loop, if statement, comparison, etc. you will only need braces "{}" if the control statement has more than one line of code attributed to it.


I do the opposite of that: I always put braces, even if it is one statement. This will save you one day, when you add more code.

This is an example of a defensive coding measure: not required by the language, but helps prevent one from messing up in the future.
On your random:
Since you need a range from 0-5 or 0-6 you'll need to do that like this:

1
2
3
4
up = 0 + rand() %5;
down =0 + rand() %6;
l = 0 + rand() %5;
r =0 + rand() %5;
Topic archived. No new replies allowed.