-
Roll a dice program.
I'm trying to make a dice program that will give a random number when user throws the dice. Every random number gets logged into a system that shows how many of every number has come. It's basic atm but my goal is to make it good.
It goes through the converter but it's not working properly.
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
srand((unsigned)time(0));
int first = 0;
int second = 0;
int third = 0;
int fourth = 0;
int fifth = 0;
int sixth = 0;
char Throw;
int i;
int randomnumber = 0;
do{
cout << "Write y if you want to throw, q quits\n";
cin >> Throw;
{
if(Throw == 'y')
for(int i=0; i<1; i++)
randomnumber = (rand()%6)+1;
}
{
if(randomnumber == '1')
first = first + 1;
else if(randomnumber == '2')
second = second + 1;
else if(randomnumber == '3')
third = third + 1;
else if(randomnumber == '4')
fourth = fourth + 1;
else if(randomnumber == '5')
fifth = fifth + 1;
else if(randomnumber == '6')
sixth = sixth + 1;
cout <<first<<"\n";
cout <<second<<"\n";
cout <<third<<"\n";
cout <<fourth<<"\n";
cout <<fifth<<"\n";
cout <<sixth<<"\n";
}
}
while(Throw != 'q');
return 0;
}
It prints: "
y
0
0
0
0
0
0
" indicating that none of the variables have changed.
-
Re: Roll a dice program.
Proper indenting and code tags should show you part of the problem:
Code:
int main()
{
srand((unsigned)time(0));
int first = 0;
int second = 0;
int third = 0;
int fourth = 0;
int fifth = 0;
int sixth = 0;
char Throw;
int i;
int randomnumber = 0;
do{
cout << "Write y if you want to throw, q quits\n";
cin >> Throw;
{
if(Throw == 'y')
for(int i=0; i<1; i++)
randomnumber = (rand()%6)+1;
}
{
if(randomnumber == '1')
first = first + 1;
else if(randomnumber == '2')
second = second + 1;
else if(randomnumber == '3')
third = third + 1;
else if(randomnumber == '4')
fourth = fourth + 1;
else if(randomnumber == '5')
fifth = fifth + 1;
else if(randomnumber == '6')
sixth = sixth + 1;
cout <<first<<"\n";
cout <<second<<"\n";
cout <<third<<"\n";
cout <<fourth<<"\n";
cout <<fifth<<"\n";
cout <<sixth<<"\n";
}
}while(Throw != 'q');
return 0;
}
You proabably want those "if(randomnumber == 'x')"inside the for loop.
Also:
Code:
randomnumber == '1'
Randome is an integer who's value is between 1 and 6. '1' is a char. It's integer value is just a code that represents one. Proabably some random number between 40 an 100. what you want is:
Code:
if(randomnumber == 1)
Notice the lack of quotes
-
Re: Roll a dice program.
Code:
if(randomnumber == '1')
This is comparing randomnumber (which is HEX 0x01.....0x06) to ASCII '1' which is HEX 0x31.
Try removing the apostrophies in the other if statements as well.
Code:
if(randomnumber == 1)
-
Re: Roll a dice program.
My friend helped me with this but he missed the loop thing. Thanks so much now it works!