目前我已經達到了這個程序的所有目標,除了輸出我的字母分級平均值。現在它總是輸出70而不是字母等級。有沒有人看到我的問題是什麼?我目前知道它在通過lettergrade函數後將我的歌譜值重新定義爲70。輸出字母等級的問題
#include<iostream>
#include<string>
using namespace std;
int getexams(int count)
{
cout << "Number of exams to be entered:";
cin >> count;
if (count > 1024)
{
cout << "Please enter a number below 1024." << endl;
system("pause");
return 0;
}
else
return count;
}
int enterexams(int name[],int count, int b)
{
int numexams = count;
for (int a = 0;a < numexams;a++)
{
cout << "Enter grades: ";
cin >> name[a];
b++;
}
return b;
}
char letter(double average, char lettergrade)
{
int x;
x = average;
if (x >= 90) {
lettergrade = 'A';
}
else if (x >= 80) {
lettergrade = 'B';
}
else if (x >= 70) {
lettergrade = 'C';
}
else if (x >= 60) {
lettergrade = 'D';
}
else {
lettergrade = 'F';
}
return lettergrade;
}
void output(int count,int name[], double average, int lettergrade)
{
cout << "Number of exams: " << count << endl;
for (int a = 0;a < count;a++)
cout << "Exam grades: " << name[a] << endl;
cout << "Average grade: " << average << endl;
cout << "Letter grade: " << lettergrade << endl;
}
int averagecalc(int average,int count,int name[])
{
int a,sum,x;
sum = 0;
for (a = 0;a < count;a++)
{
sum += name[a];
}
average = sum/count;
return average;
}
int main()
{
char lettergrade = 0;
int b = 0;
int total = 0;
double average = 0;
int count =0;
count = getexams(count);
// Number of exams
int exams = count;
const int x = exams; // Exam Values
int name[1024];
enterexams(name,count,b);
average = averagecalc(average,count,name);
lettergrade = letter(average, lettergrade);
output(count, name, average, lettergrade);
system("pause");
return 0;
}
這聽起來像您可能需要學習如何使用調試器來逐步執行代碼。使用一個好的調試器,您可以逐行執行您的程序,並查看它與您期望的偏離的位置。如果你打算做任何編程,這是一個重要的工具。深入閱讀:** [如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver
您可以打印出X的值IF語句前的lettergrade函數。什麼是輸出? – cosmoonot
定義後輸出50 – James