숫자를 10 진수에서 2 진수로 변환하는 프로그램을 작성 중입니다. 나는 이미 그것을위한 알고리즘을 가지고 있고 프로그램은 괜찮아요 cout을 사용하는 동안 작동합니다. 그러나, 루프에서 outfile을 사용하자마자 프로그램은 오류 코드 (0xC0000005)와 충돌합니다. 아웃 오브 바운드 요소에 액세스하여Outfile이 메모리 위반으로 인해 충돌을 일으킴
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{
int num, remainder_count;
ifstream infile; //define new input file stream
ofstream outfile; //define new output file stream
infile.open("C:\\Users\\Arctic-Gaming\\CLionProjects\\working\\Source\\Binary Conversion (Loop w File)\\Binary Input.txt"); //connect the stream to an actual file
if (!infile)
{
cout << "Cannot open input file! Program aborted" << endl;
return 1;
}
outfile.open("C:\\Users\\Arctic-Gaming\\CLionProjects\\working\\Source\\Binary Conversion (Loop w File)\\Decimal Output.txt"); //connect the stream to an actual file
do
{
int remainder [15] = {0};
remainder_count = 15;
infile >> num;
outfile << "\n" << num << endl;
if (num > 0 && num <= 65535)
{
while (num > 0)
{
remainder[remainder_count] = num % 2;
num /= 2;
remainder_count--;
}
remainder_count = 0;
while (remainder_count < 16)
{
if (remainder_count % 4 == 0)
{
outfile << " ";
}
outfile << remainder[remainder_count];
remainder_count++;
}
}
else if (num == 0)
outfile << "0000 0000 0000 0000" << endl;
else
cout << "Error! Invalid Input." << endl;
}
while (!infile.eof());
}
'remainder [remainder_count]'-'remainder_count == 15 '인 경우 어떻게됩니까? 그 라인에 뭐가 잘못 보이니? – PaulMcKenzie
스택 오버플로에 오신 것을 환영합니다. [The Tour] (http://stackoverflow.com/tour)를 읽으신 후 [Help Center] (http://stackoverflow.com/help/asking)의 자료를 참조하십시오. 여기에 물어보십시오. –
이러한 문제를 해결하는 올바른 도구는 디버거입니다. 스택 오버플로를 묻기 전에 코드를 단계별로 실행해야합니다. 자세한 도움말은 [작은 프로그램 디버깅 방법 (Eric Lippert 작성)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 참조하십시오. 문제를 재현하는 [최소, 완료 및 확인 가능] (http://stackoverflow.com/help/mcve) 예제와 함께 해당 질문을 \ [편집]해야합니다. 디버거. –