hi, i am pretty new at using c++. i am trying to read a mathematical expression from a file but i keep getting errors. i am using the cin.get function to read it but it isnt working. can anyone help me please. this is what i have. the file being read contains
3+4. i am trying to read it in and execute it.

#include<iostream>
#include <fstream>
using namespace std;  
int main()
{
        
ifstream infile;
ofstream outfile;
char sign;
int answer,num1,num2;

 
infile.open("math.txt");
outfile.open("output.txt");
if (!infile)
        cout<<"Could Not Open File"<<endl;
else
{
while(!infile.eof())
{
infile.ignore();
infile.get(num1);
infile.get(sign);
infile.get(num2);
num1 sign num2 = answer;

outfile<<num1<<sign<<num2<<"="<<answer<<endl;
}
}

You don't have to use cin.get for that purpose, just do something like this: infile >> [I]yourvariable[/I]; :) ...

Edit:: num1 sign num2 = answer; will never work (even if you had typed num1 + num2 = answer; it won't work) ...
You'll have to use something like a switch statement to parse the expression:

switch(sign)
{
case '+':
    /* Your code for addition here */
    break;
case '-':
    /* Your code for subtraction here */
    break;
case '*':
    /* Your code for multiplication here */
    break;
case '/':
    /* Your code for division here */
    break;
default:
    cout << "Unknown operator\n";
}

thanks i took out the cin.get and just used infile. that really helped. now i get a parse error here though.

infile>>num1;
infile>>sign; 
infile>>num2; 
num1 sign num2 = answer; //parse error here for some reason

>> num1 sign num2 = answer; is not a valid C++ expression

>> e.g.: (this code will never work)

char sign = '+';
int n1 = 5, n2 = 7, result;
result = n1 sign n2; // won't compile

>> This code is the correct way to do this:

char sign = '+';
int n1 = 5, n2 = 7, result;
if(sign == '+') result = n1 + n2; // will compile :)

use infile.getline(name of your array,size,delmiter);
it will read the whole line,and you are reading the '+' sign in character form yo cannot perform any operations on it!.

another way to solve this problem is to read the intergers in integers!
lile these lines:

void main()
{
int a,b;
char operator;

ifstream infile;

infile.open("whatver.txt");
infile >> a;  //read the first integer
infile >> operator;  //operator

infile >> b;   //second integer

}

now you have to figure out how to use the operator which is in character form!.....hint(use ascii values to compare)

i hope you get it right!

another way to solve this problem is to read the intergers in integers!

That's actually what he was doing right now :) ...

and you are reading the '+' sign in character form yo cannot perform any operations on it!.

And what's this?

char sign = '+';
int n1 = 5, n2 = 7, result;
if(sign == '+') result = n1 + n2; // will compile :)

Comparing a variable to another one, isn't that an operation?
BTW, Reading the expression as a whole line isn't very useful here as the expressions are always like a+b , a-b , a*b , a/d ...

To the OP:
> When performing the division it might be useful to check first whether the second operand is a zero ...
> If you're reading the line which contains the expression using getline , it might be useful to create a simple SkipSpaces function which skips over the spaces, so you can perform operations like this: a *b or a -__________b (where '_' is a space) or ... instead of only a+b , etc ...

And what's this?

char sign = '+';
int n1 = 5, n2 = 7, result;
if(sign == '+') result = n1 + n2; // will compile :)

Comparing a variable to another one, isn't that an operation?
BTW, Reading the expression as a whole line isn't very useful here as the expressions are always like a+b , a-b , a*b , a/d ...

To the OP:
> When performing the division it might be useful to check first whether the second operand is a zero ...
> If you're reading the line which contains the expression using getline , it might be useful to create a simple SkipSpaces function which skips over the spaces, so you can perform operations like this: a *b or a -__________b (where '_' is a space) or ... instead of only a+b , etc ...

i said operator,not operation.
you can also solve this by reading the whole line and then making tokens with strtok(array,delimiters). or use if statements to solve the variable like...

if(operator=='+')
{

int result = a + b;

}
else if(operator=='-')
{
int result = a + b;

}

using extraction operator skips the spaces or '\n'

no need for a skip space function!

....

btw does anyone know about using treeview in windows forms application,then please let me know!

or use if statements to solve the variable like...

if(operator=='+')
{

int result = a + b;

}
else if(operator=='-')
{
int result = a + b;

}

Actually I see a better way: use the switch statement for that purpose (As I already mentioned in one of my previous posts)

switch(sign)
{
case '+':
    /* Your code for addition here */
    break;
case '-':
    /* Your code for subtraction here */
    break;
case '*':
    /* Your code for multiplication here */
    break;
case '/':
    /* Your code for division here */
    break;
default:
    cout << "Unknown operator\n";
}
else if(operator=='-')
{
    int result = a + b;
}

Dear arshad take a look at your code:

else if(operator=='-')
{
    int result = a [B]+[/B] b;
}

that has to be

else if(operator=='-')
{
    int result = a [B]-[/B] b;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.