Sign in to follow this  
Followers 0
Hellu

Need some help with c++ code

3 posts in this topic

Hello there, I got a problem which I hope you can help me with.

I would like to get the lowest score and the name of the one with the lowest score(score =  "total") to be written out.

I think I got it but the thing is that the last "player" always gets printed out as the one with the lowest score.

 

This is all of my code, don't mind the comments that I've made, they are just there to help me keep track of things.

#include <iostream>#include <string>#include <cstdlib>#include <ctime>using namespace std;int main(){    srand(time(NULL));    int dice1, dice2, DiceThrow, total = 0, minTotal = 0;    string name, exit, badLuckName;    cout << "We will now be playing \"Bad luck with dices\" " << endl;    cout << "How many throws with the dices? " << endl;    cin >> DiceThrow;    cout << endl;    do{        cout << "Your name: ";        cin >> name;        if (name != "exit")    { //Öppnar if            total = 0;            for (int i = 0; i < DiceThrow; i++)            {//öppnar for                dice1 = rand() % 6 + 1;                dice2 = rand() % 6 + 1;                total += dice1 + dice2;                minTotal = total;                cout << "Dice thrown: " << dice1 << " " << dice2 << " = " << dice1 + dice2 << endl;                            }//stänger for                        if (minTotal <= total)                        {                            minTotal = total;                            badLuckName = name;                        }            cout << "Total sum: " << total << endl << endl;    }//Stänger if                    //Startar om/avslutar do-loopen.    } while ( name != "exit"); //Kollar om man har skrivit "exit" som name och går då vidare till if-loopen    if (name == "exit")    {        cout << badLuckName<< " got the lowest score of: " << minTotal << "!" << endl; //Skriver ut den som har lägst poäng    }    return 0;}

any ideas? Help is HIGHLY appreciated.

 

*EDIT* I have no idea why the code is in different colours, I used the "<> - code" copy/paste thing, guess it's supposed to be like that with it.

Share this post


Link to post
Share on other sites

I had some spare time so I figured I'd walk you through the debugging process. If you just want the answer, it's here: 

Please login or register to see this link.

 


 

Ok so there are a few problems with your code, but the biggest one is your formatting is messed up which makes it much harder to see what's going on. Here is your code reformatted:

 

#include <iostream>#include <string>#include <cstdlib>#include <ctime>using namespace std;int main(){    srand(time(NULL));    int dice1, dice2, DiceThrow, total = 0, minTotal = 0;    string name, exit, badLuckName;    cout << "We will now be playing \"Bad luck with dices\" " << endl;    cout << "How many throws with the dices? " << endl;    cin >> DiceThrow;    cout << endl;    do    {        cout << "Your name: ";        cin >> name;        if (name != "exit")        {            total = 0;            for (int i = 0; i < DiceThrow; i++)            {                dice1 = rand() % 6 + 1;                dice2 = rand() % 6 + 1;                total += dice1 + dice2;                minTotal = total;                cout << "Dice thrown: " << dice1 << " " << dice2 << " = " << dice1 + dice2 << endl;                            }            if (minTotal <= total)            {                minTotal = total;                badLuckName = name;            }            cout << "Total sum: " << total << endl << endl;        }    } while (name != "exit");    if (name == "exit")    {        cout << badLuckName<< " got the lowest score of: " << minTotal << "!" << endl;    }    return 0;}
If you walk through the code step by step and produce pseudocode for it, you can see it doesn't make sense

 

Seed the random number generator

Get number of dice throws per run

Get player's name

If the player's name is exit, display name of player with lowest score and display lowest score

We can immediately stop there and realise that the name of the player with the lowest score and the lowest score are going to be undefined, so we need to fix those problems before carrying on with the program. There are a few solutions to this, but my ideal solution to this would be reversing the if statement in the do while loop, like this:

 

#include <iostream>#include <string>#include <cstdlib>#include <ctime>using namespace std;int main(){    srand(time(NULL));    int dice1, dice2, DiceThrow, total = 0, minTotal = -1;    string name, exit, badLuckName;    cout << "We will now be playing \"Bad luck with dices\" " << endl;    cout << "How many throws with the dices? " << endl;    cin >> DiceThrow;    cout << endl;    do    {        cout << "Your name: ";        cin >> name;        if (name == "exit")        {            if(minTotal != -1)            {                cout << badLuckName<< " got the lowest score of: " << minTotal << "!" << endl;            }        }        else        {            total = 0;            for (int i = 0; i < DiceThrow; i++)            {                dice1 = rand() % 6 + 1;                dice2 = rand() % 6 + 1;                total += dice1 + dice2;                minTotal = total;                cout << "Dice thrown: " << dice1 << " " << dice2 << " = " << dice1 + dice2 << endl;                            }            if (minTotal <= total)            {                minTotal = total;                badLuckName = name;            }            cout << "Total sum: " << total << endl << endl;        }    } while (name != "exit");    return 0;}
Now let's walk through it again:

Seed the random number generator

Get number of dice throws per run

Get player's name

If the player's name is exit, and if the minimum total is not unset (-1), display the name of the player with the lowest score and the lowest score

If the player's name is not exit...

    For the number of dice throws per game

        Set dice1 to a random number from 1-6

        Set dice2 to a random number from 1-6

        Increment total by (dice1 + dice2)

    If the minimum total is less than or equal to the total

        Set the minimum total to the total

        Set the name of the player with the lowest score to the current player's name

Continue Loop

Now your problem is it's always the last player that gets printed out as the one with the lowest score, so let's take a look at all the assignments to badLuckPlayer

if (minTotal <= total){    minTotal = total;    badLuckName = name;}
Well, clearly badLuckName is having the current player's name assigned to them if the minimum total score is less than the total score, so let's look at assignments to minTotal

for (int i = 0; i < DiceThrow; i++){    dice1 = rand() % 6 + 1;    dice2 = rand() % 6 + 1;    total += dice1 + dice2;    minTotal = total;}
As you can see, we're assigning minTotal the value of total every time the loop runs, which means we lose the value we stored before, so let's remove that assignment, reverse the check so we're checking if minTotal is less than or equal to total, and add a check so that we assign minTotal if minTotal is unset

#include <iostream>#include <string>#include <cstdlib>#include <ctime> using namespace std; int main(){    srand(time(NULL));     int dice1, dice2, DiceThrow, total = 0, minTotal = -1;    string name, exit, badLuckName;     cout << "We will now be playing \"Bad luck with dices\" " << endl;    cout << "How many throws with the dices? " << endl;    cin >> DiceThrow;    cout << endl;     do    {        cout << "Your name: ";        cin >> name;         if (name == "exit")        {            if(minTotal != -1)            {                cout << badLuckName<< " got the lowest score of: " << minTotal << "!" << endl;            }        }        else        {            total = 0;            for (int i = 0; i < DiceThrow; i++)            {                 dice1 = rand() % 6 + 1;                dice2 = rand() % 6 + 1;                total += dice1 + dice2;                 cout << "Dice thrown: " << dice1 << " " << dice2 << " = " << dice1 + dice2 << endl;            }                            if (total <= minTotal || minTotal == -1)            {                minTotal = total;                badLuckName = name;            }             cout << "Total sum: " << total << endl << endl;        }     } while (name != "exit");     return 0;}
After running it through ideone here 

Please login or register to see this link.

I get the following results

 

1fd55bf974.png

 

Which I think is the result you wanted (:

4 people like this

Share this post


Link to post
Share on other sites

Thank you for your incredibly detailed answer! You explained it very well and it was exactly what I wanted it to do. You also showed me that pseudocode is an important step in the process, next time I'll make sure not to forget making one :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0