Rather than flame me, please give me constructive criticism as Im very new to programming and still in the learning phase.
My objective: make a dice game where the user can choose to continue or quit, and if he chooses to continue then he would be asked how many times he wants the computer to roll a dice (he can only choose 1-6 because anything after 6 would mess with my switch statement). Say you type 3, the computer rolls 3 random numbers from 1 to 6 (which is a dice) and prints the output. You can continue to type a number for the computer to roll or you can type -1 to finish. If you type -1, the computer adds up all of the times 1, 2, 3, 4, 5, and 6 were rolled and puts an output at the end for the result.
My problem: I cant get the result to output correctly.. It is taking the value of what the user inputs for how many times they want to roll. I know thats whats going wrong, but I dont know how to rewrite the code to do specifically what Im wanting it to do (which I stated above). Can anyone help please? Thanks!
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
string rollorquit;
int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0;
int wanttoroll;
cout << "------------------------------------DICE GAME-----------------------------------\n";
cout << "Type 'roll' to roll, or 'quit' to quit\n";
cin >> rollorquit;
cin.ignore();
if (rollorquit != "roll"){
cout << "PROGRAM TERMINATED";
}
while (rollorquit == "roll" && wanttoroll != -1){
while (wanttoroll != -1){
cout << "How many times do you want to roll?\n(cant be more than 6) or type -1 to finish: ";
cin >> wanttoroll;
cin.ignore();
srand(time(0));
for (int x = 0; x < wanttoroll; x++){
cout << 1+(rand()%6) << endl;
switch (wanttoroll){
case 1:
one++;
break;
case 2:
two++;
break;
case 3:
three++;
break;
case 4:
four++;
break;
case 5:
five++;
break;
case 6:
six++;
break;
default:
cout << "The program had an error!";
}
}
}
cout << "The total times the number one came up: " << one << endl;
cout << "The total times the number two came up: " << two << endl;
cout << "The total times the number three came up: " << three << endl;
cout << "The total times the number four came up: " << four << endl;
cout << "The total times the number five came up: " << five << endl;
cout << "The total times the number six came up: " << six << endl;
}
return 0;
}
You want to store the result of the random number generator on line 35 and use that for your switch instead. wanttoroll is your variable for the number of rolls, not the result of a roll.
I changed the variable in the switch statement to equal randvar, and set randvar equal to the result of random number generator. But when I compiled, it was spitting out random numbers. It said one was randomly rolled 3 times, but it was only twice. Two randomly rolled 4 times, but it was rolled once. You get the idea.. what am I doing wrong? Give me an example please?
while (rollorquit == "roll" && wanttoroll != -1){
while (wanttoroll != -1){
cout << "How many times do you want to roll?\n(cant be more than 6) or type -1 to finish: ";
cin >> wanttoroll;
cin.ignore();
//srand(time(0));
for (int x = 0; x < wanttoroll; x++){
int rvar = 1+(rand()%6);
cout<<rvar<<" "<<endl;
switch (rvar){
case 1:
one++;
break;
case 2:
two++;
break;
case 3:
three++;
break;
case 4:
four++;
break;
case 5:
five++;
break;
case 6:
six++;
break;
default:
cout << "The program had an error!";
}
}
}
cout << "The total times the number one came up: " << one << endl;
cout << "The total times the number two came up: " << two << endl;
cout << "The total times the number three came up: " << three << endl;
cout << "The total times the number four came up: " << four << endl;
cout << "The total times the number five came up: " << five << endl;
cout << "The total times the number six came up: " << six << endl;
}
It works fine for me and I didn't really change your code. I just changed the formatting. I commented out srand for consistency while testing. Are you sure the count for the frequency is random? Are you printing the result of each roll and checking against the frequency at the end?
Im not sure I understand your question. What Im doing is seeing how many times each number shows up in all of the rolls. If he enters 3, 5, 4, then -1 to finish, the program prints 3 random numbers, 5 random numbers, then 4 random numbers. Then it would check to see how many tmies 1, 2, 3, 4, 5, 6, were printed out, if at all, and give us the output.
Nevermind! I found the solution =D!!!! I did what you said except I forgot to remove the cout << 1+(rand()%6) << endl;
from my for loop and replace it with the new variable equaling the random number generator, then outputting that new variable and adding it into the switch statement. THANK YOU
Dont forget: this is my first program. I know what an array is but dont really know how to implement them, and same for loops, I still have a hard time implementing some concepts. But it should get better over time.