943,987 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 705
  • C++ RSS
Sep 26th, 2008
0

help with stream files

Expand Post »
Hey again!

This time I have this program which is supposed to read some stream file called "numbers.dat" which has the numbers 1 up to 10.
I get the reading, but the thing is in the for loop I want to add the numbers in circles.
For example:
1 2 3 4 5 6 7 8 9 10
I want the number 1 and number 10 to be added, then number 2 and number 9 to be added.
This addition I want to write it in the stream file called "numbers2.dat"
The program does add the numbers, but in parallel: 1+2 = 3, then 3+4=7

Here is the code.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.  
  11. int size = 100;
  12. int pos = 0;
  13. int largo = 0;
  14. int dato, dato2, circulo, i;
  15. int array[size];
  16.  
  17. ifstream read("numbers.dat");
  18. ofstream write("numbers2.dat");
  19.  
  20.  
  21.  
  22. read >> dato >> dato2;
  23.  
  24. while (!read.eof()) {
  25.  
  26.  
  27.  
  28. array[pos] = dato; // first number of the file
  29. array[size-pos-1]= dato2; // last number of the file
  30.  
  31. pos++;
  32. largo += 2; // how many elements are in the file
  33.  
  34. read >> dato >> dato2;
  35.  
  36.  
  37. }
  38.  
  39.  
  40. circulo = largo/2; // the actual number of circles
  41.  
  42. write << "LA SUMA DE TODOS LOS CIRCULOS ES: " << endl;
  43.  
  44. for (i = 0; i <= circulo; i++) {
  45.  
  46. write << "\ncirculo # " << (i+1) << ": " << dato+dato2; // write to "numbers2.dat" the addition in circles
  47.  
  48. }
  49.  
  50.  
  51. return 0;
  52.  
  53. }

So, could you guys give me a hint or is there some way to make the addition in circles? I think I have it already but there's some kind of mistake somewhere. Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008
Sep 26th, 2008
0

Re: help with stream files

1) the read loop is all screwed up
C++ Syntax (Toggle Plain Text)
  1. int pos = 0;
  2. while( read >> array[pos] )
  3. pos++;
  4. // now that you have all the numbers in an array, just add them
  5. // up the way you want them
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Sep 26th, 2008
0

Re: help with stream files

in adition, change int size = 100; to const int size = 100; size of an array must be a constant known at compile time.
Last edited by vijayan121; Sep 26th, 2008 at 1:58 am.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Sep 28th, 2008
0

Re: help with stream files

1) the read loop is all screwed up
C++ Syntax (Toggle Plain Text)
  1. int pos = 0;
  2. while( read >> array[pos] )
  3. pos++;
  4. // now that you have all the numbers in an array, just add them
  5. // up the way you want them

You mean I need to replace this with the whole read loop? And what about the dato and dato2
int variables? I want them to read them (the numbers in the file) and save them into these variables.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008
Sep 28th, 2008
0

Re: help with stream files

you don't need dato and dato2 because the code you posted does nothing with them. You could modify the code I posted like this:
C++ Syntax (Toggle Plain Text)
  1. int pos = 0;
  2. while( read >> array[pos] >> array[size-pos-1])
  3. {
  4. pos++;
  5. largo += 2; // how many elements are in the file
  6. }
  7. // now that you have all the numbers in an array, just add them
  8. // up the way you want them
Last edited by Ancient Dragon; Sep 28th, 2008 at 9:58 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Sep 29th, 2008
0

Re: help with stream files

Ok, I replaced the code with the one you gave me. I have this now:

C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.  
  10. int i, circulo;
  11. const int size = 100;
  12. int pos = 0;
  13. int largo = 0;
  14. int array[size];
  15.  
  16.  
  17.  
  18. ifstream read("numbers.dat");
  19. ofstream write("numbers2.dat");
  20.  
  21.  
  22. while( read >> array[pos] >> array[size-pos-1])
  23.  
  24. {
  25. pos++;
  26.  
  27. largo += 2;
  28.  
  29.  
  30. }
  31.  
  32.  
  33. circulo = largo/2;
  34.  
  35. write << "LA SUMA DE TODOS LOS CIRCULOS ES: " << endl;
  36.  
  37.  
  38. for (i = 0; i <= circulo; i++) {
  39.  
  40.  
  41. write << "\ncirculo # " << (i+1) << ": " << array[pos]+array[size-pos-1];
  42.  
  43. }
  44.  
  45.  
  46. return 0;
  47.  
  48. }

This is what I get in the output of the file:

LA SUMA DE TODOS LOS CIRCULOS ES:

circulo # 1: 6822532
circulo # 2: 6822532
circulo # 3: 6822532
circulo # 4: 6822532
circulo # 5: 6822532
circulo # 6: 6822532

What am I doing wrong?
Last edited by lmastex; Sep 29th, 2008 at 12:31 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008
Sep 29th, 2008
0

Re: help with stream files

>>write << "\ncirculo # " << (i+1) << ": " << array[pos]+array[size-pos-1];

Variable pos is never changed. I think you should use the loop counter instead of pos.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Sep 29th, 2008
0

Re: help with stream files

hehe right! Thanks man!
Last edited by lmastex; Sep 29th, 2008 at 1:23 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Please help w/ compile errors
Next Thread in C++ Forum Timeline: String comparison question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC