0

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.

8
  • 6
    every number will be either >= 1 or <= 6
    – bolov
    Commented Apr 4, 2021 at 15:17
  • 4
    x y z are terrible names. Instead of commenting //user choice name your variable user_choice.
    – bolov
    Commented Apr 4, 2021 at 15:19
  • this was just something i threw together yesterday to see if i knew enough so far, but ill take that into consideration next time! ty!
    – cs07
    Commented Apr 4, 2021 at 15:22
  • also yeah i forgot that integers are wonky like that so should have known that was a dumb way to put the if
    – cs07
    Commented Apr 4, 2021 at 15:26
  • Not that this bug has nothing to do with ints. The issue is the logical operator that you used.
    – cigien
    Commented Apr 4, 2021 at 15:27

2 Answers 2

4

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.

5
  • This helped! ty! also yeah i noticed the infinite break thing but that was only once and i thought it was just the keyboard lol
    – cs07
    Commented Apr 4, 2021 at 15:25
  • sorry i removed the check from yours, when i compiled the code it'd give me a memory error and prevent me from using it but amicharski came in clutch and when i applied some of his changes i didn't get it anymore, ty tho
    – cs07
    Commented Apr 4, 2021 at 15:39
  • I'm not sure what you mean by memory error, but I'm glad you fixed it. Another thing is that you expect the user to put a number between 1 and 6, but z only takes values between 0 and 5. You need to add 1 to z to get it right.
    – cigien
    Commented Apr 4, 2021 at 15:43
  • i got c6001 when i put in yours, and about the z issue wdym? when i compile it im not getting any apparent issues but do you mean just add 1 to z like this or? ``` z = rand() % 7; ```
    – cs07
    Commented Apr 4, 2021 at 15:49
  • If the user inputs 6, it can never match z. Basically, the program and the user are rolling slightly different dice, and that's probably not what you want.
    – cigien
    Commented Apr 4, 2021 at 15:51
1

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.";

        }
    }
5
  • alright thanks! I applied every change you made to my code but why is it better to use while(true)?
    – cs07
    Commented Apr 4, 2021 at 15:33
  • Because for loops are designed specifically for discrete looping and while loops are designed for a more broad range of looping techniques: support.khanacademy.org/hc/en-us/articles/…. Commented Apr 4, 2021 at 15:39
  • ooh i get it, basically for is more restricted and has less headspace when it comes to what you want to do with the loop but while is better because it allows you to do more with the loop right?
    – cs07
    Commented Apr 4, 2021 at 15:42
  • 1
    for and while both have nearly identical functional capabilities, in the sense that any for loop can be transformed into a while loop and vice versa but you might have to put some extra brackets around the while version. The difference isn't that one is more restricted than the other, it's just a matter of convention for which kinds of loops each construct is used for. for tends to be used more for "do something for each discrete element of a collection", especially with the new range-based for, and while tends to be more "while arbitraryCondition is true" Commented Apr 4, 2021 at 15:59
  • oh ok Nathan. ty
    – cs07
    Commented Apr 4, 2021 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.