the char character for grade are coming up with weird ascii like characters instead of the letter grades. Please help!

//declaration

string first, last;
int prog1, prog2,prog3,prog4, prog5, test1, test2;
int totalpts, progavg, testavg, courseavg;
char grade;

//executables
totalpts = (prog1+prog2+prog3+prog4+prog5+test1+test2);
progavg = (prog1+prog2+prog3+prog4+prog5)/5;
testavg = (test1+test2)/2;
courseavg = (progavg+testavg)/2;

if (courseavg >=90 )grade=='A';
else if (courseavg <= 89 )grade=='B';
else if (courseavg <=79 )grade=='C';
else grade=='F';

cout<<left<<setw(20)<<"Student Name"<<setw(10)<<"Total"<<setw(10)<<"Program"<<setw(10)<<"Test"<<setw(10)<<"Course"<<setw(10)<<"Grade"<<endl;
cout<<right<<setw(26)<<"Points"<<setw(11)<<"Average"<<setw(10)<<"Average"<<setw(10)<<"Average"<<endl;

Instead of showing up the letter grades, everytime the program run, it will come up with new ascii like weird character. Any help would be much appreciated! Thanks

Dani AI

Generated

Quick diagnosis: those “weird ASCII” characters almost always mean the char being printed never held a valid letter. That can happen two ways here — the grade variable was never assigned (left with indeterminate data) or the conditional logic let execution skip assignment. The operator == is a comparison and does not store a value; use the assignment operator = when you intend to set grade. Also make sure the if/else chain tests ranges in descending order (for example: 90+, then 80–89, then 60–79, else fail) so a middle score doesn't get caught by the wrong branch.

File I/O and loop robustness: using while(fin) and then extracting inside the loop can let a failed extraction slip through and leave previous values unchanged. Prefer doing the extraction in the loop condition so the body runs only for successfully read records (and check the stream state). Initialize numeric variables and set a default value for grade at the start of each iteration so a partial read or logic path cannot leave it indeterminate.

Other practical points: integer types truncate averages — use floating point (or cast) if fractional averages are needed; setprecision affects floating output, not integer printing. If student names may contain spaces, read the whole name (or use a CSV-style parser) rather than assuming two space-separated tokens. Replace fragile system("pause") and ad-hoc peek() handling with clearer control flow.

Quick checklist

  1. Initialize variables or set a default grade at loop start.
  2. Use = to assign the letter, not ==.
  3. Test ranges in the correct order (>=90, else if >=80, else if >=60, else).
  4. Read records with extraction in the loop condition and check success.
  5. Use floating-point for averages when precision matters and consider a small helper function to return the grade for a given numeric score.

This addresses the root causes and were hinting at and should stop the garbage characters from appearing.

Recommended Answers

All 10 Replies

a lot of uninitialized variables O.o

a lot of uninitialized variables O.o

I even tried this
courseavg = (prog1+prog2+prog3+prog4+prog5+test1+test2)/7;

but.. still got weird characters for grade.
Everything else works just fine as far as it goes with showing up the right numbers except the grade letter!

well..don't you need to ask the user to input numbers so the program can calculate the result? Right now it looks as though your just trying to add words..

hint :

int x = 0, y = 0;
int t = x + y;

is not the same as

int x, y;
int t = x + y

well..don't you need to ask the user to input numbers so the program can calculate the result? Right now it looks as though your just trying to add words..

No. This is a fin fout. I have all the program and test score information saved on a notepad file where this program will be reading it from and will output the result on both screen and another output notepad file.
The program works just fine except for the grade letter.

hint :

int x = 0, y = 0;
int t = x + y;

is not the same as

int x, y;
int t = x + y

I am not following you if you don't mind explaining where you see an error please.

I am not following you if you don't mind explaining where you see an error please.

I think he just talking about initializing that variables. Anyway...could you include the resource file that you are using, so I can accurately test out the program? :)

As well, all of the code would be helpful, even the heading files. (yes I know that they are <iomanip>, <iostream>, <string>, etc. But it may help to just include them anyway.)

I think he just talking about initializing that variables. Anyway...could you include the resource file that you are using, so I can accurately test out the program? :)

As well, all of the code would be helpful, even the heading files. (yes I know that they are <iomanip>, <iostream>, <string>, etc. But it may help to just include them anyway.)

#include<iostream>
#include<fstream>
#include<iomanip> 
using namespace std;

int main(){

    string first, last;
    int prog1, prog2,prog3,prog4, prog5, test1, test2;
    int totalpts, progavg, testavg, courseavg;
    char grade;

    ifstream fin;
    ofstream fout;

    fin.open("students.txt");
    fout.open("output.txt");

    if(!fin){
             cout<<"Input Failure"<<endl;
             system("pause");
             return 1;
             } //end of error check
    if(!fout){
              cout<<"Output Failure"<<endl;
              system("pause");
              return 1;
}

    cout<<"Press any key to read input and calculate the grades of all students"<<endl;

    cin.get(); //waits for the user to press any key 

    cout<<"1234567890123456789012345678901234567890123456789012345678901234567890"<<endl;
    cout<<"======================================================================"<<endl;          
    cout<<left<<setw(20)<<"Student Name"<<setw(10)<<"Total"<<setw(10)<<"Program"<<setw(10)<<"Test"<<setw(10)<<"Course"<<setw(10)<<"Grade"<<endl;
    cout<<right<<setw(26)<<"Points"<<setw(11)<<"Average"<<setw(10)<<"Average"<<setw(10)<<"Average"<<endl;
    cout<<"----------------------------------------------------------------------"<<endl;

    fout<<"======================================================================"<<endl;          
    fout<<left<<setw(20)<<"Student Name"<<setw(10)<<"Total"<<setw(10)<<"Program"<<setw(10)<<"Test"<<setw(10)<<"Course"<<setw(10)<<"Grade"<<endl;
    fout<<right<<setw(26)<<"Points"<<setw(11)<<"Average"<<setw(10)<<"Average"<<setw(10)<<"Average"<<endl;
    fout<<"----------------------------------------------------------------------"<<endl;                            

    while(fin){
    fin>>first>>last>>prog1>>prog2>>prog3>>prog4>>prog5>>test1>>test2;

    totalpts = (prog1+prog2+prog3+prog4+prog5+test1+test2);
    progavg = (prog1+prog2+prog3+prog4+prog5)/5;
    testavg = (test1+test2)/2;
    courseavg = (progavg+testavg)/2;

    if (courseavg >=90 )grade='A';
    else if (courseavg <= 89 )grade='B';
    else if (courseavg <=79 )grade='C';
    else grade='F';

    cout<<fixed<<showpoint<<setprecision(2);
    cout<<left<<setw(20)<<first+" "+last<<setw(10)<<totalpts<<setw(10)<<progavg<<setw(10)<<testavg<<setw(10)<<courseavg<<setw(10)<<grade<<endl;
    fout<<left<<setw(20)<<first+" "+last<<setw(10)<<totalpts<<setw(10)<<progavg<<setw(10)<<testavg<<setw(10)<<courseavg<<setw(10)<<grade<<endl;
    if(fin.peek()=='\n')fin.ignore();

    }//end of fin controlled while
    cout<<"======================================================================"<<endl;
    fout<<"======================================================================"<<endl;
    fin.close();
    fout.close(); 

    system("pause");
    return 0;
}//end of main

Ensure the following data is saved as a txt file to the same folder where the compiler file is located:

John Doe 95 89 94 88 98 92 90
Jack Black 67 77 72 88 86 89 85
Dan Snyder 88 65 78 88 86 85 93
Sarah Peets 94 78 89 98 93 81 87
David McGregor 76 86 93 91 75 90 76
John Anderson 89 92 95 66 58 84 90
Robert Blake 85 93 84 92 90 75 94

like try changing Robert Blake's numbers to even lower values
65 93 64 92 60 65 94

then the course avg comes out 76 but the letter grade still shows B

so i'm sure == is used instead of = because we don't want to assign the grade to letters but simply state that it is equal to the letters.
Only problem is the letter are showing in weird characters.. and I can't for the sake of my life figure it out!! :P

hmm.. it seems to work by putting in and statement.
if (courseavg >=90 )grade='A';
else if (courseavg >= 80 && courseavg <= 89)grade='B';
else if (courseavg >= 60 && courseavg <= 79)grade='C';
else grade='F';

but still I am little hesitant on using = instead of ==
:/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.