a problem with my coding

So I am just starting to learn C++ and I was asked to make the following program

The program is suppose to reads in letter grades for a class of any size and displays the number of students who passed (D or better) and the number who failed (E or F).

however my codes produce 0.00 as the percent pass or fail I think there might be a problem in my declaration, or the type I used, it would be great help if anyone can solve this



   
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    char grade, sentinel = 'X';
    int numberofpass = 0, numberoffail = 0, numberoftotal = 0;
    double percentpass = 0, percentfail = 0;
    
    cout << sentinel << " to quit!" << endl;
    cout << "Enter a Grade: ";
    cin >> grade;
    cout << endl;

    while (grade != sentinel)
    {
          if ((grade == 'A')||(grade == 'B')||(grade == 'C')||(grade == 'D'))
          {
             numberofpass++;
             numberoftotal++;
          }
          else if ((grade == 'E')||(grade== 'F'))
          {
             numberoffail++;
             numberoftotal++;
          }
          else
          {
          }
          
    cout << "Enter a Grade: ";
    cin >> grade;
    cout << endl;
    }
    
    cout.setf(ios::fixed);
    cout.precision(2);
    
    percentpass = ((numberofpass / numberoftotal) * 100);
    percentfail = ((numberoffail / numberoftotal) * 100);
    
    cout << numberofpass << " students passed: " << percentpass << endl;
    cout << numberoffail << " students failed: " << percentfail << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Last edited on
1
2
percentpass = ((numberofpass / numberoftotal) * 100);
percentfail = ((numberoffail / numberoftotal) * 100);


numberofpass/numberoftotal does integer division, which will give you 0. You need to cast one of them to a double to make it do floating point arithmetic.
lol I feel stupid now, I knew it was something simple as declaring the wrong type..

but anyways thx for the help
Topic archived. No new replies allowed.