I've written a pretty simple calculator program in C++ (haven't we all?), and I've worked out all the errors; it seems to run fine, except that if the answer to the problem is a large answer, the program keeps looping (which I set it up to do), but won't stop on cin's. I'm pretty new to C++, and I tried searching several times for clues as to the problem, but couldn't find anything. I know from seeing a million others get their asses ripped for posting entire programs, that you're not supposed to, but I don't know how else to explain my problem, and ask for any suggestions. So here's a good bit of my program. I don't expect you to rewrite my program, this is just practice for me anyway, but any suggestions on making my proggie shorter and more efficient would be appreciated. Thanks.
--LiKWiD
Becoming un-noobified one day at a time.
Code:
int op, rem;
int res = 1;
double x1, x2, x3, x4;
double tmpsum = 0.1337;
double tmpsum2 = 0.1337;
double tmpsum3 = 0.1337;
char a1[2], a2[2], a3[2], problem[18];
char add[2] = "+";
char subtract[2] = "-";
char multiply[2] = "*";
char divide[2] = "/";
/* Organizes user input ('problem[18]') for processing */
void ioProb()
{
ofstream oprob("problem.dat", ios::trunc); // Outputs user input to file
oprob<<problem;
oprob.close();
ifstream iprob("problem.dat", ios::nocreate); // Inputs and organizes user input from file
iprob>>x1>>a1>>x2>>a2>>x3>>a3>>x4>>rem>>rem;
iprob.close();
}
/* Identifies and performs the math operation(s) */
void exeArith()
{
cout<<"Please enter the problem you wish solved: "<<endl<<endl
<<" ";
cin.getline(problem, 11, '\n');
ioProb();
op = strcmp(a1, add);
if (op == 0)
{
tmpsum = x1 + x2;
}
op = strcmp(a1, subtract);
if (op == 0)
{
tmpsum = x1 - x2;
}
op = strcmp(a1, multiply);
if (op == 0)
{
tmpsum = x1 * x2;
}
/* ... I cut out a lot of the IF statement here to shorten it some.. you get the point. */
op = strcmp(a3, divide);
if (op == 0)
{
tmpsum3 = tmpsum2 / x4;
}
if (tmpsum != 0.1337 && tmpsum2 != 0.1337 && tmpsum3 == 0.1337)
{
cout<<tmpsum2<<endl;
}
if (tmpsum3 != 0.1337)
{
cout<<tmpsum3<<endl;
}
}
/* Calculates new problem or quits program. */
void newProb()
{
cout<<endl<<endl
<<"Solve another problem or quit?"<<endl
<<" 1- Solve another"<<endl
<<" 2- Quit"<<endl;
cin>>res;
cin.ignore();
}
/* MAIN() */
int main()
{
cout<<"================================================"<<endl
<<"-- Welcome to LiKWiD's --"<<endl
<<"-- ARITHMETIC v1.0 - (SYNTAX SPECIFIC) --"<<endl
<<"------------------------------------------------"<<endl<<endl
<<" Syntax: x + y"<<endl
<<" x - y"<<endl
<<" x * y"<<endl
<<" x / y"<<endl
<<" x + y * z"<<endl<<endl<<endl;
while (res == 1)
{
exeArith();
newProb();
}
return 0;
}