473,414 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

how can i read a number twice in fstream code?

hi again i have this code
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     ifstream infile;
  4.     ofstream outfile;
  5.     float num1,num2,num3,limit1=0,limit2=0,counter1=0,counter2=0,sum=0;
  6.     float aver;
  7.  
  8.     infile.open("testin.txt");
  9.     outfile.open("testout.txt");
  10.  
  11.     if (infile.fail() || outfile.fail())
  12.     {
  13.         cout<<"failed";
  14.         return 1;
  15.     }
  16. else
  17.     while(!infile.eof())
  18.     {
  19.         infile>>num1>>num2;
  20.         limit1=num1;
  21.         limit2=num2;
  22.  
  23.         while(counter1<limit1)
  24.         {
  25.         sum=0;
  26.         counter2=0;
  27.  
  28.         while (counter2<limit2)
  29.         {
  30.             infile>>num3;
  31.             outfile<<num3<<" ";
  32.             sum+=num3;
  33.             counter2++;
  34.  
  35.  
  36.         }
  37.         outfile<<"\nTotal      ="<<sum<<endl;
  38.         aver=sum/limit2;
  39.         outfile<<"Average    ="<<aver<<endl;
  40.         outfile<<"____________________"<<endl;
  41.         counter1++;
  42.  
  43.  
  44.         }
  45.  
  46.     }
  47.  
  48.         return 0;
  49. }
Im just trying to read the num3 variable twice to calculate the min and max from input file but i cant figure out a way!!!!!
when i try to read num 3 inside the

and then initialing
minValue and maxValue to it
and the read the num3 again inside the while(counter1<limit1)
like
while(counter1<limit1)
{
sum=0;
counter2=0;
infile>>num3;
and then read the same number inside the
while (counter2<limit2)
and compare them using
Expand|Select|Wrap|Line Numbers
  1. if(maxValue<num3)
  2. maxValue=num3;
  3. else if(num3<minValue)
  4. minValue=num3
  5. else
  6. {
  7. minValue=num3 
  8. maxValue=num3
  9. }
it reads the next # in the input file?
any suggestions?
Nov 10 '07 #1
1 1219
Studlyami
464 Expert 256MB
You already have a thread for trying to find min/max please keep all questions in the same thread. I have also shown a solution for this in the other thread. Any other questions please direct them in that thread.
Nov 10 '07 #2

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

Similar topics

7
by: Christian Henke | last post by:
Hi! maybe someone can help me?! I want to use fstream to read an .au file . This .au file includes multiple information in its header. a MagicNumber, the No of Channels used, and so on. In...
0
by: lion | last post by:
I have in my programm three files: SCANNER.CPP,tst_scanner.cpp and scaner.h i need read and write from text file which is glopal. how i can do it? the strange things if i put all programm in one...
3
by: phwashington | last post by:
I am new to C++ and have a data file I want to read, which was stored in binary. I have looked at the data with a hex editor and it appears to be correct. Whenever I try to read it though as an...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.