이 프로그램은 for 루프를 사용하여 7 일 동안의 기온을 수집 한 다음 기본적으로 평균 온도와 최고 기록 온도로 다시 인쇄합니다. 다음 코드는 훨씬 더 큰 프로그램의 일부입니다. 어쨌든 문제는 "highest_temp1"float 변수 인 것 같습니다. 프로그램을 실행하면 최고 온도 대신 오류 코드가 생성됩니다. 이 코드 조각은 별도의 소스 파일에서 테스트되었으며 아무런 문제가 없습니다.스위치 내부에서 부동 변수가 작동하지 않음 Statst
switch (choice)
{
case 3:
int n;
float temperatures [7];
float lastweektemp [7] = {12.56,8.65,7.5,10,7.9,5,8};
float highest_temp1, highest_temp2;
float accumulated_temp1, accumulated_temp2;
system("CLS");
cout << "____________Weather Data____________" << endl << endl;
for (n = 0; n<7; n++)
{
cout << "What is the temperature for Day " << n+1 << " ?" << endl;
cin >> temperatures[n];
if (highest_temp1 < temperatures [n])
{
highest_temp1 = temperatures [n];
}
if (highest_temp2 < lastweektemp [n])
{
highest_temp2 = lastweektemp [n];
}
accumulated_temp1 = accumulated_temp1 + temperatures[n];
accumulated_temp2 = accumulated_temp2 + lastweektemp [n];
}
cout << endl << " Day This Week Last Week" << endl;
for (n=0; n<7; n++)
{
cout << n+1 << temperatures[n] << lastweektemp[n] << endl;
}
system("CLS");
cout << " Weather Report" << endl;
cout << " --------------" << endl << endl;
cout << "Current Week: " << endl;
cout << "-------------" << endl;
for (n=0; n<7; n++)
{
cout << "Day " << n+1 << ": " << temperatures[n] << endl;
}
cout << endl << " Average: " << accumulated_temp1/7 << endl;
cout << " Highest Temperature: " << highest_temp1 << endl;
cout << "Last Week: " << endl;
cout << "----------" << endl;
for (n=0; n<7; n++)
{
cout << "Day " << n+1 << ": " << lastweektemp[n] << endl;
}
cout << endl << " Average: " << accumulated_temp2/7 << endl;
cout << " Highest Temperature: " << highest_temp2 << endl;
system("PAUSE");
}
현재 주에서 가장 높은 온도는 24이지만 인쇄하는 "최고 온도 : 3.45857e + 032"
이 정확한 '오류 코드'가 변화하지 않는 내가 프로그램을 실행할 때마다 나타나고있다 .
나는 초보자이며 사진을 업로드 할 수없는 이유는 무엇입니까?
도움을 주시면 감사하겠습니다. 나는 대학에서 작은 과제를하고있다. 이것은 내 첫 질문이므로 쉽게 가십시오 !!
_ 사용하기 전에 변수를 초기화 그래서 당신은 당신의 질문에 여기 오류 코드의 텍스트를 복사 할 수 있습니다. 우리는 어쨌든 그것을 그림으로보고 싶지 않습니다. –
디버거에서 코드를 단계적으로 수행하여 초기화되지 않은 변수를 사용중인 위치를 정확히 확인합니다. 또한, 컴파일러 경고를 사용하면 같은 것을 말할 수 있습니다. –