Search code examples
visual-c++triggersbreakpoints

Why is this triggering a breakpoint?


I have looked extensively for the problem in this code, but I can't seem to figure out what tragic error I made and why it is triggering a breakpoint. (After 3 or 4 inputs, it triggers and I don't know why it doesn't trigger at the start or what is causing it)

#include <conio.h> // For function getch()
#include <cstdlib>  // For several general-purpose functions
#include <fstream>  // For file handling
#include <iomanip>  // For formatted output
#include <iostream>  // For cin, cout, and system
#include <string>  // For string data type
using namespace std;  // So "std::cout" may be abbreviated to "cout", for   example.

string convertDecToBin(int dec)
{
    int *arrayHex, arraySize = 0;
    arrayHex = new int[];
    string s = " ";
    int r = dec;
    for (int i = 0; r != 0; i++)
    {
        arrayHex[i] = r % 2;
        r = r / 2;
        arraySize++;
    }

    for (int j = 0; j < arraySize; j++)
    {
        s = s + to_string(arrayHex[arraySize - 1 - j]);
    }
    delete[] arrayHex;
    return s;
}


string convertDecToOct(int dec)
{
    int *arrayHex, arraySize = 0;
    arrayHex = new int[];
    string s = " ";
    int r = dec;
    for (int i = 0; r != 0; i++)
    {
        arrayHex[i] = r % 8;
        r = r / 8;
        arraySize++;
    }

    for (int j = 0; j < arraySize; j++)
    {
        s = s + to_string(arrayHex[arraySize - 1 - j]);
    }
    delete[] arrayHex;
    return s;
}


int main()
{
    int input = 0;
    while (input != -1)
    {
        cout << "\nEnter a decimal number (-1 to exit loop): ";
        cin >> input;
        if (input != -1)
        {
            cout << "Your decimal number in binary expansion: " << convertDecToBin(input);
            cout << "\nYour decimal number in octal ecpression: " << convertDecToOct(input);
        }

    }
    cout << "\n\nPress any key to exit. . .";
    _getch();
    return 0;
}

Solution

  • arrayHex = new int[] is your problem - C\C++ does not support dynamic sizing arrays. You need to specify a size for the array to allocation, otherwise you'll get memory block overruns.