if语句在C ++代码百分比

时间:2018-03-08 03:14:42

标签: c++ if-statement percentage

我遇到了百分比公式无效的问题

#include <iostream>
using namespace std;

int main() {
    int tmarks,intermarks, passmarks;

    float per;

    cout << "Enter Your Inter Marks:\n";
    cin >> intermarks;
    cout << "Enter Your Total Marks:\n";
    cin >> tmarks;
    cout << "Enter Your PassMarks:\n";
    cin >> passmarks;
    per = (intermarks/tmarks) * 100;
    cout << "percentage:" << per;

    if (per >= 45 && passmarks >= 50) {
        cout << "Welcome To Uni\n";
    } else {
        cout << "Improve Your Marks You are eligible\n";
    }

}

1 个答案:

答案 0 :(得分:1)

如果intermarks = 50tmarks = 75,则intermarks/tmarks将为0。因为两者都是整数。你需要在除法运算之前进行类型转换。这种方式float(intermarks) / float(tmarks)将为0.67per将为67