2 Replies - 120 Views - Last Post: 13 September 2012 - 06:44 AM Rate Topic: -----

#1 40mpg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-April 09

I need help with a way to output to a file

Posted 13 September 2012 - 04:51 AM

I want to be able to output the array below to a file, but i want to put some labels onto the array like this

http://imageshack.us...s/253/picbl.jpg (sorry i can't post this picture here)


the number pairs are array elements, as you guys can see, i would like to add the word HW, LAB, Best etc..into the array so i can output something similar to the example above to a text file, i've already calculated all the required variables,

what i've tried is cout << setw(4) << 1 << set(w) << 2 etc...to manually draw out the layout that i want, it looks ok, but how do i output everything to a text file? there has to be a better way of doing this yeah?

#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "cmath"
#include "iomanip"
using namespace std;


int main()
{
	double grade [2][5];
	double averageLab = 0;
	double averageHw = 0;
	double totalLab = 0;
	double totalHw = 0;
	double bestLab = 0;
	double worseLab = 0;
	double bestHw = 0;
	double worseHw = 0;
	double averageClass = 0;

	

	ofstream out;
	out.open("temp.txt");


	for(int i = 0; i < 5; i ++)
	{

	cout << "please enter your grade percentage for your"<< i + 1<<" lab assignment" << endl;
	cin >> grade[0][i];
	}

	for(int j = 0; j < 5; j++)
	{
		cout << "please enter your grade percentage for your " << j + 1 << "hw assignment" << endl;
		cin >> grade[1][j];
	}

	for( int k = 0; k < 5 ; k++)
	{
		totalLab += grade[0][k];
		totalHw += grade[1][k];
		averageLab = totalLab / 5;
		averageHw = totalHw / 5;
		
		bestLab = grade[0][k];
		worseLab = grade[0][k];
		if(grade[0][k] > bestLab)
		{
			bestLab = grade[0][k];
		}
		else if(grade[0][k] < worseLab )
		{
			worseLab = grade[0][k];
		}
	}
	
	for(int y = 0; y < 5 ; y ++)
	{
		bestHw = grade[1][y];
		worseHw = grade[1][y];

		if(grade[1][y] > bestHw)
		{
			bestHw = grade[1][y];
		}
		else if(grade[1][y] < worseHw)
		{
			worseHw = grade[1][y];
		}
	}
	
	averageClass = (averageHw + averageLab) / 2;

	if(averageClass < 50.00)
	{
		cout <<" your average for the class is : " << averageClass << endl;
		cout << " you didnt pass the class because your average is less than 50%" << endl;
	}
	else
	{
		cout <<"your average for the class is : " << averageClass << endl;
		cout << "congratulation you passed the class" << endl;
	}


	cout <<"Best Lab = " << bestLab << endl;
	cout <<"Worse Lab = " <<worseLab << endl;
	cout <<"best Hw = "<< bestHw << endl;
	cout <<"Worse Hw = " << worseHw <<endl;




	

	system("PAUSE");

}




Is This A Good Question/Topic? 0
  • +

Replies To: I need help with a way to output to a file

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 3055
  • View blog
  • Posts: 9,292
  • Joined: 25-December 09

Re: I need help with a way to output to a file

Posted 13 September 2012 - 06:06 AM

Quote

what i've tried is cout << setw(4) << 1 << set(w) << 2 etc...to manually draw out the layout that i want, it looks ok, but how do i output everything to a text file?

What have you tried. Outputting text to a text file is basically the same as outputting text to the screen. The only difference is the stream you are using.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,530
  • Joined: 08-August 08

Re: I need help with a way to output to a file

Posted 13 September 2012 - 06:44 AM

This isn't directly related to what you're trying to do, but it will make it and everything else you will want to do easier: use functions. Writing code is mostly about organization, and a first step in the direction of organizing code is to use functions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1