LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-31-2010, 11:04 PM   #1
Iswandy
LQ Newbie
 
Registered: Jan 2010
Posts: 2

Rep: Reputation: 0
HELP NEEDED in creating a program using fstream...absolutely no idea how to do it


a)using notepad,prepare a text file with marks for 8 students on seperate lines. The program will then read this file and store all the marks into an array.
b)It will then calculate the average marks.
c)Finally, displays all the marks and average marks on the screen.

THKS!!

would you be kind enough to give me the workout solutions...need help in this...thks lots
 
Old 02-01-2010, 12:41 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
What's so difficult about it ?

Example of reading from a file using fstream:
http://www.codersource.net/cpp_file_io.html

fstream man:
http://www.cplusplus.com/reference/iostream/fstream/

Have U tried anything so far ? Be kind enough to post it here !!

Try reading the above 2 links, try a small program and then if u face problems, feel free to report back..

Read this post too:
http://www.linuxquestions.org/questi...line-771427/#2
 
Old 02-01-2010, 07:33 AM   #3
Iswandy
LQ Newbie
 
Registered: Jan 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Hmmm this is what i got....help me to check if there are errors and whether is it the correct approach...(refer to question above)

#include <iostream>
#include <fstream>

using namespace std;

int main () {

double marks[8],sum=0,average;
int i,size=0,;
ifstream myfile;

myfile.open ("SPROG.txt");

while( size < 8 && myfile >> marks[size] )
++size;

for(i=0;i<8;i++)
{
myfile >> marks[8];
cout << "Student " << i+1 << " scored " << marks[i] << " marks." << " \n\n";

}
for(i=0;i<8;i++)
{
myfile >> marks[8];
sum +=marks[i] ;
}

average = sum /8.0;
cout << "\nThe average mark is " << average << endl;

myfile.close();


return 0;
}
 
1 members found this post helpful.
Old 02-01-2010, 07:39 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
You should put code in [code] tags, it will print in a monospaced font.

It looks like this

Code:
Code tags
And you should also indent your code.
 
Old 02-01-2010, 07:42 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by Iswandy View Post
Hmmm this is what i got....help me to check if there are errors and whether is it the correct approach...(refer to question above)

#include <iostream>
#include <fstream>

using namespace std;

int main () {

double marks[8],sum=0,average;
int i,size=0,;
ifstream myfile;

myfile.open ("SPROG.txt");

while( size < 8 && myfile >> marks[size] )
++size;

for(i=0;i<8;i++)
{
myfile >> marks[8];
cout << "Student " << i+1 << " scored " << marks[i] << " marks." << " \n\n";

}
for(i=0;i<8;i++)
{
myfile >> marks[8];
sum +=marks[i] ;
}

average = sum /8.0;
cout << "\nThe average mark is " << average << endl;

myfile.close();


return 0;
}
Why do you need the while loop and why do you need two for loops? Also, you need to check where you're storing the input - for one thing, C/C++ arrays are indexed from 0, so the last index of an N element array will be N - 1, not N.
 
Old 02-01-2010, 08:22 AM   #6
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Homework?

There are various things wrong with your code, but I'm curious, what do you think this is supposed to do:

Code:
while( size < 8 && myfile >> marks[size] )
 
Old 02-02-2010, 01:33 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by Iswandy
Hmmm this is what i got....help me to check if there are errors and whether is it the correct approach...(refer to question above)
1. I tested your program, It is working correctly, so what's the problem now ?

2. PUT COMMENTS IN YOUR CODE SO THAT IT BECOMES EASIER FOR MEMBERS TO UNDERSTAND WHAT U ARE TRYING TO DO !!

3. Put your code in code tags.

Last edited by Aquarius_Girl; 02-02-2010 at 01:56 AM.
 
Old 02-02-2010, 07:34 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by anishakaul View Post
2. PUT COMMENTS IN YOUR CODE SO THAT IT BECOMES EASIER FOR MEMBERS TO UNDERSTAND WHAT U ARE TRYING TO DO !!

3. Put your code in code tags.
Indent it, too.

Not indented:

Code:
int main()
{
int i;
for(i=0; i<10; i++)
{
printf("Hello%d\n", i);
}
return 0;
}
Indented:

Code:
int main() {
    int i;
    for(i=0; i<10; i++) {
        printf("Hello%d\n", i);
    }
    return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java help needed: absolutely no output from the JDK tools, etc. ?? HowDoIProgramIt Linux - Software 2 11-09-2007 05:16 PM
fstream.h is creating erratic binary programs sriram_16a SUSE / openSUSE 0 10-02-2007 12:48 AM
Need An Ups, And Have Absolutely No Idea About What To Look For Durham Linux - Hardware 1 05-13-2006 08:33 AM
New Project Idea needed Gracy Linux - Newbie 4 11-11-2005 04:42 AM
absolutely very new 'n stuff...help needed plastiquepony Linux - Newbie 3 11-16-2001 09:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration