help please

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 please

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

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. int main() {
  9.  
  10. const int CAPACIDAD = 100;
  11. int array[CAPACIDAD];
  12. int num1, num2, pos = 0;
  13. ifstream Entrada;
  14.  
  15.  
  16. Entrada.open("entrada.txt");
  17.  
  18.  
  19. Entrada >> num1 >> num2;
  20.  
  21. while(!Entrada.eof())
  22.  
  23. {
  24.  
  25. array[pos] = num1;
  26.  
  27. cout << num1;
  28.  
  29. array[CAPACIDAD - pos - 1] = num2;
  30.  
  31. cout << "\t" << num2 << endl;
  32. pos++;
  33.  
  34.  
  35. Entrada >> num1 >> num2;
  36. }
  37.  
  38.  
  39. cin.ignore();
  40. return 0;
  41. }

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,677
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: help please

 
0
  #2
Sep 1st, 2008
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:
  1. while( Entrada >> num1 >> num2 )
  2. {
  3. //do the processing
  4. }
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.
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.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC