I am inputing from a file and I am outputting to a file. well when i run the program it just puts the headers up there and no names or grades. This is the input. - Balto 85 83 77 91 76
-
Mickey 80 90 95 93 48
-
Minnie 78 81 11 90 73
-
Doc 92 83 30 69 87
-
Goofy 23 45 96 38 59
-
Duckey 60 85 45 39 67
-
Grumpy 27 31 52 74 83
-
Sunny 93 94 89 77 97
-
Piggy 79 85 28 93 82
-
Pluto 85 72 49 75 63
However, this is the output im getting: - Student Test1 Test2 Test3 Test4 Test5 Average Grade
And thats it.
Here is my code: - #include <iostream>
-
#include <stdlib.h>
-
#include <iomanip>
-
#include <string>
-
#include <fstream>
-
-
using namespace std;
-
-
-
-
void calculateAverage(double test1, double test2, double test3, double
-
test4, double test5, double& studentAverage);
-
int calculateGrade(double grade);
-
-
int main()
-
{
-
-
string studentName;
-
int numberOfStudents = 0;
-
double classAverage = 0;
-
double studentAverage = 0;
-
double totalAverage = 0; //To add the average of all student averages
-
char grade;
-
double test1, test2, test3, test4, test5;
-
-
ifstream inFile; // input stream variable for the student file
-
ofstream outFile; // output stream variable
-
-
inFile.open("inGrade.txt");
-
outFile.open("FinalGrades.txt");
-
-
cout << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
-
"Student" << setw(10) << "Test1"
-
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
-
<< setw(8) << "Test5"
-
<< setw(8) << "Average" << setw(8) << "Grade" <<endl;
-
-
outFile << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
-
"Student" << setw(10) << "Test1"
-
<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"
-
<< setw(8) << "Test5"
-
<< setw(8) << "Average" << setw(8) << "Grade" << endl;
-
-
-
if (!inFile)
-
{
-
cout << "Unable to open the file." <<endl;
-
return 1;
-
}
-
-
while(inFile)
-
{
-
outFile.setf(ios::fixed, ios::floatfield);
-
outFile.setf(ios::showpoint);
-
outFile << setprecision(2);
-
-
inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5
-
;
-
-
calculateAverage(test1, test2, test3, test4, test5,
-
studentAverage);
-
-
grade = calculateGrade(studentAverage);
-
-
cout << setfill(' ') << setiosflags(ios::left) << setw(11) <<
-
studentName << setw (10) << test1
-
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
-
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
-
grade<<endl;
-
outFile << setfill(' ') << setiosflags(ios::left) << setw(11) <<
-
studentName << setw (10) << test1
-
<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4
-
<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<
-
grade<<endl;
-
-
totalAverage = totalAverage + studentAverage;
-
-
numberOfStudents++;
-
classAverage = totalAverage / numberOfStudents;
-
}
-
-
-
outFile << endl << setprecision(2)<< "Class average is:" <<
-
classAverage << endl;
-
-
inFile.close();
-
outFile.close();
-
-
system("PAUSE");
-
return 0;
-
}
-
-
-
-
//function to calculate the average
-
void calculateAverage(double test1, double test2, double test3, double
-
test4, double test5, double& studentAverage)
-
{
-
-
studentAverage = static_cast<double>(test1 + test2 + test3 + test4
-
+ test5) / 5.0;
-
-
}
-
-
-
-
-
int calculateGrade(double studentAverage)
-
{
-
char grade;
-
-
if (studentAverage <= 100 && studentAverage >= 90)
-
grade = 'A';
-
else if (studentAverage < 90 && studentAverage >= 80)
-
grade = 'B';
-
else if (studentAverage < 80 && studentAverage >= 70)
-
grade = 'C';
-
else if (studentAverage < 70 && studentAverage >= 60)
-
grade = 'D';
-
else if (studentAverage < 60 && studentAverage >= 0)
-
grade = 'F';
-
else
-
cout << "Invalid grade " << endl;
-
-
return grade;
-
-
}
Any help would be greatly appreciated. Thanks.
2 3922
I'm not sure. The code works fine for me and writes all the data to the file. Maybe the program is not able to open the inGrade.txt file. How about putting a system("PAUSE") before the return so you can check: - if (!inFile)
-
{
-
cout << "Unable to open the file." <<endl;
-
system("PAUSE");
-
return 1;
-
}
I hope this helps solve the problem.
That's not the way to check if the file is open.This call is same as calling ifstream::fail() which checks if there was a fail bit(generally some file access error) or bad bit(checks to see if stream has lost it's integrity).To check if the file is open use: - ifstream::is_open()
-
-
if(inFile.is_open==false)
-
{
-
....
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: da Vinci |
last post by:
Hello again.
I have a question regaring pass-by-reference and multiple functions.
This is an assignment that I have to use pass-by-reference for
everything.
First off, I made the following...
|
by: DrNoose |
last post by:
Hi!
I've got a program that's almost done, but I'm getting compile errors in
two lines: 317 & 319. I think the main error has to do with the Truck Class.
I'm a newbie and keep looking at the...
|
by: trialproduct2004 |
last post by:
Hi all,
Can someone tell me how virtual functions works.
Like how memory is getting allocated to virtual function. And how to
call base class function through derived class pointer.
And why...
|
by: Fao |
last post by:
Hello, I am having some problems with inheritance. The compiler does
not not return any error messages, but when I execute the program, it
only allows me to enter the number, but nothing else...
|
by: dasilva109 |
last post by:
Hi guys
I am new to C++ and need urgent help with this part of my code for a
uni coursework I have to submit by Thursday
//ClientData.h
#ifndef CLIENTDATA_H
#define CLIENTDATA_H
#include...
|
by: dasilva109 |
last post by:
Hi guys
I am new to C++ and need urgent help with this part of my code for a
uni coursework I have to submit by Thursday
//ClientData.h
#ifndef CLIENTDATA_H
#define CLIENTDATA_H
#include...
|
by: Tigerlily |
last post by:
// Program to create new accounts, perform deposits, withdrawals, and bank
//inquiries
#include<iostream>
#include<fstream>
using namespace std;
void menu();
int read_accts(int , double ,...
|
by: al2004 |
last post by:
Write a program that reads information about youth soccer teams from a file, calculates the average score for
each team and prints the averages in a neatly formatted table along with the team name....
|
by: Amali |
last post by:
I'm newdie in c programming. this is my first project in programming.
I have to write a program for a airline reservation. this is what i
have done yet. but when it runs it shows the number of...
|
by: Stephen Torri |
last post by:
Below is a class that is suppose to represent a segment of memory or a
contents of a binary image (e.g. ELF executable). I have started to read
Modern C++ Design and thought the best way to ensure...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |