//This program averages 4 grades and assigns a letter grade based on the numeric average.
#include <iostream>
using namespace std;
int main()
{
int g1, g2, g3, g4; //Numeric grades to be averaged.
cout << "This program averages 4 grades. \n"; // Gathers number inputs to output GPA (Grade Point Average)
cout << "Enter first grade \n";
cin >> g1;
cout << "Enter second grade \n";
cin >> g2;
cout << "Enter third grade \n";
cin >> g3;
cout << "Enter fourth grade \n";
cin >> g4;
cout << "Your average is: \n";
const int
gpa = ((g1 + g2 + g3 + g4) / 4.0); // Average of g1, g2, g3, and g4.
rep = "Your GPA letter grade is: \n"; // Placeholder for the letter grade reply string.
cout << gpa
cout << gpa; // Outputs letter grade string
if (gpa >= 90)
cout << rep "A";
else if (gpa >= 80)
cout << rep "B";
else if (gpa >= 70)
cout << rep "C";
else if (gpa >= 60)
cout << rep "D";
else if (gpa >= 0)
cout << rep "F";
else
cout << "Invalid grade entered, enter positive digits only. \n"
system("pause");
return 0;
}
我收到以下错误消息:
1>c:\users\seven\source\repos\project2\project2\source.cpp(9): error C2144: syntax error: 'int' should be preceded by ';'
1>c:\users\seven\source\repos\project2\project2\source.cpp(9): error C2270: 'main': modifiers not allowed on nonmember functions
1>c:\users\seven\source\repos\project2\project2\source.cpp(14): error C2062: type 'int' unexpected
我特别不明白第一个错误。我为什么要一个;在int之前? “; int”,在每种情况下都有一个;在行之前的“ int”之前。抱歉,如果我没有正确地寻求帮助。尽我所能,谢谢!
答案 0 :(得分:1)
#include <iostream>
using namespace std;
int main()
{
int g1, g2, g3, g4; //Numeric grades to be averaged.
cout << "This program averages 4 grades. \n"; // Gathers number inputs to output GPA (Grade Point Average)
cout << "Enter first grade \n";
cin >> g1;
cout << "Enter second grade \n";
cin >> g2;
cout << "Enter third grade \n";
cin >> g3;
cout << "Enter fourth grade \n";
cin >> g4;
cout << "Your average is: \n";
const int gpa = ((g1 + g2 + g3 + g4) / 4.0); // Average of g1, g2, g3, and g4.
const string rep = "Your GPA letter grade is: \n"; // Placeholder for the letter grade reply string.
cout << gpa;
cout << gpa; // Outputs letter grade string
if (gpa >= 90) {
cout << rep << "A";
}else if (gpa >= 80){
cout << rep << "B";
}else if (gpa >= 70){
cout << rep << "C";
}else if (gpa >= 60){
cout << rep << "D";
}else if (gpa >= 0){
cout << rep << "F";
}else{
cout << "Invalid grade entered, enter positive digits only. \n";
}
system("pause");
return 0;
}
请检查您的语法。所有错误都是由语法错误引起的。