Jump to content

Need Help C++

ohJey
Go to solution Solved by Enderman,
6 minutes ago, ohJey said:

the problem is I'm trying to subtract the last dice roll and I can't figure out how to do it

the lands.back() was me trying to get the last number in the vector... but it gives me random numbers

why cant you do 

number = rand()%6;
            i=i-number;

 

like you did in the first if statement?

I'm trying to make the program simulate dice rolls... the dice add up until 100.  If the dice "rolls" and the number is over 100, the roll is subtracted until 100 is landed upon.  Ex: 98+4=102, then 102-4=98, then it keeps "rolling" and subtracting until 100 is landed on

 

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace std;

int main()
{
    srand(time(0));
    int number, tries{0}; ///the number that the dice lands on
    vector<unsigned int> lands(5);
    for(int i=0; i<=100; ++i)
    {
        if(i<100)
        {
            number = rand()%6;
            i=number+i;
            cout << "i: " << i << endl;
            ++lands[number];
        }
        if(i>100)
        {
            i=i-lands.back(); ///need help here
            cout << "lands.back(): " << lands.back() << endl; ///debugging
            cout << "i: " << i << endl;
        }
        else if(i==100)
        {
            cout << "i: " << i << endl;
            cout << "Done!" << endl;
        }
    }
    tries = lands[0]+lands[1]+lands[2]+lands[3]+lands[4]+lands[5];
    cout << "Tries: " << tries;
    cout << "\nAfter " << tries << " throws the lands are: "
         << "\n#1= " << lands[0]
         << "\n#2= " << lands[1]
         << "\n#3= " << lands[2]
         << "\n#4= " << lands[3]
         << "\n#5= " << lands[4]
         << "\n#6= " << lands[5];
}

 

 

 

 

Linux "nerd".  If I helped you please like my post and maybe add me as a friend :)  ^_^!

Link to comment
Share on other sites

Link to post
Share on other sites

I dont know what youre trying to do with lands.back(), ive never used that

but i know there is a much easier way to solve this

 

just use a while loop, with the condition that i!=100

then just loop, if i<100 then add, if i>100 then subtract

then you get to i=100 the loop will break and you can have the print statement at the end

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Enderman said:

I dont know what youre trying to do with lands.back(), ive never used that

but i know there is a much easier way to solve this

 

just use a while loop, with the condition that i!=100

then just loop, if i<100 then add, if i>100 then subtract

then you get to i=100 the loop will break and you can have the print statement at the end

the problem is I'm trying to subtract the last dice roll and I can't figure out how to do it

the lands.back() was me trying to get the last number in the vector... but it gives me random numbers

Linux "nerd".  If I helped you please like my post and maybe add me as a friend :)  ^_^!

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, ohJey said:

the problem is I'm trying to subtract the last dice roll and I can't figure out how to do it

the lands.back() was me trying to get the last number in the vector... but it gives me random numbers

why cant you do 

number = rand()%6;
            i=i-number;

 

like you did in the first if statement?

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Enderman said:

why cant you do 


number = rand()%6;
            i=i-number;

 

like you did in the first if statement?

wow... I did 

i = i-number-1 and it worked

:/

Linux "nerd".  If I helped you please like my post and maybe add me as a friend :)  ^_^!

Link to comment
Share on other sites

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

×