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
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.
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.
// 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