Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C++ experts; Help finish program? file I/O

    Hey guys my program is to read in numbers from input.txt, compute the complex number operation, then output the solutions to output.txt until end of file marker (control z). For some reason it's only outputing 1 line to output.txt, instead of all 5.

    int main()
    {

    int i1, i2, i3 ,i4;
    char op;

    ifstream in("input.txt", ios::in);

    do
    {
    in >> i1 >> i2 >> op >> i3 >> i4;

    ofstream out("output.txt", ios:ut);

    if (op == '*')
    {
    out<<endl<< "(" << i1 << " + " << i2 << "i" << ") " << op << " (" << i3 << " + " << i4 << "i" << ")" << " = ";
    out<< "(" << (i1*i3)-(i2*i4) << " + " << (i2*i3)+(i1*i4) << ")";
    }
    else if (op == '+')
    {
    out<<endl<< "(" << i1 << " + " << i2 << "i" << ") " << op << " (" << i3 << " + " << i4 << "i" << ")" << " = ";
    out<< "(" << i1+i3 << " + " << i2+i4 << "i)";
    }
    else if (op == '-' && i2-i4<0)
    {
    int x = abs(i2-i4);
    out<<endl<< "(" << i1 << " - " << i2 << "i" << ") " << op << " (" << i3 << " - " << i4 << "i" << ")" << " = ";
    out<< "(" << i1-i3 << " + " << x << "i)";
    }
    else if (op == '-' && i2-i4>=0)
    {
    out<<endl<< "(" << i1 << " - " << i2 << "i" << ") " << op << " (" << i3 << " - " << i4 << "i" << ")" << " = "; out<< "(" << i1-i3 << " - " << i2-i4 << "i)";
    }
    }
    while( !in.eof( ) ) ;

    system("pause");
    return 0;
    }





    With this do/while loop or with just while at the top it outputs; (1 + -5i) * (1 + 5i) = (26 + 0) to the 2nd line of output.txt with this as input.txt;

    2 3 - 4 5
    2 3 * 4 1
    3 2 + 7 -8
    3 2 * 7 -8
    1-5 * 1 5







    Anybody want to plunge into this and tell me what im doing wrong? I know its the day before turkey day so you probably have better stuff to do but this is due at midnight thursday night =)

    i thank ya-

    kyle

  • #2
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i just rewrote it using void functions and a case switch.... and it's giving me the EXACT SAME output



    #include <iostream>
    #include <fstream>
    using namespace std;

    struct complex_input
    {
    int i1;
    int i2;
    char op;
    int i3;
    int i4;
    };

    void addition(complex_input p1);
    void subtraction(complex_input p1);
    void multiplication(complex_input p1);

    void main()
    {
    complex_input x;

    ifstream in("input.txt", ios::in);
    {
    do
    {

    in >> x.i1 >> x.i2 >> x.op >>x.i3 >> x.i4;


    switch(x.op)
    {
    case '+' : addition(x);
    break;
    case '-' : subtraction(x);
    break;
    case '*' : multiplication(x);
    break;
    default: cout << "Invalid operator!" << endl;

    }

    }
    while (!in.eof()) ;
    }
    system ("pause");
    }




    void addition(complex_input p1)
    {
    ofstream out("output.txt", ios:ut);
    out << "(" << p1.i1 << " + " << p1.i2 << "i) + (" << p1.i3
    << " + " << p1.i4 << "i) = (" << p1.i1 + p1.i3 << " + " <<
    p1.i2 + p1.i4 << "i)" << endl;
    out.close();



    }


    void multiplication(complex_input p1)
    {
    ofstream out("output.txt", ios:ut);
    out<<endl<< "(" << p1.i1 << " + " << p1.i2 << "i" << ") " << p1.op << " (" << p1.i3 << " + " << p1.i4 << "i" << ")" << " = ";
    out<< "(" << (p1.i1*p1.i3)-(p1.i2*p1.i4) << " + " << (p1.i2*p1.i3)+(p1.i1*p1.i4) << ")";
    out.close();

    }


    void subtraction(complex_input p1)
    {
    ofstream out("output.txt", ios:ut);
    out << "(" << p1.i1 << " + " << p1.i2 << "i) - (" << p1.i3
    << " + " << p1.i4 << "i) = (" << p1.i1- p1.i3 << " + " <<
    p1.i2 - p1.i4 << "i)" << endl;
    out.close();

    }




    I'm about ready to pull my hair out at this point....

  • #3
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok ok ok ok

    i got it

    void addition(complex_input p1, ofstream& out)

    etc etc i just kept writing over the output file

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are probably going to kick yourself when you find out that you are just opening the file in the wrong mode.

    ios::out is implied for all ofstream objsects, so you don't need to specify that, but every function that opens the output.txt file in your program also truncates it. Very simple solution:
    Code:
    	ofstream out("output.txt", ios::app);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •