help with stream files

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 17
Reputation: lmastex is an unknown quantity at this point 
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

help with stream files

 
0
  #1
Sep 26th, 2008
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.

  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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with stream files

 
0
  #2
Sep 26th, 2008
1) the read loop is all screwed up
  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
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: help with stream files

 
0
  #3
Sep 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 17
Reputation: lmastex is an unknown quantity at this point 
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

Re: help with stream files

 
0
  #4
Sep 28th, 2008
Originally Posted by Ancient Dragon View Post
1) the read loop is all screwed up
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with stream files

 
0
  #5
Sep 28th, 2008
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:
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 17
Reputation: lmastex is an unknown quantity at this point 
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

Re: help with stream files

 
0
  #6
Sep 29th, 2008
Ok, I replaced the code with the one you gave me. I have this now:

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with stream files

 
0
  #7
Sep 29th, 2008
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 17
Reputation: lmastex is an unknown quantity at this point 
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

Re: help with stream files

 
0
  #8
Sep 29th, 2008
hehe right! Thanks man!
Last edited by lmastex; Sep 29th, 2008 at 1:23 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC