what's the wrong with my code?

Thread Starter

moslem

Joined Dec 16, 2009
20
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;
}
 

Harrington

Joined Dec 19, 2009
85
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

Rich (BB code):
#include <sstream>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

void put_into_vector( ifstream& ifs, vector<int>& v )
{
  string s;
  getline( ifs, s );
  istringstream iss( s );
  // not the fastest method ... 
  copy( istream_iterator<int>( iss ), istream_iterator<int>(), back_inserter( v ) );
}

int main()
{
  vector<int> line_1, line_2;

  ifstream ifs( "data.txt" );

  put_into_vector( ifs, line_1 );
  put_into_vector( ifs, line_2 );

}
 

Harrington

Joined Dec 19, 2009
85
Put this another way Whats the difference between this
Rich (BB code):
# include "iostream.h"


void main()

{
 
  int c = 4 ;

 while( c=4)
 {
  cout << "Hello World " << endl ;  // whats the problem with this ?? 
  c += 1 ; // increment c by 1 

 }

}
And this ??

Rich (BB code):
void main()

{
 
    int c = 4 ;

     while( c==4)
    {
     cout << "Hello World " << endl ;  // whats the problem with this then ??
     c+= 1 ; // increment c  by one 

     }

}
Whats your answer ?? Both are valid statements in C++ Both will compile Both will give you entirely different results
 

Harrington

Joined Dec 19, 2009
85
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
Rich (BB code):
import java.io.* ;

public class HelloWorld {

    
    public static void main (String Args[])
    {

        int c = 4 ;

         while (c=4)
         {
             System.out.println("Hello World") ; // out Hellworld

             c+= 1 ;

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

eblc1388

Joined Nov 28, 2008
1,542
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 ??
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.
 

davebee

Joined Oct 22, 2008
540
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...
 
Top