RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 250 | Replies: 7
Reply
Join Date: Jan 2008
Posts: 6
Reputation: lmastex is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

help with stream files

  #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.

 
  #include <fstream>
  #include <iostream>
  #include <string>
 

 using namespace std;

 int main() {

  int size = 100;
  int pos = 0;
  int largo = 0;
  int dato, dato2, circulo, i;
  int array[size];
  
 ifstream read("numbers.dat");
 ofstream write("numbers2.dat");



read >> dato >> dato2;

  while (!read.eof()) {



   array[pos] = dato; // first number of the file
   array[size-pos-1]= dato2; // last number of the file
   
  pos++;
  largo += 2; // how many elements are in the file

  read >> dato >> dato2;


 }


 circulo = largo/2; // the actual number of circles

 write << "LA SUMA DE TODOS LOS CIRCULOS ES: " << endl;

 for (i = 0; i <= circulo; i++) {

 write << "\ncirculo # " << (i+1) << ": " << dato+dato2; // write to "numbers2.dat" the addition in circles

 }


  return 0;

 }

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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,564
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 977
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: help with stream files

  #2  
Sep 26th, 2008
1) the read loop is all screwed up
int pos = 0;
while( read >> array[pos] )
   pos++;
// now that you have all the numbers in an array, just add them
// up the way you want them
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,087
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 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: help with stream files

  #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  
Join Date: Jan 2008
Posts: 6
Reputation: lmastex is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

Re: help with stream files

  #4  
Sep 28th, 2008
Originally Posted by Ancient Dragon View Post
1) the read loop is all screwed up
int pos = 0;
while( read >> array[pos] )
   pos++;
// now that you have all the numbers in an array, just add them
// 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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,564
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 977
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: help with stream files

  #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:
int pos = 0;
while( read >> array[pos]  >> array[size-pos-1])
{
   pos++;
   largo += 2; // how many elements are in the file
}
// now that you have all the numbers in an array, just add them
// up the way you want them
Last edited by Ancient Dragon : Sep 28th, 2008 at 9:58 pm.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Jan 2008
Posts: 6
Reputation: lmastex is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

Re: help with stream files

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

  #include <fstream>
  #include <iostream>
  #include <string>
 

 using namespace std;

 int main() {

  int i, circulo;
  const int size = 100;
  int pos = 0;
  int largo = 0;
  int array[size];
  
 

 ifstream read("numbers.dat");
 ofstream write("numbers2.dat");


  while( read >> array[pos]  >> array[size-pos-1])

{
   pos++;

   largo += 2;


}


 circulo = largo/2;

 write << "LA SUMA DE TODOS LOS CIRCULOS ES: " << endl;


 for (i = 0; i <= circulo; i++) {

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

 }


  return 0;

 }

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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,564
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 977
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: help with stream files

  #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.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Jan 2008
Posts: 6
Reputation: lmastex is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

Re: help with stream files

  #8  
Sep 29th, 2008
hehe right! Thanks man!
Last edited by lmastex : Sep 29th, 2008 at 1:23 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:45 pm.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC