3 Replies - 1068 Views - Last Post: 29 April 2009 - 12:20 PM Rate Topic: -----

#1 fraandres   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 80
  • Joined: 31-March 09

Infile highest, lowest average

Posted 28 April 2009 - 08:02 PM

I'm starting this program I have to use Infile: and the file is
346 130 982 90 656 117 595
415 948 126 4 558 571 87
42 360 412 721 463 47 119
441 190 985 214 509 2 571
77 81 681 651 995 93 74
310 9 995 561 92 14 288
466 664 892 8 766 34 639
151 64 98 813 67 834 369
I have to find the sum the average the highest and the lowes of each line. I just started but I dont know why when I run it it just shows up sum of all the numbers not the first line of number. I believe the counter is correct.
#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

int main()

{
	int lowest=0, highest=0, sum=0, num=0;
	int average=0,counter=0;
	int input_number=0;



	ifstream infile;
	ofstream outputfile;

	infile.open("F:\\labaa.txt");
	outputfile.open("F:\\output.txt");

	if(!infile)
	{
		cout<<"cannot open the input file."<<endl;
		outputfile<<"cannot open the input file."<<endl;
	
		return 0;
	}
		counter=0;
		while(counter<7)
		{
	

		while (!infile.eof())
		{
			infile>>input_number;

			cout<< input_number<<"\t";
			outputfile<< input_number<<"\t";
			sum = input_number + sum;
			counter++;
		}

			}
		outputfile<<endl<<"Sum Is:"<<sum;
		cout<<endl<<"Sum Is:"<<sum;
	infile.close();
	outputfile.close();
	return 0;
}

this are my results

/*
346	 130	 982	 90	  656	 117	 595	 415	 948	 126
4	   558	 571	 87	  42	  360	 412	 721	 463	 47
119	 441	 190	 985	 214	 509	 2	   571	 77	  81
681	 651	 995	 93	  74	  310	 9	   995	 561	 92
14	  288	 466	 664	 892	 8	   766	 34	  639	 151
64	  98	  813	 67	  834	 369	 369
Sum Is:21856Press any key to continue . . .

*/



I will really appreciate if anyone give me a hand please

This post has been edited by fraandres: 28 April 2009 - 08:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Infile highest, lowest average

#2 Plus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Infile highest, lowest average

Posted 29 April 2009 - 12:52 AM

your problem is easy,

the pointer the istream use moves when you read,
after the first cycle of the first loop the pointer is in the end,

thus, in the other 6 cycles .. the second loop won't work !

it's simple, we just take the pointer back to the beginning everytime,

 while(counter<7)
 {
	   infile.seekg(0,ios::beg);

	   while(!infile.eof())
	   {
			infile >> input_number;

			cout <<  input_number << "\t";
			outputfile << input_number << "\t";
			sum = input_number + sum;
			counter++;
	   }

 }


Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Infile highest, lowest average

Posted 29 April 2009 - 11:29 AM

Moving to Homework.
Was This Post Helpful? 0
  • +
  • -

#4 Notorion   User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: Infile highest, lowest average

Posted 29 April 2009 - 12:20 PM

What you need to do, is output the sum at the end of every line (counter in your case). Then when you start on another line, (or before you start), set the sum back to 0 for each line.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1