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<<"******************************************************"<<endl;
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<<" "<<endl;

total=a+b+c+d+e+f;
cout<<" "<<endl;

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<<endl;
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<<"*";
}

cout<<endl;
}
system("PAUSE");                                                     //pauses to display program
return EXIT_SUCCESS;
}``````
2
Contributors
1
2
Views
8 Years
Discussion Span
Last Post by Murtan

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