+ 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(); } }

26th May 2018, 4:35 AM
a a
a a - avatar
5 ответов
+ 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); }
26th May 2018, 5:14 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Here, I've updated the code. Now it's working fine! https://code.sololearn.com/cdfDh6vpZYSM/?ref=app
26th May 2018, 5:04 AM
Çůřîöş ßąšäñț 🇮🇳
Çůřîöş ßąšäñț 🇮🇳 - avatar
+ 1
https://code.sololearn.com/cJ6oeeddBmnJ/#cpp Here is the solution for your code.
26th May 2018, 5:36 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
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.
26th May 2018, 7:36 AM
ODLNT
ODLNT - avatar
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.
2nd Jun 2018, 3:08 PM
husainadwan