+ 2
Can someone help me fix my code?
I have to repeat the program upon user entering 1 when prompted the second time. #include <iostream> #include <cstdlib> using namespace std; int times; int value_continue; int rollDie() { return rand() % 6; } void shouldContinue() { cout << "Do you want to continue\n1. Yes 2. No" << endl; cin >> value_continue; } int main() { cout << "How many times do you want to roll a die?: " << endl; cin >> times; for (int x = 0; x <= times; x++){ cout << rollDie() << endl; } shouldContinue(); if (value_continue == 1){ rollDie(); shouldContinue(); } }
5 Respuestas
+ 7
// Perhaps a better replacement:
#include <iostream>
#include <cstdlib>
using namespace std;
int times;
int value_continue;
int rollDie()
{
return 1 + rand() % 6;
}
int shouldContinue()
{
cout << "Do you want to continue\n1. Yes 2. No" << endl;
cin >> value_continue;
return value_continue ;
}
int main()
{
do {
cout << "How many times do you want to roll a die?: " << endl;
cin >> times;
for (int x = 0; x < times; x++)
cout << rollDie() << endl;
}
while(shouldContinue() == 1);
}
+ 1
Here, I've updated the code. Now it's working fine!
https://code.sololearn.com/cdfDh6vpZYSM/?ref=app
+ 1
https://code.sololearn.com/cJ6oeeddBmnJ/#cpp
Here is the solution for your code.
0
a a
Just in case you were wondering what was wrong with your code. In the if statement code block you were calling rollDie() with no for loop or cout statement. So rollDie() just sits there and generates a random number that is it.
The reason you are printing out an extra dice roll is due to the 0, the code should be for(int x=1 )
Your die sometimes rolls a 0 and we know that not right. Hasty Rei fixed that by adding 1 to the random statement.
As you can see opinions are like assholes everyone has one here is mine (opinion)
https://code.sololearn.com/c60dapoT0oL7/#cpp
I hope I added something of value to the discussion.
0
you could look at it online becaus i know a littel bit of java caus i’m new. hope you will have the right friend. ; ) &thanks for all of your codes.