#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main(){
srand((unsigned)time(0));
int random_integer;
for(int index=0; index<20; index++){
random_integer = (rand()%6)+1;
}
int move;
cout << "Would you like to go left, right, up, or down?(l,r,u,d)";
cin >> move;
if (move == 'l'){
if (random_integer > 4){
cout << "You got a apple";
}
else{
cout << "You found nothing";
}
}
if (move == 'r'){
if (random_integer > 4){
cout << "You got a apple";
}
else{
cout << "You found nothing";
}
}
if (move == 'u'){
if (random_integer > 4){
cout << "You got a apple";
}
else{
cout << "You found nothing";
}
}
if (move == 'd'){
if (random_integer > 4){
cout << "You got a apple";
}
else{
cout << "You found nothing";
}
}
return 0;
}
Im a self taught programmer so this code isnt made well. Im compiling it on ubuntu with gcc with this command g++ game.cpp -o apples. I call it apples because your to press l,r,u,d and if random_interger > 4 then you get a apple. When I run it it shows a line i put in l,r,u, or d and then it exits. Help?
Maybe I should be clearer. When I compile and run this on ubuntu with gcc it makes the compiled file. I then run it in terminal and then It shows the first line "Where would you like to move?(l, r, u, d)" I input one of those 4 letters and then press enter and it ends. Can someone tell me what im doing wrong?
Line 14 you said to output that text
Line 15 you said to read the user's input
Line 16 you said to do lines 18 and 19 only if the input was l, r, u, or d.
Line 21 says to return 0 from main(), which returning from main() causes the program to end.