나는 C++으로 간단한 응용 프로그램을 작성하여 합계의 빈도 (즉, 주사위를 굴릴 때)를 계산하도록 고안되었습니다. 프로그램이 완전히 실행되고 올바른 결과가 나오기는하지만 실행이 끝나면 Windows에서 프로그램이 작동하지 않는다고 알려줍니다.C++ 프로그램이 완전히 실행되지만 끝에 끊어 집니까?
다음 코드를 작성하고 컴파일하려면 TDM-GCC 4.9.2 32 비트 릴리스 컴파일러에서 Dev-Cpp 5.11을 사용하고 있습니다. while(val[pos] >= r){...
가 pos
때까지 계속 반복 할 수있다
#include<iostream>
#include<limits>
#include<string>
using namespace std;
const int int_min = numeric_limits<int>::min(),
int_max = numeric_limits<int>::max();
int getint(string ln, int lower, int upper){
int input = 0;
cout << ln;
if(cin >> input && input >= lower && input <= upper){
cin.clear();
cin.ignore(80,'\n');
}else{
cout << "ERR:\tINVALID\nDesc:\t";
if(cin.good() && (input <= lower || input >= upper))
cout << "OUT OF BOUNDS [" << lower << " <= val <= " << upper << "]";
else
cout << "NAN";
cout << "\n\n";
cin.clear();
cin.ignore(80,'\n');
return getint(ln,lower,upper);
}
return input;
}
int main(){
int
n = getint("Input(n) > ",1,int_max),
a = getint("Input(a) > ",0,int_max),
b = getint("Input(b) > ",a,int_max),
r = b - a + 1,
t = n * (r - 1) + 1;
int
pos = 0,
sum = 0,
val[n],
frq[t];
for(int i = 0; i < n; i++)
val[i] = 0;
for(int i = 0; i < t; i++)
frq[i] = 0;
while(pos < n){
pos = 0;
sum = 0;
for(int i = 0; i < n; i++)
sum += val[i];
frq[sum]++;
val[pos]++;
while(val[pos] >= r){
val[pos++] = 0;
if(pos <= n - 1)
val[pos]++;
}
}
for(int i = 0; i < t; i++){
cout << "frq(" << i + n << ")\t|\t" << frq[i] << endl;
}
return 0;
}
경고 : 가변 길이 배열 (val [n] 및 frq [t]')을 사용하고 있습니다. 이것은 비표준 구문이며 스택 오버플로를 일으키는 경향이 있습니다. – user4581301
참고 사항 : 알파벳 스프프 변수 이름 지정 체계를 사용하면 코드를 디버깅하려는 사람들에게 방해가됩니다. 아디오스. – user4581301