i have a prob wit this code...it says variable letter is being used without being initialized...can anione tell me how n where should i initialize it...plz...m desperately in need of help...i m blank...m jst a beginner in c++...

the c++ code is...

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
void calcAvg(ifstream& in_file, ofstream& out_file, float& avg);
char calc_grade(float avg);
int main() {

    ifstream inp;
    ofstream outp;
    inp.open("C:\\Users\\Q-mar\\Desktop\\grades.txt" );
    outp.open("C:\\Users\\Q-mar\\Desktop\\grades.txt" );
    outp << "Student    Test1    Test2    Test3    Test4    Test5    Average    Grade";
    string name;
    float classAvg = 0;
    char grade;
    int k;
    float avg;
    for (k=1; k<=10; k++)
    {
        inp >> name;
        outp << setw(10) << name;
        calcAvg(inp, outp, avg);
        outp << setw(6) << avg;
        classAvg = classAvg + avg;
        grade = calc_grade(avg);
        outp << setw(5) << " " << grade << endl;
    }
    classAvg = classAvg / 10;
    outp << endl << "Class Average = " << classAvg;
    return 0;
}
void calcAvg(ifstream& inp, ofstream& outp, float& avg)
{
    int score, k;
    float sum = 0.0;
    for (k=1; k<=5; k++)
    {
        inp >> score;
        outp << setw(4) << score << " ";
        sum = sum + score;
    }
    avg = sum / 5;
}
char calc_grade(float total)
{
    char letter;
    if (total >= 0)
         letter = 'F';

    else if (total >= 60)
            letter = 'D';

        else if (total >= 70)
                letter = 'C';

                else if (total >= 80)
                    letter = 'B';

                    else if (total >= 90)
                        letter = 'A';

                    else if (total > 100)
                            cout << "Grade can not exceed 100" << endl;
                    else
                        cout << "Grade can not be a negative number" << endl;
    return letter;
}

the .txt file content is...

Johnson 85 83 77 91 76 
Aniston 80 90 95 93 48 
Cooper 78 81 11 90 73 
Gupta 92 83 30 69 87 
Blair 23 45 96 38 59 
Clark 60 85 45 39 67 
Kennedy 77 31 52 74 83 
Bronson 93 94 89 77 98 
Sunny 79 85 28 93 82 
Smith 85 72 49 75 63

Please use code tags when posting code.

To answer your question: Change char letter; to char letter = 'F'; That will initialize the letter variable to some known value and ensure that non-random data is not used if nothing else is assigned. In your case, if total is less than 0 or greater than 100 what does the variable letter contain?

Please use code tags when posting code.

To answer your question: Change char letter; to char letter = 'F'; That will initialize the letter variable to some known value and ensure that non-random data is not used if nothing else is assigned. In your case, if total is less than 0 or greater than 100 what does the variable letter contain?

if the total is less than 0,it says grade cannot be a negative number...if the total is greater than 100,it says grade cannot exceed 100

Please use code tags when posting code.

To answer your question: Change char letter; to char letter = 'F'; That will initialize the letter variable to some known value and ensure that non-random data is not used if nothing else is assigned. In your case, if total is less than 0 or greater than 100 what does the variable letter contain?

I usually initialize chars to the NULL char. That way, if I have an accidental output, the output won't be a valid answer and will hopefully flag a problem.

char someChar = '\0';

I usually initialize chars to the NULL char. That way, if I have an accidental output, the output won't be a valid answer and will hopefully flag a problem.

char someChar = '\0';

in which line i should identify char someChar = '\0';

You don't. It was just an example. Besides, you didn't use [code] ...code tags... [/code], so there aren't any line numbers to reference.

Take the concept demonstrated in the example and apply it to your code yourself. Where are your char declarations?

You don't. It was just an example. Besides, you didn't use [code] ...code tags... [/code], so there aren't any line numbers to reference.

Take the concept demonstrated in the example and apply it to your code yourself. Where are your char declarations?

my char declarations is char letter; coz i use letters such as A,B,C,D,F

Apologies if this is slightly off-topic, but the initialisation of the char isn't really the biggest problem you've got. The real problem with this code lies in this block of code (which I've enclosed in CODE tags and indented for you!):

char calc_grade(float total)
{
	char letter='\0'; // this is what the others are on about, this was not initialised
	if (total >= 0)   // However, the logic in the rest of this function is severely flawed!
		letter = 'F';

	else if (total >= 60)
		letter = 'D';

	else if (total >= 70)
		letter = 'C';

	else if (total >= 80)
		letter = 'B';

	else if (total >= 90)
		letter = 'A';

	else if (total > 100)
		cout << "Grade can not exceed 100" << endl;
	else
		cout << "Grade can not be a negative number" << endl;
	return letter; // If a negative number was passed in, in your original code, it's possible that an uninitialised value could be returned here!!
}

Consider this:
Your calc_grade function is called passing the value 85.5
So the value of total will be 85.5.
Now we go into your series of if statements and the very first condition is:
If total is greater than or equal to 0, letter is set to 'F'.
As total is 85.5 and is therefore greater than 0, letter is set to F.
In other words, whenever a value greater than 0 is passed-in, your function will award that person an F grade regardless of their score!
You could change the condition of the first if statement to:

if (total <60)

But then the 'else if' statements in this block will also cause a problem.
With the first if modified as mentioned above, with a value of 85.5 the first 'if' will now be skipped. Scores of less than 60 will corectly get an F but ANY scores over 60 will result in a grade of 'D' being issued, regardless of their score.

So you need to rethink the entire function. Instead of checking for a single condition, you might want to consider checking a range of values.
e.g. in pseudo-code
if total < 60 then give grade F
else if total >=60 and total <70 then give grade D
else if total >=70 and total <80 then give grade C
etc.

Hope this is of some help!

Apologies if this is slightly off-topic, but the initialisation of the char isn't really the biggest problem you've got. The real problem with this code lies in this block of code (which I've enclosed in CODE tags and indented for you!):

char calc_grade(float total)
{
	char letter;
	if (total >= 0)
		letter = 'F';

	else if (total >= 60)
		letter = 'D';

	else if (total >= 70)
		letter = 'C';

	else if (total >= 80)
		letter = 'B';

	else if (total >= 90)
		letter = 'A';

	else if (total > 100)
		cout << "Grade can not exceed 100" << endl;
	else
		cout << "Grade can not be a negative number" << endl;
	return letter;
}

Consider this:
Your calc_grade function is called passing the value 85.5
So the value of total will be 85.5.
Now we go into your series of if statements and the very first condition is:
If total is greater than or equal to 0, letter is set to 'F'.
As total is 85.5 and is therefore greater than 0, letter is set to F.
In other words, whenever a value greater than 0 is passed-in, your function will award that person an F grade regardless of their score!
You could change the condition of the first if statement to:

if (total <60)

But then the 'else if' statements in this block will also cause a problem.
With the first if modified as mentioned above, with a value of 85.5 the first 'if' will now be skipped. Scores of less than 60 will corectly get an F but ANY scores over 60 will result in a grade of 'D' being issued, regardless of their score.

So you need to rethink the entire function. Instead of checking for a single condition, you might want to consider checking a range of values.
e.g. in pseudo-code
if total < 60
give grade F
else if total >=60 and total <70
give grade D
else if total >=70 and total <80
give grade C
etc.

Hope this is of some help!

will try bro...tanks...reli do appreciate it...

will try bro...tanks...reli do appreciate it...

bro...if u dnt mind...try compile n run the program...the program automatically quits...seriously have no idea y is it so...could u help me out wit it...plz...

I'm assuming that you're just seeing a console window pop up and then disappear. If that's the case, then that's just the way it goes, there's absolutely no problem there!

You could put some code at the end of your program to make it wait for the user to press a key. However, as your program only writes to files and doesn't output anything to the screen I don't imagine that there's any point in pausing the program at the end.

Assuming that you're using Visual studio, you could try pressing ctrl-F5 to run the program without debugging, that usually prompts you to 'press any key to continue' at the end of a program. But again, as your program currently produces no output to the screen is there any point?

I'm assuming that you're just seeing a console window pop up and then disappear. If that's the case, then that's just the way it goes, there's absolutely no problem there!

You could put some code at the end of your program to make it wait for the user to press a key. However, as your program only writes to files and doesn't output anything to the screen I don't imagine that there's any point in pausing the program at the end.

Assuming that you're using Visual studio, you could try pressing ctrl-F5 to run the program without debugging, that usually prompts you to 'press any key to continue' at the end of a program. But again, as your program currently produces no output to the screen is there any point?

wat u say is true bro...i even tried system ("pause")...still didnt work...reli hope u cn check on my code n help me out on wat i should do so tat could complete up tis program of mine bro...plz...

You don't want to be using system("pause") anyway. That's another big no no that I won't go into here!

At the end of main, you could use something like a call to cin.get or std::getline before the return statement.

EDIT: You might also want to consider outputting a message telling the user that the program has finished or to 'press return to continue', otherwise the user (your teacher?) will probably think that your program is hanging!

So you need to rethink the entire function. Instead of checking for a single condition, you might want to consider checking a range of values.
e.g. in pseudo-code
if total < 60 then give grade F
else if total >=60 and total <70 then give grade D
else if total >=70 and total <80 then give grade C
etc.

Another option is to flip the function over and evaluate the higher grades first. This way, only one conditional evaluation is needed instead of 2 and the code becomes more efficient.

Another option is to flip the function over and evaluate the higher grades first. This way, only one conditional evaluation is needed instead of 2 and the code becomes more efficient.

Good point!

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.