2017-03-08 44 views
-1

我已經廣泛查看了這段代碼中的問題,但我似乎無法弄清楚我做了什麼悲劇錯誤以及爲什麼它會觸發一個斷點。 (後3個或4個輸入,它觸發,我不知道爲什麼它不會在開始觸發或者是什麼原因造成)這是爲什麼觸發一個斷點?

#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; 
} 

回答

0

arrayHex = new int[]是你的問題 - Visual C \ C++不支持動態調整陣列大小。您需要爲要分配的數組指定大小,否則會導致內存塊超限。

相關問題