need help in this code

M

maico

#include<iostream.h>
#include<conio.h>

#include<fstream.h>

#include<stdlib.h>
int main(int argc,char *argv[])
{
int number[9]={11,22,33,44,55,66,77,88,99};
if(argc!=3)
{
cout<<"argc = "<<argc<<"\n";
cout<<"error in args\n";

}
ofstream fout1,fout2;
fout1.open(argv[1]);
if (fout1.fail())
{
cout<<"could t open file";

}
fout2.open(argv[2]);
if (fout2.fail())
{
cout<<"could nt open file";

}
for(int i=0;i<9;i++)
{
if(number%2==0)
fout2<<number<<" ";
else
fout2<<number<<" ";
}
fout1.close();
fout2.close();
getch();
return 0;
}

this code written in turbo c++ compiles & givs success.....
bt showing error while run......
can anyone help me with this code...
pls mail the copy of correct code to (e-mail address removed)
 
H

Harald Finster

Hello,

after a few corrections to make the code portable,
I tested it with g++ and it compiles and runs fine.

As you did not say, what your program is expected to
do and what kind of "error" the program shows,
I can only guess, that you expect numbers with
odd indices be written to the first ('fout1') file
and evenly indexed numbers to the second file.

This would work, if you would modify the if/else
section as follows:

if(number%2==0)
fout1<<number<<" ";
else
fout2<<number<<" ";
}

otherwise all output goes to 'fout2'

Harald
 
M

maico

Hello,

after a few corrections to make the code portable,
I tested it with g++ and it compiles and runs fine.

As you did not say, what your program is expected to
do and what kind of "error" the program shows,
I can only guess, that you expect numbers with
odd indices be written to the first ('fout1') file
and evenly indexed numbers to the second file.

This would work, if you would modify the if/else
section as follows:

if(number%2==0)
 fout1<<number<<" ";
else
 fout2<<number<<" ";

}

otherwise all output goes to 'fout2'

Harald
#include<iostream.h>
#include<conio.h>
#include<fstream.h>

#include<stdlib.h>
int main(int argc,char *argv[])
{
int number[9]={11,22,33,44,55,66,77,88,99};
if(argc!=3)
{
cout<<"argc = "<<argc<<"\n";
cout<<"error in args\n";
}
ofstream fout1,fout2;
fout1.open(argv[1]);
if (fout1.fail())
{
cout<<"could t open file";
}
fout2.open(argv[2]);
if (fout2.fail())
{
cout<<"could nt open file";
}
for(int i=0;i<9;i++)
{
if(number%2==0)
fout2<<number<<" ";
else
fout2<<number<<" ";
}
fout1.close();
fout2.close();
getch();
return 0;
}

this code written in turbo c++ compiles & givs success.....
bt showing error while run......
can anyone help me with this code...
pls mail the copy of correct code to (e-mail address removed)


actually it is showing
arg=1
error in arguments
that's what it's showing...
rest all is fyn..
 
H

Harald Finster

maico said:
actually it is showing
arg=1
error in arguments
that's what it's showing...
rest all is fyn..

sorry, but I get the impression, that you do not have the faintest
idea of what you code is intended to do. Is this your own code?

Your error message indicates, that you did not pass arguments
(filenames) to your program, which are expected.

Harald
 
M

maico

sorry, but I get the impression, that you do not have the faintest
idea of what you code is intended to do. Is this your own code?

Your error message indicates, that you did not pass arguments
(filenames) to your program, which are expected.

Harald

ya actually im nt an expert ahem a beginner.........
& im learning on working with files..so i need help
that's all..no this is nt mine code i got it frm a book
 
H

Helge Kruse

It cant, since your code is:
| cout<<"argc = "<<argc<<"\n";
| cout<<"error in args\n";

ya actually im nt an expert ahem a beginner.........
& im learning on working with files..so i need help
that's all..no this is nt mine code i got it frm a book

I would go back to reading the book, to find that you need to pass two
arguments at the command line, when you start the program. See:
| fout1.open(argv[1]);
| fout2.open(argv[2]);

If you want to run the program in a debugger, see the debugger's manual
how to pass command line arguments to the program.

Helge
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top