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.
Aucun commentaire:
Enregistrer un commentaire