assistance with file program

Hello Everyone I am a bit stuck on this program, if you can offer some insight to what may be wrong or any tips to get my code in working order I will appreciated and thank you in advance.


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

int main()
{
ifstream infile;
infile.open("math.txt");
if(infile.fail()){
cout<<"error!"<<endl;
}
int x, y;
char op;

while(infile>>op>>x>>y){

if(op=='+'){
cout<<x+y<<endl;
}
if(op=='-'){
cout<<x-y<<endl;
}
if(op=='*'){
cout<<x*y<<endl;
}
if(op=='/'){
cout<<x/y<<endl;
}
if(op=='^'){
cout<<pow(x,y)<<endl;
}

}

infile.close();

return 0;
}

//this is the file and the output is error!

the math file is :
+ 5 5
- 3 1
* 9 2
/ 10 2
^ 2 2
The code is ok. I get the following output:
10
2
18
5
4


So what's the problem? Well the output "error!" means the file could not be opened. Usually that is because it is in the wrong folder or directory. Try placing "math.txt" either in the same location as the executable (.exe on windows) or as the source code .cpp (depending on which IDE is used and how the program is run).
Topic archived. No new replies allowed.