#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
int main ()
{
string name;
int number_of_Students = 4,
grade = 0;
double average= 0;
ofstream outputFile;
outputFile.open("avegrade.txt");
for (int i = 1; i <= number_of_Students; i++)
{
cout << "Enter a name for student #" << i << " ";
getline (cin, name);
cin.ignore();
outputFile << name << endl;
//write the name to the text file
int number_of_Grades = 3;
for (int j = 1; j <= number_of_Grades; j++)
{
cout << "Enter a grade #" << j << " ";
cin >> grade;
while (grade > 100 || grade < 1)
{
cout << "Invalid: Please enter a number between 1 & 100!" << endl;
cin >> grade;
}
//add the grade to the average
cout << setprecision(1) << fixed;
average = average + grade;
average = average / 3;
outputFile << average << endl;
}
//calculate the average and write to file. divide by 3
}
outputFile.close();
return 0;
}
Should I be initializing variables in main or the function as well?
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
ofstream outputFile;
char grd;
int number_of_Students = 4,
grade = 0;
float average= 0;
string name;
float gradeloopFunction()
{
for (int i = 1; i <= number_of_Students; i++)
{
cout << "Enter a name for student #" << i << " ";
getline (cin, name);
cin.ignore();
outputFile << name << endl;
}
//write the name to the text file
int number_of_Grades = 3;
for (int j = 1; j <= number_of_Grades; j++)
{
cout << "Enter a grade #" << j << " ";
cin >> grade;
while (grade > 100 || grade < 1)
{
cout << "Invalid: Please enter a number between 1 & 100!" << endl;
cin >> grade;
}
//add the grade to the average
cout << setprecision(1) << fixed;
average = average + grade;
average = average / 3;
return average;
}
}
char averageloopFunction()
{
if(average >= 90)
{
grd = 'A';
}
if(average >= 80; average < 90)
{
grd = 'B';
}
if(average >= 70; average < 80)
{
grd = 'C';
}
else(average < 70)
{
grd = 'F';
}
return grd;
}
int main()
{
outputFile.open("avegrade.txt");
gradeloopFunction();
averageloopFunction(average);
outputFile << average << grd << endl;
outputFile.close();
return 0;
}
im getting these errors
1 2 3 4 5 6 7 8 9 10 11 12 13
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(57): error C2143: syntax error : missing ')' before ';'
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(57): warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(57): error C2059: syntax error : ')'
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(58): error C2143: syntax error : missing ';' before '{'
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(58): warning C4552: '<' : operator has no effect; expected operator with side-effect
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(61): error C2143: syntax error : missing ')' before ';'
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(61): warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(61): error C2059: syntax error : ')'
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(62): error C2143: syntax error : missing ';' before '{'
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(62): warning C4552: '<' : operator has no effect; expected operator with side-effect
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(65): error C2181: illegal else without matching if
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(66): error C2143: syntax error : missing ';' before '{'
1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(76): error C2660: 'averageloopFunction' : function does not take 1 arguments
Thomas the reason number_of_Grades is smaller because it takes 3 grades from input while the number of students is 4.
3 grades from 4 different students.
yeah i fixed the errors in 52 and 56 . im still getting 1>c:\users\zeit\documents\visual studio 2012\projects\project17\project17\source.cpp(66): error C2143: syntax error : missing ';' before '{'