Reputation: 15
I just started learning c++ and I'm trying to make a dice game where the user puts in a number between 1 and 6 and then the code prints a random number within that range and if y and z are the same you win.
This is the code I have but when I put in a number that isn't in the array it works as if it were in the array.
#include <iostream>
#include <stdlib.h>
#include <time.h>
int main() {
for (; ; ) {
int x[6] = { 1,2,3,4,5,6 }; //dice numbers
int y; //user choice
int z; // random number generated
std::cout << "Pick a number between 1 and 6\n";
std::cin >> y;
if (y >= 1 || y <= 6) { //if y is greater then or = to 1 or less then
std::cout << "Rolling dice, if your number is the correct number you win\n";
srand(time(NULL)); //this makes rand() actually random.
z = rand() % 6;
std::cout << "The number is " << x[z] << "\n" << "The game will repeat..\n";
}
else { //if the num generated isn't within 1 or 6 this is printed
std::cout << "ERROR: Generated number is not within 1 nor 6, try again.";
}
if (y == z) {
std::cout << "Congratulations you rolled the right number";
}
}
(input is y) (the array is x) (the number you need to win is z)
also I may change it so that it just reads the array so that the user could even put in the amount of sides the dice would have if this goes well.
Upvotes: 0
Views: 95
Reputation: 354
There's five issues I found with your code.
First and foremost, you are better off doing while(true)
instead of for( ; ; )
.
Secondly, your else block should start on the same line that terminates the if block.
Third, you should insert a break
statement when you win the game.
Fourth, you should change the condition if (y >= 1 || y <= 6)
to if(y >= 1 && y <= 6)
. The difference between the two is that ||
is the OR logical operator. This is true if y is greater than or equal to 1, or if y is less than or equal to 6, which essentially works for every integer. 999 would pass because it's greater than or equal to 1, and -999 would pass because it's less than or equal to 6. And also any number between 1 and 6 would pass because they would pass both y >= 1 and y <= 6. If you were to insert an AND operator &&
in place of the OR operator, then the condition would pass because it would only be true if y is in between 1 and 6.
Fifth, You should move the if block with the condition y == z
to be nested inside of y >= 1 && y <= 6
. The code below is every change I've made:
while (true) { //1
int x[6] = { 1,2,3,4,5,6 };
int y;
int z;
std::cout << "Pick a number between 1 and 6\n";
std::cin >> y;
if (y >= 1 && y <= 6) { //4
std::cout << "Rolling dice, if your number is the correct number you win\n";
srand(time(NULL));
z = rand() % 6;
std::cout << "The number is " << x[z] << "\n" << "The game will repeat..\n";
if (y == z) { //5
std::cout << "Congratulations you rolled the right number";
break; //3
}
} else { //2
std::cout << "ERROR: Generated number is not within 1 nor 6, try again.";
}
}
Upvotes: 1
Reputation: 60392
This condition:
if (y >= 1 || y <= 6)
is going to be true for all values of y
. Every integer is either greater than or equal to 1, or less than or equal to 6.
You need a conjunction, like this:
if (y >= 1 && y <= 6)
You also need to break;
when y == z
, otherwise you end up with an infinite loop.
Upvotes: 4