468,512 Members | 1,426 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Program to read grades from file and output average

My group needs help with this program. We attempted to start it and got no where. Please help us.

Write a C++ program that will read data from a file, perform computation on the data, then print the result to another file. The data from the file will contian 100 lines where each line contains three items. The three items are an arithmetic operation followed by two integer values (non zero.) The program should perform the arithmetic operation specified on the two integers. Then the program should print out to another file the first number, the arithmetic operation, the second integer, an = sign, then the result of the operation just performed. This should repeat for the 100 lines. The program should compute the result by first using a switch statement to identify the operation (+, -, *, /) then call the appropriate function to perform the computation and print the result to the second file. A sample input file: + 4, 54 - 34 45 * 42 2 / 325 5 A sample output file: 4+45=50 34-45=-11 42*2=84 325/5=13 The general format of the program should be as follows: - The include section... - Declaring the file pointers ifstream infile; ofstream outfile; - The add function - The subtract function - The multiply function - The divide function - The main function
Nov 30 '07 #1
10 5326
oler1s
671 Expert 512MB
We'd love to help. But...you need to ask a question.
Nov 30 '07 #2
Ok. Where do I start?
Nov 30 '07 #3
oler1s
671 Expert 512MB
Break down the program into components. For example, there seems to be a component that reads data from the files, another that computes data, and another that prints out the results. Take each component, in a logical order. And then repeat, until you have a component small enough to easily code. Reading from the data file, and printing out the results are small enough. Computing the data needs to broken down.

When writing the code, you should work out the algorithm, before you even start coding.

Start with reading the data rom the file then.
Nov 30 '07 #4
We tried using a program we did long time ago to read data from a file but it wouldn't work. Could you take a look at it and see what may be wrong with it.
Expand|Select|Wrap|Line Numbers
  1. # include <iostream>
  2. # include <fstream>
  3. using namespace std;
  4.  
  5. int main(){
  6.     ifstream indata;
  7.     indata.open("pmscores.txt");
  8.     char fn[80];
  9.     char mi;
  10.     char ln[80];
  11.     int i,test1,test2,test3;
  12.     double finalscore;
  13.  
  14.  
  15.     for(i=0;i<3;i++){
  16.         indata>>fn>>mi>>ln>>test1>>test2>>test3;
  17.     }
  18.     indata.close();
  19.  
  20.     finalscore=(test1+test2+test3)/3;
  21.     ofstream outdata;
  22.     outdata.open("finalscorespm.txt");
  23.  
  24.     for(i=0;i<3;i++){
  25.         outdata<<fn<<" "<<mi<<" "<<ln<<" "<<finalscore<<endl;
  26.     }
  27.  
  28.  
  29.     outdata.close();
  30.     cout << "Task Completed!"<<endl;
  31.  
  32.     return 0;
  33. }
Nov 30 '07 #5
Hi Vince,you should follow the expert advice from Oler,just think about breaking down the task into smaller sub task;for example you could start identifying functions that make computations(control), and functions that simply make I/O(boundary) .
Nov 30 '07 #6
oh ok. Well, can you help me with that program above. I just want to make sure that works.
Nov 30 '07 #7
Can you tell us what the program is supposed to do,or at least what you want it to do?thanks
Nov 30 '07 #8
I have to write a C++ program that will read grades from a file on the hard disk drive, compute the average grade and send the result to another file on the hard disk drive.
Nov 30 '07 #9
sicarie
4,677 Expert Mod 4TB
Does the program above work? Does it not compile? Are you getting output different from what it expects?

PS - This section of our Posting Guidelines was created to help new people formulate good questions - putting all the relevant information into a single post, please have a look at it. Thanks
Nov 30 '07 #10
The program does not work. It will compile but give 3 repeated answers from the txt file. I need it so that it gives me it only once and shows that other info on the txt.
Dec 1 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by hjc | last post: by
21 posts views Thread by asif929 | last post: by
1 post views Thread by Sleepwalker817 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.