2 Replies - 704 Views - Last Post: 22 February 2009 - 08:43 AM Rate Topic: -----

#1 *Pali*  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 04-February 09

help with file input/output

Post icon  Posted 21 February 2009 - 11:01 PM

i want to do this program that Opens a file named lettergrades.txt on the C drive. (lettergrades.txt contains a single line with four characters in it. Each character is either an A, B, C, D or F. Each character is separated by a space.)
Checks to make sure that file is open before reading from it, otherwise displays a useful message.
Reads in all the grades. Determines the point value of the letter grade. A = 4.0, B = 3.0, C = 2.0, D = 1.0, F = 0.0.Closes the file.
Adds all points together and divides by 4.
Displays the grade point average to the screen.


can anyone explain to me how to write data to a file and outout it?
this is what i got and thats the only thing i unerstood about this program

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
	ifstream outputfile;
	outputfile.open ("C:\\lettergrades.txt"); 
	
	outputfile << lettergrade\n;
  
	system("PAUSE");
	return EXIT_SUCCESS;
}


Is This A Good Question/Topic? 0
  • +

Replies To: help with file input/output

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: help with file input/output

Posted 22 February 2009 - 08:00 AM

Hi *Pali*. Where you appear to be just starting out, I would suggest browsing our tutorials and snippets. In those areas are lots of explanations and examples of file input and output.
Was This Post Helpful? 0
  • +
  • -

#3 OrganizedChaos  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 153
  • Joined: 29-November 08

Re: help with file input/output

Posted 22 February 2009 - 08:43 AM

First of all, you must declare lettergrade. In this case, it would be a char:
char lettergrade
Secondly, stick this in a loop
outputfile >> lettergrade;
(Code fixed: What you had was outputfile outputting lettergrade with a return character, where in reality you want your outputfile to read in whatever letter is in the file.) Such a code would look something like
 while (outputfile){  ...  }
Third, in order to calculate the average, in the same while loop, you may want to make a switch statement that takes lettergrade that's read in through outputfile and gives it a numerical value. You could then declare a variable called sum (int) and a variable called average (double). Sum would be updated with each pass of the loop and average would be found at the end of the program.
Hope this points you in the right direction.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1