473,405 Members | 2,141 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,405 software developers and data experts.

File IO arrays

19
Ok fellow programmers
i got this program
file is say (71dIN.txt)
inside file is
Expand|Select|Wrap|Line Numbers
  1. 45 98 35 23 67 84
  2. 65 91 20 54 62 37 65 84 32
  3. 21 54 95 87 35 62 54 78
  4. 56
  5. 95 62 35 54 78
  6.  
now the only problem is is that when i try to find average and total of each line it adds all lines up :S
this is my code
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     string sBuf;
  9.     int arnNums[50], nSize, i, nHighest = 0, nLowest = 0, nAverage = 0, nTotal = 0, arn[5], n = 0;
  10.     ifstream fin("71dIN.txt");
  11.     istringstream istr(sBuf);
  12.     while(1)
  13.     {
  14.         getline(fin, sBuf);
  15.         istr.clear();
  16.         istr.str(sBuf);
  17.         if(fin.eof()) break;
  18.         i = 0;
  19.         while(1)
  20.         {
  21.             istr >> arnNums[i];
  22.             i++;
  23.             if(istr.eof()) break;
  24.         }    
  25.         nSize = i;
  26.         for(i = 0; i<nSize; i++)
  27.         {
  28.             nTotal += arnNums[i];
  29.             nAverage = nTotal / 4;
  30.             cout << arnNums[i] << " ";
  31.         }
  32.         cout << endl;
  33.         for(i = 0; i < 1; i++)
  34.         {
  35.             cout << "Average is " << nAverage << " and Total is " << nTotal << endl;
  36.             cout << endl << endl;
  37.         }
  38.     }
  39.     return 0;
  40. }
  41.  
can you guys help if u know the problem... thanks alot
Oct 29 '06 #1
5 1533
Fir3Bat
19
This is my output
Expand|Select|Wrap|Line Numbers
  1. 45 98 35 23 67 84
  2. average is 88 and total is 352
  3.  
  4. 65 91 20 54 62 37 65 84 32
  5. average is 215 and total is 862
  6.  
  7. 21 54 95 87 35 62 54 78
  8. average is 337 and total is 1348
  9.  
  10. 56
  11. average is 351 and total is 1404
  12.  
  13. 95 62 35 54 78
  14. average is 432 and total is 1728
  15.  
Oct 29 '06 #2
Fir3Bat
19
none wants to help :(
Oct 29 '06 #3
vninja
40
looks like your logic is wrong

nSize = i;
for(i = 0; i<nSize; i++)
{
nTotal += arnNums[i];
nAverage = nTotal / 4;
cout << arnNums[i] << " ";
}

try this instead

nSize = i;
for(i = 0; i<nSize; i++)
{
nTotal += arnNums[i];
nAverage = nTotal / i; // <- average is
// sum/num
cout << arnNums[i] << " ";
}
Oct 30 '06 #4
vninja
40
also i don't see where you're reinitializing the values of totals and average back to zero. (i might be blind) but if i'm not then find a place to reinitialize them on the way back.
Oct 30 '06 #5
vninja
40
try this



#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{

ifstream fin("71dIN.txt");
istringstream istr(sBuf);
while(1)
{

string sBuf;
int arnNums[50], nSize, i, nHighest = 0, nLowest = 0, nAverage = 0, nTotal = 0, arn[5], n = 0;


getline(fin, sBuf);
istr.clear();
istr.str(sBuf);
if(fin.eof()) break;
i = 0;
while(1)
{
istr >> arnNums[i];
i++;
if(istr.eof()) break;
}
nSize = i;
for(i = 0; i<nSize; i++)
{
nTotal += arnNums[i];
nAverage = nTotal / 4;
cout << arnNums[i] << " ";
}
cout << endl;
for(i = 0; i < 1; i++)
{
cout << "Average is " << nAverage << " and Total is " << nTotal << endl;
cout << endl << endl;
}
}
return 0;
}
Oct 30 '06 #6

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

Similar topics

13
by: Kim Jensen | last post by:
Hi there I currently have a .txt file (lets call it jimharper.txt) that holds a number of arrays like for instance: $name = "Jim Harper" $relatives = "Joan Harper (wife)" $relatives =...
2
by: kelly | last post by:
Hi, I don't have a code to show you, what I need are references or algorithms so that I'm in a right track. By the way, thanks for introducing me the array or arrays. Now I can continue my...
13
by: giovanniparodi79 | last post by:
Hello everybody is there some utility to convert a raw image in an header file? Thanks everybody Gio
4
by: bei | last post by:
Hi, I am trying to write several arrays into one file, with one arrays in one column. Each array (column) is seperated by space. ie. a= b= c= 1 5 9 2 6 10 3 7 11 4 8 12
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
3
by: Sean Davis | last post by:
I have a set of numpy arrays which I would like to save to a gzip file. Here is an example without gzip: b=numpy.ones(1000000,dtype=numpy.uint8) a=numpy.zeros(1000000,dtype=numpy.uint8) fd =...
2
by: Compass | last post by:
Hi all, I have an int array in a text file. The file structure is like this: , , , ] How can I easily read them in to three int arrays? Thanks a lot!
17
by: Bill Cunningham | last post by:
I was wondering if someone could look this file over for me. It compiles correctly and prints the number 2 so I know fscanf is working. I am reading a text file and converting the text data...
7
by: lancewassing | last post by:
I'm hoping this question is simple. I am trying to create a login script for my already in place client manage using php. I have created a text file filled with usernames and passwords delimited by...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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.