read in a string, and two ints hen make a choice, compute a value and print the
ID: 3623523 • Letter: R
Question
read in a string, and two ints hen make a choice, compute a value and print the results This program will compute basic integer math. We will use five commands, add, subtract, multiply, divide and remainder. Your program will read this word into a string, then perform the correct calculation on the following two integers. * Input file: Your program must use: problem.txt* Output file: Your program must use: solution.txt
* Do not use any cin statements.
* Do not use system("pause");
Format for the input: The input will have the following form:
<operation>tab<integer>tab<integer>
Where <operation> will be one of the following five commands: add, subtract, multiply, divide or remainder
<integer> will be an integer number.
There will only be one command and two integers in the file.
Samples:
add 1 1
subtract 1 1
multiply 1 1
divide 1 1
remainder 1 1
Output Sample
For this will we make this look like a math problem written vertically. So the corresponding output files to the samples from above would look like this:
1
+ 1
----
2
1
- 1
----
0
1
* 1
----
1
1
/ 1
----
1
1
% 1
----
0
Explanation / Answer
please rate - thanks
you said no cin, since I didn't know if >> could be used I did 2 versions
#include <iostream>
#include <fstream>
using namespace std;
int main()
{ifstream in;
ofstream out;
string op;
int n,m;
in.open("problem.txt");
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
out.open("solution.txt");
in>>op;
while(in)
{in>>n>>m;
out<<" "<<n<<endl;
if(op.compare("add")==0)
out<<"+ "<<m<<" ---- "<<n+m<<endl<<endl;
else if(op.compare("subtract")==0)
out<<"- "<<m<<" ---- "<<n-m<<endl<<endl;
else if(op.compare("multiply")==0)
out<<"* "<<m<<" ---- "<<n*m<<endl<<endl;
else if(op.compare("divide")==0)
out<<"/ "<<m<<" ---- "<<n/m<<endl<<endl;
else if(op.compare("remainder")==0)
out<<"% "<<m<<" ---- "<<n%m<<endl<<endl;
in>>op;
}
out.close();
in.close();
return 0;
}
------------------------------------------------------------------
#include <iostream>
#include <fstream>
using namespace std;
int main()
{ifstream in;
ofstream out;
string op,num;
int n,m,loc,loc2;
string buffer;
in.open("problem.txt");
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
out.open("solution.txt");
getline(in,buffer);
while(in)
{loc=buffer.find(' ',0);
op=buffer.substr(0,loc);
loc++;
loc2=buffer.find(' ',loc);
num=buffer.substr(loc,loc2-loc);
n=num[0]-'0';
loc2++;
loc=buffer.find('',loc2);
num=buffer.substr(loc2,loc-loc2);
m=num[0]-'0';
out<<" "<<n<<endl;
if(op.compare("add")==0)
out<<"+ "<<m<<" ---- "<<n+m<<endl<<endl;
else if(op.compare("subtract")==0)
out<<"- "<<m<<" ---- "<<n-m<<endl<<endl;
else if(op.compare("multiply")==0)
out<<"* "<<m<<" ---- "<<n*m<<endl<<endl;
else if(op.compare("divide")==0)
out<<"/ "<<m<<" ---- "<<n/m<<endl<<endl;
else if(op.compare("remainder")==0)
out<<"% "<<m<<" ---- "<<n%m<<endl<<endl;
getline(in,buffer);
}
out.close();
in.close();
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.