Hi everyone

This code works fine but the asterisks that print to represent the percentage is wrong. The example given was there was a total of 14 grades across the A-F range, but "A" had 1 and 1 should represent 4 stars one the chart.

Whats wrong????

#include <cstdlib>
#include <iostream>
#include <iomanip>
int a,Acent,b,Bcent,c,Ccent,Dcent,d,Ecent,e,Fcent,f,total,percent;                                 //DECLARING 

using namespace std;

int main(int argc, char *argv[])
cout<<"This program will present the number of grades"<<endl;             //INTRODUCTION
cout<<"you input for grades A-F and turn this information"<<endl;         //TO
cout<<"in to a bar graph for ease."<<endl;                                //PROGRAM
cout<<" "<<endl;
    cout<<"Please enter how many grade A's"<<endl; cin>>a;                //OBTAIN'S GRADE A
    cout<<"Please enter how many grade B's"<<endl; cin>>b;                // "        "    B
    cout<<"Please enter how many grade C's"<<endl; cin>>c;                //OBTAIN'S  "    C
    cout<<"Please enter how many grade D's"<<endl; cin>>d;                //"         "    D
    cout<<"Please enter how many grade E's"<<endl; cin>>e;                //OBTAIN'S  "    E
    cout<<"Please enter how many grade F's"<<endl; cin>>f;                //OBTAIN'S  "    F
    percent=100/total;                                                    //TOTALS THE GRADES
    cout<<" "<<endl;
    cout<<"      TOTAL GRADES ENTERED - "<<total<<endl;                   //PRINTS TOTAL GRADES
    Acent=percent*a/2;  Bcent=percent*b/2;  Ccent=percent*c/2;            //calculates grade as percentage 
    Dcent=percent*d/2;  Ecent=percent*e/2;  Fcent=percent*f/2;            //"
    cout<<"      0   10   20   30   40   50   60   70   80   90   100"<<endl;
    cout<<"      |   |    |    |    |    |    |    |    |    |    |"<<endl;
    cout<<"      **************************************************"<<endl;
    int array[6] = {Acent,Bcent,Ccent,Dcent,Ecent,Fcent};                                  //stores grades as percentage
    char array2[6] = {'A','B','C','D','E','F'};                          //stores letters a-f
    for(int loopcounter=0; loopcounter<6; loopcounter++)                 //performs loop for generating asterisks
    {                         for(int asteriskcounter=0; asteriskcounter<6; asteriskcounter++)
             cout<<" ";
             for(int asteriskcounter=0; asteriskcounter<array[loopcounter]; asteriskcounter++)
              cout<<setw(3)<<"   GRADE "<<array2[loopcounter]<<endl;      //prints grade after asterisks

    system("PAUSE");                                                     //pauses to display program
    return EXIT_SUCCESS;

Given your input data (14 grades, one of which was an A)
I would have expected 3 * for A

percent = 100 / 14; // 7 in integer math
Acent = percent * a / 2; // 3 in integer math

Do the other grades display the number of stars you expect?

Question: What's the setw(3) on line 50 supposed to do?

Arbitrary personal style comment:
I personally don't like artificially large loop variable names, and I don't like loops just to loop. For the loop starting on line 39 I would have used something like "gradeidx" instead of "loopcounter". The name has more meaning and is shorter. I wouldn't loop a constant number of times to output spaces...just output the spaces. For the inner loop where you use "asteriskcounter" I would have used something like "ii".

So just to demonstrate the difference:

// loop for each grade
    for (int gradeidx = 0; gradeidx < 6; gradeidx++)
        cout<<"     ";
        // loop to output each asterisk
        for (int ii = 0; ii < array[gradeidx]; ii++)
            cout << "*";
        // print the grade after the asterisks
        cout << "   GRADE " << array2[gradeidx] << endl;
This article has been dead for over six months. Start a new discussion instead.