| | |
help with stream files
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 17
Reputation:
Solved Threads: 0
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.
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!
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)
#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
C++ Syntax (Toggle Plain Text)
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
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.
•
•
Join Date: Jan 2008
Posts: 17
Reputation:
Solved Threads: 0
•
•
•
•
1) the read loop is all screwed up
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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.
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.
•
•
Join Date: Jan 2008
Posts: 17
Reputation:
Solved Threads: 0
Ok, I replaced the code with the one you gave me. I have this now:
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?
C++ Syntax (Toggle Plain Text)
#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.
![]() |
Similar Threads
- Calling external files (C++)
- Accessing files with C++ (C++)
- Unknown DLL files (Viruses, Spyware and other Nasties)
- Reading stream with two xml files in (Java)
- Reading Binary files via stdin (C++)
- Print (C++)
- i got trapped in a stream of warez sites (Viruses, Spyware and other Nasties)
- Problem Downloading large files (ASP)
Other Threads in the C++ Forum
- Previous Thread: Please help w/ compile errors
- Next Thread: String comparison question
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






