473,511 Members | 16,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trouble with external file usage

143 New Member
I'm having trouble reading the first 2 lines of data in an external file.

I am supposed to use a while loop to read the first 2 lines of an external file that contains several random integers. I think I need 2 loops. I also think it is best not to reinitialize the variables 'sum' and 'num' before the second loop executes.

The overall goal is to read the integers on the 1st 2 lines and compute their sum.

FYI, the data on the 1st 3 lines of the external file is:
21 33 56 725 81
405 628 27
167 254 823 78 63 45

Here's the code I have so far: Please help!!



// read file and compute the average of all integers on the first 2 lines

inFile.open("A: DATFILE1.TXT"); //open DATFILE1.TXT

char inchar = ' '; //assigns inchar to a blank character
sum = num = avg = 0;
count = 1;

inFile>>num;
inFile.get(inchar);
while(inchar !='\n') //this loop is supposed to get the data from the 1st line of data and compute a sum
{
inFile>>num;
count++;
sum = sum + num;
inFile.get(inchar);
}

while(inchar !='\n') //this loop is supposed to get the data from the 2nd line of data and compute the sum
{
inFile>>num;
count++; //NOT GETTING 2nd LINE OF DATA
sum = sum + num;
}

avg = sum/count;

cout<<"The average of the numbers in the 1st 2 lines of data is: "<<avg<<endl<<endl;
outFile<<"The average of the numbers in the 1st 2 lines of data is: "<<avg<<endl<<endl;

inFile.close(); //closes the file for the next calculation
inFile.clear();
Oct 11 '06 #1
1 1806
teddarr
143 New Member
Nevermind.....Got it.
Oct 12 '06 #2

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

Similar topics

19
5630
by: Svennglenn | last post by:
I'm working on a program that is supposed to save different information to text files. Because the program is in swedish i have to use unicode text for ÅÄÖ letters. When I run the following...
11
5565
by: Jake j | last post by:
Yes, this is a simple and straightforward thing to do as long as the .js file doesn't use a function. When I try a script with a function in a js file I can't get it to work (though it works fine...
6
1683
by: Mellow Crow | last post by:
Just discovered this technique. Is this old hat? Would there be any disadvantage to doing this? In your external .js file: /* Summary: includes external scripts in this external script so...
3
1731
by: brian.newman | last post by:
I'm having trouble with XML in IE. I wrote the following function and it works fine in Firefox, but when I try to run it in IE, I get the error "getElementsByTagName(..).0' is null or not an...
7
10256
by: christian.eickhoff | last post by:
Hi Everyone, I am currently implementing an XercesDOMParser to parse an XML file and to validate this file against its XSD Schema file which are both located on my local HD drive. For this...
5
2045
by: yihan | last post by:
------ Build started: Project: Test, Configuration: Debug Win32 ------ Linking... Test.obj : error LNK2019: unresolved external symbol "int __cdecl CLIAppTerm(long *,long *,char *)"...
0
2212
by: UncleRic | last post by:
Environment: Mac OS X (10.4.10) on MacBook Pro I'm a Perl Neophyte. I've downloaded the XML::Parser module and am attempting to install it in my working directory (referenced via PERL5LIB env): ...
3
2470
by: plbaldri | last post by:
I have written a very simple and basic program for an intro Comp Sci class and am having trouble with it crashing. The program needs to accept a filename on the command line at the prompt and has to...
0
7245
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
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7356
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
7427
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...
1
7085
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
7512
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
5671
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,...
1
5069
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...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.