我正在写一本GPA计算器。我在OR(||)函数中遇到问题,检查信用输入是否正确(如果不正确,则应再次询问)。此外,当成绩的输入是" I"或" W"程序不应该使用该信息(它不应该对它做任何事情)。 谢谢你的帮助。 这是代码:
// Include Statements
#include <iostream>
using namespace std;
//Program to calculate GPA
int main()
{
// Program made by Jose Luis Landivar
// Fall 2016
cout << ""<< endl;
cout << "The following program will calculate your GPA "<< endl<<'\n';
char grade;
int g = 1;
//variables for the algebraic operations needed
int credit = 0;
int totalcredit = 0;
int ptotal = 0;
double gpa, p = 1;
double count = 1;
double nclass;
string coursename, coursenumber;
cout << "Please enter the number of classes for the GPA calculator"<< endl;
cin >> nclass;
cout << ""<< endl;
while (count <= nclass ) // While
{
//Information for the class
cout << "Please enter the course name for the class # "<< count << endl;
cin >> coursename;
cout << "Please enter the course number for the class # "<< count << endl;
cin >> coursenumber;
//Getting grades and credits from user
cout << "Enter the grade for the class # "<< count <<" (A,B,C,D,F,I,W)"<< endl;
cin >> grade;
if (grade == 'A' || grade == 'B' || grade == 'C' || grade == 'D' || grade == 'F' || grade == 'I' || grade == 'W')
{
// If for grades
if (grade == 'A')
{
g = 4;
cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl;
cin >> credit;
cout << ""<< endl;
if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){
p = g * credit;
totalcredit = totalcredit + credit;
ptotal = ptotal + p;
}
else {cout << "Input is incorrect\n";
}
}
if (grade == 'B')
{
g = 3;
cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl;
cin >> credit;
cout << ""<< endl;
if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){
p = g * credit;
totalcredit = totalcredit + credit;
ptotal = ptotal + p;
}
else {cout << "Input is incorrect\n";
}
}
if (grade == 'C')
{
g = 2;
cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl;
cin >> credit;
cout << ""<< endl;
if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){
p = g * credit;
totalcredit = totalcredit + credit;
ptotal = ptotal + p;
}
else {cout << "Input is incorrect\n";
}
}
if (grade == 'D')
{
g = 1;
cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl;
cin >> credit;
cout << ""<< endl;
if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){
p = g * credit;
totalcredit = totalcredit + credit;
ptotal = ptotal + p;
}
else {cout << "Input is incorrect\n";
}
}
if (grade == 'F')
{
g = 0;
cout << "Enter the number of credits for the class # "<< count <<" (1,2,3,4,5)"<< endl;
cin >> credit;
cout << ""<< endl;
if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){
p = g * credit;
totalcredit = totalcredit + credit;
ptotal = ptotal + p;
}
else {cout << "Input is incorrect\n";
}
}
count = count + 1;
}
else {cout << "Input is incorrect\n";
}
}
//algebraic formulas
gpa = ptotal/totalcredit;
//output results
cout << "Your total number of credits is: " << totalcredit << endl;
cout << "Your GPA is: " << gpa << endl;
if (grade == 'I')
{
}
if (grade == 'W')
{
}
return 0;
}
答案 0 :(得分:3)
将int
与char
进行比较时要小心。例如:
int credit = 3;
// THIS WILL FAIL
if (credit == '3') {
}
3的int和'3'的char不相等。虽然您可以比较char和int,但它们没有相同的值。例如
cout << "Int : " << int(3) << std::endl;
cout << "Char: " << int('3') << std::endl;
给出输出:
Int : 3
Char: 51
你应该改变
的每一次出现if (credit == '5' || credit == '4' || credit == '3' || credit == '2' || grade == '1'){
// ^^^ is this suppose to be "credit"?
到
if (credit == 5 || credit == 4 || credit == 3 || credit == 2 || credit == 1){
<强>更新强>
请注意,当提供“I”或“W”时,您的代码实际上从不执行任何操作(除了增加课程count
变量之外)。但是,当 EVERY 当然是“我”或“W”时(即所有课程都是(I)ncomplete或(W)ithdrawl)那么你的totalcredit
变量将等于0给出你是'除以0'错误。变化
//algebraic formulas
gpa = ptotal/totalcredit;
到
if (totalcredit > 0) {
gpa = ptotal/totalcredit;
} else {
gpa = 0;
}
else语句可能不是必需的(gpa
已经为零),但是当没有有效的信用来计算GPA时,它会明确显示你想要GPA的内容。