...

View Full Version : C++ experts; Help finish program? file I/O



hiroler
11-25-2004, 03:22 AM
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::out);

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

hiroler
11-25-2004, 04:39 AM
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::out);
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::out);
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::out);
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....

hiroler
11-25-2004, 07:57 AM
ok ok ok ok

i got it

void addition(complex_input p1, ofstream& out)

etc etc i just kept writing over the output file :eek:

aman
11-25-2004, 08:03 AM
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:


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum