簡體   English   中英

我收到調試斷言失敗的錯誤?

[英]I'm getting an error that the debug assertion failed?

我正在嘗試從用戶讀取字符串,將其更改為 int 數組,然后按 int 顯示它。 這樣我就可以獲得非常大的 int 數字,但是當我運行它時,它讓我輸入一個數字,但是它給我一個錯誤,提示調試斷言失敗,我應該中止、重試或忽略。 存在無效的空指針。 我不知道是什么意思。 這是我所擁有的:

  //LargeInt.h File
  #pragma once
  #include <iostream>
  #include <string>

  using namespace std;

  class LargeInt
  {
  private:
  string number1;
  string number2;
  string sum;

  public:
  LargeInt();
  ~LargeInt();


  //This function will take the 
  //the number entered by user 
  //as int digits and print out
  //each digit. 
 void ReadNumber(int[]);
 };


 //LargeInt.cpp file
 #include "stdafx.h"
 #include "LargeInt.h"
#include <iostream>

 LargeInt::LargeInt() {     
 number1 = " ";
  }


 LargeInt::~LargeInt() { 
 }


  //This function will take the 
  //the number entered by user 
  //as int digits and print out 
  //each digit.  
 void LargeInt::ReadNumber(int
     number[]) {

    for (int i = 0; i < 75; i++) {      
       cout << number[i];   }

     }

 //Main File
 #include "stdafx.h"
 #include <string>
 #include "LargeInt.h"
 #include <iostream>

 using namespace std;

 int main()
{
string number1 = " ";
string number2 = " ";
string sum = " ";
int summ[75];

//Get number 1 from user and output it
int numberOne[76] = {};
cout << "Enter first number: ";
getline(cin, number1);
cout << endl;

//Check to see if it is more than 75 digits
if (number1.length() > 75) {
    cout << "Invalid number!" << endl;
}
else {
    int length1 = number1.length();
    cout << "First Number: " << endl;
    int k = 75;
    for (int i = length1; i >= 0; i--) {
        numberOne[k] = number1[i] - 48;
        k--;
    }
}
LargeInt num1;
num1.ReadNumber(numberOne);
cout << endl;

system("pause");
return 0;
}
int length1 = number1.length();

將把length1設置為字符串的大小。 然后在第一次迭代中

for (int i = length1; i >= 0; i--) {
    numberOne[k] = number1[i] - 48;
    k--;
}

您正在使用i = length1訪問number1[i]並且由於字符串位置為 0,因此您正在訪問超過字符串末尾的位置。 這是未定義的行為,在這種情況下正在拋出斷言。 要解決此問題,您需要將i設置為length1 - 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM