473,418 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

IO stream

I have the following question,
I am trying to read from a file and output to another file.
But I am not getting exactly the expected output.

Can anyone help please!
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("c:\\studentFile.txt");
outFile.open("c:\\Student_out.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;

}

Feb 28 '06
56 3693
Thank you so much,
but you still didn't recommend a good c++ book !..;-)

Mar 2 '06 #51
oLgAa25 wrote:
Thank you so much,
but you still didn't recommend a good c++ book !..;-)


I believe Bjarne Stroustrup's "The C++ Language" 3rd ed is the best.
Mar 2 '06 #52
Olga writes:
but you still didn't recommend a good c++ book !..;-)


I recommend authors, not books. Stephen Prata and Robert Lafore are both
very good. Based on what I have seen of your work, I would defer getting
Stroustrup for a while.
Mar 2 '06 #53

oLgAa25 wrote in message
<11**********************@i40g2000cwc.googlegroups .com>...
Thank you so much,
but you still didn't recommend a good c++ book !..;-)


While you are researching what books to get, go download 'Thinking in C++'
vol.1(of 2) at:
(available for free here):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

It is NOT a 'programming newbie' book. It's not 'the' best book, but, it it
extremely far from the worst (..and won't cost you anything but short
download time. You can buy it in hardcopy too.)

I've heard good things about Mr. Glassborow's book 'You Can Do It!'
http://www.spellen.org/youcandoit
It is targeted for 'programming newbies'. I wish it had been available when I
started C++.

Besides what the other posters have suggested, you can find book reviews at
www.accu.org to aid in your choice(s).

--
Bob R
POVrookie
Mar 2 '06 #54
Hello all,
one more question, what about Deitel & Deitel.? any ideas?
appreciate it

Mar 2 '06 #55
Hello all,
one more question, what about Deitel & Deitel? any comments?
Thank you

Mar 2 '06 #56
On 2006-03-02, oLgAa25 <ol********@yahoo.com> wrote:
Hello all,
one more question, what about Deitel & Deitel? any comments?
Thank you


Personally, I didn't like it. The programming style, the problems, and
the order in which they presented topics went against the
recommendations of Bjarne Stroustrup: Learn how to harness the power
of the standard library before mastering the low-level, C-compatible
features. _Accelerated C++_ (Koenig, Moo) uses the approach advocated
in _The C++ Programming Language_.

--
Neil Cerutti
Mar 2 '06 #57

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: andrewcw | last post by:
OK I am half way there - I can manipulate the stream without the byte issue like this - but is this the way to push the new values back into the stream & write out the stream without resorting to...
5
by: andrewcw | last post by:
I have an object to serialize. TextWriter writer = new StreamWriter("test.xml"); serializer.Serialize(writer,obj); writer.Close(); but below does not, why ?? I have a file that I will have...
3
by: MJB | last post by:
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and...
4
by: TT (Tom Tempelaere) | last post by:
Hey there, I need a string stream, but I can't find one in .NET. I thought StringWriter would derive from Stream, alas it doesn't do so. Which leads me to my next question: What is the purpose...
4
by: fastback66 | last post by:
Evidently per the C++ standard, it is not possible to measure the distance between two stream iterators, because two non-end-of-stream iterators are equal when they are constructed from the same...
8
by: Marc Gravell | last post by:
I want to write a method that will accept a stream as a parameter, and which will write xml to the stream (based in reality on database results) using the XmlTextWriter class. However, this insists...
4
by: Helge Jensen | last post by:
In C# 2.0 System.IO.Stream is declared as: public class Stream: ..., IDisposable { ... public void Dispose(); public void Dispose(bool); IDisposable.Dispose(); } Which must be a...
0
by: mario.lat_ | last post by:
Hallo to all, I have write a little script for connecting to cisco router BUT I have a problem: I have to send to router all the commands and then I have to read the output. If I send a command1...
3
by: sven.suursoho | last post by:
Hello, In main(), the first output API is what I try to achieve. Unfortunately it fails, printing first string as pointer instead of human readable message. Tried to initialize str(""), set new...
4
by: Scott F. Brown | last post by:
Greetings all... I was playing around with compressing streams and came across a behavior that I do not understand. I create a stream (input) from the contents of a textbox. That stream is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.