variable not being initialized. Help!

Hi i'm trying to make a program that reads three students grades and then finds there average then display their first and last names, their average and a letter grade. But for some reason when i go to debug the program i have it says "The variable 'score1' is being used without being initialized." And i don't know how to fix it.

#include <iostream>
#include <string>
#include <fstream> // Required for file streams
#include <cstdlib> // Required for exit statement
#include <iomanip> // For IO mainipulators
using namespace std; // Required for strings

ifstream in; // Creation of input file stream

int main ()
{
string firstName, lastName; // declaring strings
int score1, score2, score3, score4, score5, score6;
char grade;
double average;

in.open("grades.txt"); // Associates external file name
// with input file stream name


cout << fixed << showpoint << setprecision(2);


// Algorithim
average = (score1 + score2 + score3 + score4 + score5 + score6) / 6.00;

// Determine the letter grade
if (average < 69)
grade = 'F';
else if (average < 76)
grade = 'D';
else if (average < 84)
grade = 'C';
else if (average < 92)
grade = 'B';
else if (average > 92.4)
grade = 'A';

// Read and process first set of data
in >> firstName >> lastName >> score1, score2, score3, score4, score5, score6; // Reads from input file

// Write to the screen
cout << "Name: " << lastName << ", " << firstName << " " << average << grade << endl;

// Read and process second set of data
in >> firstName >> lastName >> score1, score2, score3, score4, score5, score6; // Reads from input file

// Write to the screen
cout << "Name: " << lastName << ", " << firstName << " " << average << grade << endl;

// Read and process third set of data
in >> firstName >> lastName >> score1, score2, score3, score4, score5, score6; // Reads from input file

// Write to screen
cout << "Name: " << lastName << ", " << firstName << " " << average << grade << endl;
in.close();

system ("pause");
return 0;
}

i would appreciate all the help i cant get.
the comma operation is wrong here, you need the operator>> like so:

in >> firstName >> lastName >> score1 >> score2 >> score3 >> score4 >> score5 >> score6; // Reads from input file

The same applies to the other lines alike.
Last edited on
Here is how to initialise a value.
int score1 = 0;

Here is another way.
1
2
int score1;
score1 = 0;


Here is another way.
1
2
int score1;
cin >>score1;


The warning is telling you that you are using score1 before giving it a value, here, average = (score1 + score2 + score3 + score4 + score5 + score6) / 6.00;, so it will contain whatever random number happens to be in that memory.
Last edited on
Oh, and you're indeed using the variables before the have values:
1
2
// Algorithim
average = (score1 + score2 + score3 + score4 + score5 + score6) / 6.00;
None of them have valid values. I think you want a function, but you can easily put that line after it "Reads from input file"
I know they don't have values, my program is supposed to read score1, score2, score3, etc from the input file, same with there names.

Edit: Is this right?

// Determine the letter grade
if (average < 69)
grade = 'F';
else if (average < 76)
grade = 'D';
else if (average < 84)
grade = 'C';
else if (average < 92)
grade = 'B';
else if (average > 92.4)
grade = 'A';
Last edited on
See my post above:

http://www.cplusplus.com/forum/beginner/81276/#msg436738

Edit:
Edit: Is this right?
Yes, but average has no useful value at that position
Last edited on
I know they don't have values, my program is supposed to read score1, score2, score3, etc from the input file,


You'll find you have more luck if you read values from the input file before you try to use them. It's very difficult in C++ to work with an input value before you have actually inputted it.
Last edited on
use an while loop
1
2
3
4
5
6
7
in>>read data;
while(!in.eof())
{
  process the variables
  cout<<statements<<endl;
  in>>read next data;
}

i stopped the variable not being initialized error, but i cant get the program to open or read my file.
Last edited on
Edit: Is this right?

// Determine the letter grade
if (average < 69)
grade = 'F';
else if (average < 76)
grade = 'D';
else if (average < 84)
grade = 'C';
else if (average < 92)
grade = 'B';
else if (average > 92.4)
grade = 'A';

grade will be empty if your average score is anywhere between 92 and 92.4 included

i stopped the variable not being initialized error, but i cant get the program to open or read my file.

How are you trying to execute your program? From DOS or an IDE?
It is possible that your program can't find the file because it is in the wrong directory
It was in the wrong directory, I got it working now. Thank all of you so much for helping me.
Topic archived. No new replies allowed.