ike
ike

Reputation: 45

why is the set width not working properly in c++ for me

On line 68 I have the set width set to change accordingly to the width of the name if the name has 6 letter it will show 9 space for it. I cant seem to get it to act properly it sets the width differently for different names.

Here is how it currently prints

Name     Quiz ScoreProject ScoreTest Score  TotalGrade
food       25.00  30.00  89.00 144.00 C
imfreakingawesome  22.00  30.00  79.00 131.00 D
thisiscool  22.00  30.00  89.00 141.00 C
eatfood    22.00  30.00  99.00 151.00 C
codeit     22.00  29.00  99.00 150.00 C

This is how I need it to print

Name              Quiz ScoreProject ScoreTest Score  TotalGrade<--disregard line
food              25.00  30.00  89.00 144.00 C
imfreakingawesome  22.00  30.00  79.00 131.00 D
thisiscool        22.00  30.00  89.00 141.00 C
eatfood           22.00  30.00  99.00 151.00 C
codeit            22.00  29.00  99.00 150.00 C

There are stars next to the lines where i set it

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
void calculateTotal (double qS[], double pS[], double tS[], double total[], int SIZE);
void calculateGrade (double total[], double grade[], int SIZE);
void generateReport (string names[], double qS[], double pS[], double tS[], double total[], char grade[], int SIZE);

double totalScore[5];
char finalGrade[5];
int nameWidth;

int main()
{

    string studentName[5];
    double quizScore[5];
    double projectScore[5];
    double testScore[5];
    double grade[5]; //dont understand the need for this
    double total[5];
    int lastwidth=0;

    for(int i=0;i<5;i++)
    {
        cout<<"Enter student's name"<<endl;
        cin>>studentName[i];

        cout<<"Enter student's quiz score"<<endl;
        cin>>quizScore[i];
        while(quizScore[i]>25||quizScore[i]<0)
        {
            cout<<"Enter a quiz score between 0 and 25"<<endl;
            cin>>quizScore[i];
        }

        cout<<"Enter student's project score"<<endl;
        cin>>projectScore[i];
        while(projectScore[i]>30||projectScore[i]<0)
        {
            cout<<"Enter a project score between 0 and 30"<<endl;
            cin>>projectScore[i];
        }

        cout<<"Enter student's test score"<<endl;
        cin>>testScore[i];
        while(testScore[i]>100||testScore[i]<0)
        {
            cout<<"Enter a test score between 0 and 100"<<endl;
            cin>>testScore[i];
        }
    }

    for(int i=0;i<5;i++)
        calculateTotal(quizScore, projectScore,testScore,total,i);

    for(int i=0;i<5;i++)
        calculateGrade (totalScore, grade,i);

    for(int i=0;i<5;i++)        
        if(studentName[i].length()+3>lastwidth)
            nameWidth=(studentName[i].length()+3);

    // The problem lies here    
    cout<<left<<setw(nameWidth)<<"Name"<<setw(7)<<right<<fixed<<"Quiz Score"<<setw(7)<<right<<"Project Score"<<setw(7)<<right<<"Test Score"<<setw(7)<<right<<"Total"<<setw(5)<<right<<"Grade"<<endl;

    for(int i=0;i<5;i++)
    generateReport(studentName,quizScore,projectScore,testScore,totalScore,finalGrade,i);                                   
}

// And here lies the problem, too
void generateReport (string names[], double qS[], double pS[], double tS[], double total[], char grade[], int SIZE)
{ //width of 7 is better
    cout<<left<<setw(nameWidth)<<names[SIZE]<<setw(7)<<right<<fixed <<setprecision(2)<<qS[SIZE]<<setw(7)<<right<<pS[SIZE]<<setw(7)<<right<<tS[SIZE]<<setw(7)<<right<<total[SIZE]<<setw(2)<<right<<grade[SIZE]<<endl;
}

void calculateTotal (double qS[], double pS[], double tS[], double total[], int SIZE)
{
    totalScore[SIZE]=qS[SIZE]+pS[SIZE]+tS[SIZE];
}

void calculateGrade (double total[], double grade[], int SIZE)
{
    grade[SIZE]=total[SIZE]/200;
    if (grade[SIZE] >=.90)
        finalGrade[SIZE]='A';
    else if(grade[SIZE] >=.80)
        finalGrade[SIZE]='B';
    else if(grade[SIZE] >=.70)
        finalGrade[SIZE]='C';       
    else if(grade[SIZE] >=.60)
        finalGrade[SIZE]='D';
    else if(grade[SIZE] <=.60)      
        finalGrade[SIZE]='F';
}

Upvotes: 0

Views: 401

Answers (1)

James Adkison
James Adkison

Reputation: 9602

int nameWidth;

for (int i = 0; i < 5; i++)
{
    if (studentName[i].length() + 3 > lastwidth)
    {
        nameWidth = (studentName[i].length() + 3);
    }
}

You're checking if the length of the current name is longer than the lastwidth and if so you update nameWidth but you're not updating lastWidth (which is always zero).

This effectively leaves you with the length of the last name in the list plus three.

Why not the following?

// Remove the lastwidth variable completely
std::size_t maxNameSize = 0;

for (int i = 0; i < 5; ++i)
{
    maxNameSize = std::max(maxNameSize, studentName[i].size() + 3);
}

Note: since this is C++ I'd avoid the C-style arrays and use standard containers (e.g., std::vector<std::string> instead of std::string var[5]).

Upvotes: 1

Related Questions