what's the wrong with my code?

Discussion in 'Programmer's Corner' started by moslem, Jul 15, 2010.

  1. moslem

    Thread Starter New Member

    Dec 16, 2009
    20
    0
    hello,
    my code is to read a file and inserting it to a list and type it to a nother file my problem is with files when opening it it already be opened but i reads no thing from the file and i don't know why?
    i want to know the reason.
    Thanks.
    my code:
    #include"ordering.h"

    using namespace std;

    int main()
    {
    ordering A;
    ifstream in;
    bool flag;
    string line;
    in.open("C:\\infile.txt");
    while(! in.eof());
    {getline(in,line);
    A.inorder_insertion(line);
    }
    in.close();
    ofstream out;
    out.open("C:\\outfile.txt");
    flag=A.inorder_first(line);
    while(flag){
    out<<line<<endl;
    flag=A.inorder_next(line);}
    out.close();

    return 0;
    }
     
  2. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
    Is this really what you want to do?

    while(! in.eof());
     
  3. moslem

    Thread Starter New Member

    Dec 16, 2009
    20
    0
    yes i want to read from file till it's end what the problem with that?
     
  4. Harrington

    New Member

    Dec 19, 2009
    86
    3
    hmmmm

    C is a strange langauge Its the most powerful of all langauges You can do anything with C / C++ What you tell it to do it will literally do It doesent ask questions like java or other compilers

    Try this

    Code ( (Unknown Language)):
    1.  
    2. #include <sstream>
    3. #include <fstream>
    4. #include <vector>
    5. #include <iterator>
    6. #include <algorithm>
    7.  
    8. using namespace std;
    9.  
    10. void put_into_vector( ifstream& ifs, vector<int>& v )
    11. {
    12.   string s;
    13.   getline( ifs, s );
    14.   istringstream iss( s );
    15.   // not the fastest method ...
    16.   copy( istream_iterator<int>( iss ), istream_iterator<int>(), back_inserter( v ) );
    17. }
    18.  
    19. int main()
    20. {
    21.   vector<int> line_1, line_2;
    22.  
    23.   ifstream ifs( "data.txt" );
    24.  
    25.   put_into_vector( ifs, line_1 );
    26.   put_into_vector( ifs, line_2 );
    27.  
    28. }
    29.  
     
  5. Harrington

    New Member

    Dec 19, 2009
    86
    3
    Put this another way Whats the difference between this
    Code ( (Unknown Language)):
    1.  
    2. # include "iostream.h"
    3.  
    4.  
    5. void main()
    6.  
    7. {
    8.  
    9.   int c = 4 ;
    10.  
    11.  while( c=4)
    12.  {
    13.   cout << "Hello World " << endl ;  // whats the problem with this ??
    14.   c += 1 ; // increment c by 1
    15.  
    16.  }
    17.  
    18. }
    19.  
    20.  
    And this ??

    Code ( (Unknown Language)):
    1.  
    2. void main()
    3.  
    4. {
    5.  
    6.     int c = 4 ;
    7.  
    8.      while( c==4)
    9.     {
    10.      cout << "Hello World " << endl ;  // whats the problem with this then ??
    11.      c+= 1 ; // increment c  by one
    12.  
    13.      }
    14.  
    15. }
    16.  
    17.  
    Whats your answer ?? Both are valid statements in C++ Both will compile Both will give you entirely different results
     
  6. Harrington

    New Member

    Dec 19, 2009
    86
    3
    Now lets see what happens if you do the same in java First Line of code
    that Ive given you

    Note the difference in the compilers reports

    It wont compile wont let you do it

    Code example
    Code ( (Unknown Language)):
    1.  
    2. import java.io.* ;
    3.  
    4. public class HelloWorld {
    5.  
    6.    
    7.     public static void main (String Args[])
    8.     {
    9.  
    10.         int c = 4 ;
    11.  
    12.          while (c=4)
    13.          {
    14.              System.out.println("Hello World") ; // out Hellworld
    15.  
    16.              c+= 1 ;
    17.  
    18.          }
    19.        
    20.        
    21.     }
    22.  
    23.  
    The compiler reports the following

    Compiling 1 source file to
    HelloWorld.java:20: incompatible types
    found : int
    required: boolean
    while (c=4)
    1 error

    Do you see the big difference between C++ And Java
     
  7. Harrington

    New Member

    Dec 19, 2009
    86
    3
    So the question is Do you really want it to do that In other words "What are you telling this program to do ?? Good question isn't it ??
     
  8. eblc1388

    Senior Member

    Nov 28, 2008
    1,542
    102
    Yes, it is a good question by davebee.

    He(or I think) simply means the semicolon at the end of the While statement, which shouldn't be there if user wants the code block immediately following it to be executed.
     
  9. Harrington

    New Member

    Dec 19, 2009
    86
    3
    Thank you L Chung I had a feeling someone else had worked this out
     
  10. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
    That's right; I was trying to pass along a hint to what looked to me like a problem in hopes that moslem would work it out and gain some debugging skills...
     
  11. Durara

    New Member

    Jul 27, 2010
    1
    0
    debugging skills growing by experiment
     
Loading...