need help in this code

Discussion in 'C++' started by maico, Apr 12, 2009.

  1. maico

    maico Guest

    #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
    maico, Apr 12, 2009
    #1
    1. Advertising

  2. 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

    maico wrote:
    > #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
    Harald Finster, Apr 12, 2009
    #2
    1. Advertising

  3. maico

    maico Guest

    On Apr 12, 1:19 pm, Harald Finster <> wrote:
    > 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
    >
    > maico wrote:
    > > #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

    >
    >


    actually it is showing
    arg=1
    error in arguments
    that's what it's showing...
    rest all is fyn..
    maico, Apr 12, 2009
    #3
  4. maico wrote:

    > 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
    Harald Finster, Apr 12, 2009
    #4
  5. maico

    maico Guest

    On Apr 12, 1:46 pm, Harald Finster <> wrote:
    > maico wrote:
    > > 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


    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
    maico, Apr 12, 2009
    #5
  6. maico

    Helge Kruse Guest

    maico wrote:
    > On Apr 12, 1:46 pm, Harald Finster <> wrote:
    >> maico wrote:
    >>> actually it is showing
    >>> arg=1
    >>> error in arguments


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


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

    >
    > 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
    Helge Kruse, Apr 12, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    2,469
    Edward
    Jun 18, 2004
  2. Teep
    Replies:
    2
    Views:
    764
    Larry Marburger
    Jun 21, 2004
  3. John
    Replies:
    1
    Views:
    392
    John C. Bollinger
    Aug 19, 2004
  4. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    558
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  5. ElementX
    Replies:
    9
    Views:
    488
    RedGrittyBrick
    Oct 1, 2008
Loading...

Share This Page