I don't understand why my code crashes when the variable "dicerolls" exceeds 15

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <iomanip>
using namespace std;

int main()
{
    string a_or_b;
    string aorbreverse;
    unsigned short int dicerolls;
    string y_or_n;
    double ab = 0.5;
    double aa = 0.5;
    double ba = (double)2 / 3;
    double bb = (double)1 / 3;
    double ans = 0;
    string letters = "";
    restart:
    cout << "Tring School Set 1 Maths H/W Quick Solver (by Kelechi)\n\nPress Enter";
    cin.get();
    system("cls");
    cout << "Are we starting at A or B ?\n\nType A or B below\n\n";
    cin >> a_or_b;
    system("cls");
    cout << "Okay so how many dice rolls ? (Between 1 and 65535)\n\n";
    cin >> dicerolls;
    system("cls");
    cout << "So to recap we're\n\nStarting at " << a_or_b
    << "\n\nAnd we're doing\n\n" << dicerolls 
    << " Dice Rolls\n\nType Y to continue, X to exit, or N to start again\n\n";
    cin >> y_or_n;
    if (y_or_n == "n" || y_or_n == "N")
       goto restart;
    else if (y_or_n == "x" || y_or_n == "X")
         return 0;
    int possible = 2;
    letters = a_or_b;
    unsigned long long int table [dicerolls];
    for (int n = dicerolls; n>0; n--)
    {
        table [dicerolls - n] = possible;
        possible = possible * 2;
    }
    string letterlist [table[dicerolls - 1]][dicerolls];
    unsigned long long int totalruns = 0;
    for (int n = dicerolls; n>0; n--)
    {
        totalruns = totalruns + table [dicerolls - 1];
    }
    unsigned int long long runs = 0;
    unsigned int long long yrownumber = 0;
    unsigned int long long xrownumber = 0;
    cout << letterlist [3][0];
    if (a_or_b == "a" || "A")
    {
               aorbreverse = "B";
    }
    else
    {
        aorbreverse = "A";
    }
    int x = table[0];
    int y = 0;
    int loops = 0;
    unsigned int runs2;
    for (int n = totalruns; n>0; n--)
    {
        if (runs == table[dicerolls-1])
        {
                 yrownumber = 0;
                 xrownumber++;
                 y++;
                 x = table[y];
                 runs=0;
        }
        if (runs2 >= table[dicerolls-1]/(x/2))
        {
                 runs2 = 0;
        }
        if (runs2 < table[dicerolls -1]/x)
        {
                 letterlist [yrownumber][xrownumber] = a_or_b;
                 yrownumber++;
        }
        if (runs2 >= table[dicerolls -1]/x && runs < table[dicerolls -1])
        {
                 letterlist [yrownumber][xrownumber] = aorbreverse;
                 yrownumber++;
        }
        runs++;
        runs2++;
    };
    
    for(int n=0;n<table[dicerolls-1];n++)
    {
            for (int i = 0;i<dicerolls;i++)
            {
                cout << letterlist [n][i];
            }
            cout << "\n";
    }
             
        
    cin.get();
    cin.get();
}

Debug and see what is the error. Tell us about the error so we can help. We are not compilers.

Lines 40 and 46 - These lines won't compile at all with many compilers since dicerolls isn't constant.

Where exactly does it crash? What line number and what's the error message?

As Vernon says, you are very lucky that line 40 and 46 compile. HOWEVER: if they do, the (which in your case they obviously do). The actual error is this: cout << letterlist [3][0]; .

That is because possible is an integer an actually over flows!!

for (int n = dicerolls; n>0; n--)
    {
        table [dicerolls - n] = possible;
        possible = possible * 2;
        std::cout<<"possible == "<<possible<<std::endl;
    }

Look at the output from this modification.

Integers only have a certain number of bit, e.g. 16. 1 is used for the sign and the other 15 are for the value, hence why it crashes after diceroll > 15.

How do I correct the problem with possible I need table to store larger values than an int but since table is used to control other arrays I'm at a loss of what to do. Though (as for lines 40 - 46 easy to correct)

I've made a few slight alterations, I'm using Dev-C++ and it's debugger is a load of good **rolls eyes**

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    double ab = 0.5;
    double aa = 0.5;
    double ba = (double)2 / 3;
    double bb = (double)1 / 3;
    double ans = 0;
    restart:
    cout << "Tring School Set 1 Maths H/W Quick Solver (by Kelechi)\n\nPress Enter";
    cin.get();
    system("cls");
    cout << "Are we starting at A or B ?\n\nType A or B below\n\n";
    string a_or_b;
    cin >> a_or_b;
    system("cls");
    cout << "Okay so how many dice rolls ? (Between 1 and 65535)\n\n";
    unsigned short int dicerollsvar;
    cin >> dicerollsvar;
    const short int dicerolls = dicerollsvar;
    system("cls");
    cout << "So to recap we're\n\nStarting at " << a_or_b
    << "\n\nAnd we're doing\n\n" << dicerolls 
    << " Dice Rolls\n\nType Y to continue, X to exit, or N to start again\n\n";
    string y_or_n;
    cin >> y_or_n;
    if (y_or_n == "n" || y_or_n == "N")
       goto restart;
    else if (y_or_n == "x" || y_or_n == "X")
         return 0;
    y_or_n = "";
    int possible = 2;
    unsigned long long int table [dicerolls];
    for (int n = dicerolls; n>0; n--)
    {
        table [dicerolls - n] = possible;
        possible = possible * 2;
        std::cout<<"possible == "<<possible<<std::endl;
    }
    string letterlist [table[dicerolls - 1]][dicerolls];
    unsigned long long int totalruns = 0;
    for (int n = dicerolls; n>0; n--)
    {
        totalruns = totalruns + table [dicerolls - 1];
    }
    unsigned int long long runs = 0;
    unsigned int long long yrownumber = 0;
    unsigned int long long xrownumber = 0;
    cout << letterlist [3][0];
    string aorbreverse;
    if (a_or_b == "a" || "A")
    {
               aorbreverse = "B";
    }
    else
    {
        aorbreverse = "A";
    }
    int x = table[0];
    int y = 0;
    int loops = 0;
    unsigned int runs2;
    for (int n = totalruns; n>0; n--)
    {
        if (runs == table[dicerolls-1])
        {
                 yrownumber = 0;
                 xrownumber++;
                 y++;
                 x = table[y];
                 runs=0;
        }
        if (runs2 >= table[dicerolls-1]/(x/2))
        {
                 runs2 = 0;
        }
        if (runs2 < table[dicerolls -1]/x)
        {
                 letterlist [yrownumber][xrownumber] = a_or_b;
                 yrownumber++;
        }
        if (runs2 >= table[dicerolls -1]/x && runs < table[dicerolls -1])
        {
                 letterlist [yrownumber][xrownumber] = aorbreverse;
                 yrownumber++;
        }
        runs++;
        runs2++;
    }
    ofstream StoredPossiblities ("possibilites.txt");
    for(int n=0;n<table[dicerolls-1];n++)
    {
            for (int i = 0;i<dicerolls;i++)
            {
                if (StoredPossiblities.is_open())
                {
                   StoredPossiblities << letterlist [n][i];
                }
                else
                    cout << "Couldn't open file";
            }
            StoredPossiblities << "\n";
    }
    StoredPossiblities.close();
    cin.get();
    cin.get();
}

No: That doesn't solve your problem, you have just put your problem to a slightly higher number. e.g. what happens if dicerolls exceeds 64. Same thing.

The trouble is that I don't understand what the underlying problem is that you are trying to solve [e.g. the reason for writing this computer program]. But I can see that for this algorithm at even , you are going to need more bytes than all the atoms in the universe, at modest numbers like 300 dice rolls.

This is due to line string letterlist [table[dicerolls - 1]][dicerolls]; because table is populated with 2,4,8,16 etc...

So can you give me [us ?] a quick summary of the problem that you are trying to solve please, and hopefully, some of the community here can suggest a slightly less memory intensive algorithm.

Okay I'll quote you the problem

The Network Game
The game is about moving around the network
At A Roll a dice
if it lands on an odd number, stay at A
if it lands on an even number move to B
At B roll a dice
if it lands on a 1 or 2, stay at B
if it lands on a 3,4,5,or 6 move to A

I wan't to find out the probablility of a sequence of events for any given dice throw
E.G
The probablility of landing on A if you start at A is 1/2
The probablility of landing on A if you start at B is 2/3

and so on, it was a problem we were given in maths class, I though HEY I CAN SOLVE THIS USING C++.

Any help ?

I don't think C++ helps. It's a math problem. A good old-fashioned paper and pencil would be the way to go, I think. Two states. Each has the possibility of staying where they are or switching.

But if you want to do it using C++, all that matters is what state I'm in NOW and the probability of staying in the same state. In other words I don't care at all where I USED TO BE. All that matters is that I'm here now. Hence I have no need to keep track of any of my past states or any of the previous dice rolls. Hence all of the arrays you have dealing with past rolls and past states can likely be deleted. I think you are keeping track of way too much information that you don't need to solve the problem.

I don't think C++ helps. It's a math problem. A good old-fashioned paper and pencil would be the way to go, I think. Two states. Each has the possibility of staying where they are or switching.

But if you want to do it using C++, all that matters is what state I'm in NOW and the probability of staying in the same state. In other words I don't care at all where I USED TO BE. All that matters is that I'm here now. Hence I have no need to keep track of any of my past states or any of the previous dice rolls. Hence all of the arrays you have dealing with past rolls and past states can likely be deleted. I think you are keeping track of way too much information that you don't need to solve the problem.

Yes it is a Math problem BUT, when you get over the 10 number it becomes TOTALY impractical to use a pen and paper as there are over 2^10 results at 2^15 or 15 dicerolls there are already 32768 potential solutions, a computer is required to solve the problem in any sensible amount of time.

Yes it is a Math problem BUT, when you get over the 10 number it becomes TOTALY impractical to use a pen and paper as there are over 2^10 results at 2^15 or 15 dicerolls there are already 32768 potential solutions, a computer is required to solve the problem in any sensible amount of time.

If I understand the problem correctly, I think the math solution would be not to simulate it with dice/iterations at all, but it's a small enough problem that you can calculate it exactly. You'd possibly need a calculator, but not a program.

But if you want to do it with a computer program, I stand by my last post. Even if you use the die, I don't think you want to keep track of everything that happened in the past. Just keep track of your current state.

If I understand the problem correctly, I think the math solution would be not to simulate it with dice/iterations at all, but it's a small enough problem that you can calculate it exactly. You'd possibly need a calculator, but not a program.

But if you want to do it with a computer program, I stand by my last post. Even if you use the die, I don't think you want to keep track of everything that happened in the past. Just keep track of your current state.

Yeah okay thanks anyway.

This article has been dead for over six months. Start a new discussion instead.