我怎样才能添加&&在我的if else声明中

时间:2017-09-09 16:45:38

标签: c++

我正在创建一个评分程序,该程序可以输入三个变量,家庭作业,考试和背诵。然后打印出您的总分数和最终百分比。我正在尝试根据你的成绩打印出一个字符串,但是它出错了(预期的primary-expression之前)。如何添加&&在我的if else声明中。

    #include <iostream>
#include <iomanip>

using namespace std;

float homework = 150,
        exam = 250,
        recitation = 75;

int main(){
    float homeworkIn = 0,
        examIn = 0,
        recitationIn = 0,
        totalPointsPossible = 475;//beginning variables and their value

    cout << "Input your homework score out of 150: "; //prints to the console
    cin >> homeworkIn;// allows user to input the variables value
        if(homeworkIn > homework){
            cout << "\n\nYou entered to much. Please re-run the program and try again.";

            return 0;
        }

    cout << "Input your exam score out of 250: ";//prints to the console
    cin >> examIn;// allows user to input the variables value
     if(examIn > exam){
            cout << "\n\nYou entered to much. Please re-run the program and try again.";

            return 0;
        }

    cout << "Input your recitation score out of 75: "; //prints to the console
    cin >> recitationIn;// allows user to input the variables value
     if(recitationIn > recitation){
            cout << "\n\nYou entered to much. Please re-run the program and try again.";

            return 0;
        }

    float totalPoints = homeworkIn + examIn + recitationIn,
        finalPercent = totalPoints / totalPointsPossible;//defines how much total points and final percent variables are valued at

    cout << "\nYour total amount of points is " << totalPoints << ":" << totalPointsPossible << endl;//prints to the console

        cout << "\n\nYour total percentage is " << finalPercent * 100 << "%" << endl;//prints to the console

        if(finalPercent <= .59){
            cout << "Your final grade is an F. You have failed the class.";
        }else{
            if(finalPercent >= .60 && <= .69){
                cout << "Your final grade is a D.";
            }else{
                if(finalPercent >= .70 && <= .79){
                    cout << "Your final grade is a C.";
                }else{
                    if(finalPercent >= .80 && <= .89){
                        cout << "Congratulations! Your final grade is a B.";
                    }else{
                        if(finalPercent >= .90 && < .94){
                             cout << "Congratulations! Your final grade is a -A.";
                        }else{
                            if(finalPercent >= .94){
                                cout << "Congratulations! Your final grade is a A.";
                            }
                        }
                    }
                }
            }
        }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

如果您正在使用&amp;&amp;然后,在两种情况下,运算符都要提供已进行比较的变量。因此条件的变化将是if(finalPercent >= .60 && finalPercent <= .69)

如需更多理解,请查看以下代码块

if(finalPercent <= .59){
        cout << "Your final grade is an F. You have failed the class.";
    }else{
        if(finalPercent >= .60 && finalPercent <= .69){ // Change is made in this line
            cout << "Your final grade is a D.";
        }
    }