| | |
help please
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 17
Reputation:
Solved Threads: 0
Hey guys. I'm just trying out something with the fstream library. In the next code, I'm trying to read from the beginning of the array (num1) and from the end of the array (num2) the file I have as an "entry" called "entrada.txt", which has the numbers 1 up to 12. I want the program to show me in the first cout inside the while loop the first 6 numbers, from 1 to 6, and in the 2nd cout, numbers from 12 to 7. This is what i have so far.
When I run the program, all I get is this:
1 2
3 4
5 6
7 8
9 10
It doesn't even get to 12.
Do you know whats the problem. If you have any questions ask me =)... thanks
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; int main() { const int CAPACIDAD = 100; int array[CAPACIDAD]; int num1, num2, pos = 0; ifstream Entrada; Entrada.open("entrada.txt"); Entrada >> num1 >> num2; while(!Entrada.eof()) { array[pos] = num1; cout << num1; array[CAPACIDAD - pos - 1] = num2; cout << "\t" << num2 << endl; pos++; Entrada >> num1 >> num2; } cin.ignore(); return 0; }
When I run the program, all I get is this:
1 2
3 4
5 6
7 8
9 10
It doesn't even get to 12.
Do you know whats the problem. If you have any questions ask me =)... thanks
Last edited by lmastex; Sep 1st, 2008 at 2:50 am.
Does your data file end with a newline? That is, after typing "12", do you hit the enter key?
When using eof( ) to test for end of data, that's usually a crucial requirement of the data file.
Try controlling your loop thusly:
If the read action is successful, you'll enter the loop body. However, in this form, you must have an even number of data elements in the file.
When using eof( ) to test for end of data, that's usually a crucial requirement of the data file.
Try controlling your loop thusly:
C++ Syntax (Toggle Plain Text)
while( Entrada >> num1 >> num2 ) { //do the processing }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Practice Problem, strstr() function
- Next Thread: int *array[10] to dynamic array equivalent?
| 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 count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer 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 test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






