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!

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

in adition, change int size = 100; to const int size = 100; size of an array must be a constant known at compile time.

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.

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

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?

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

hehe right! Thanks man! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.