expected unqualified-id before ‘+’ token

Hi guys,

Can anybody help me with this error..

expected unqualified-id before ‘+’ token ............it is in line..... char op1, +, -, *, /, s;

my code is :


#include <iostream>

using namespace std;

float add(float,float);

float sub(float,float);

float product(float,float);

float divide(float,float);

void swap(float,float);


float add(float x,float y) // Function for addition operation

{

return(x+y);

}


float sub(float x,float y) // Function for subtract operation

{

return(x-y);

}

float product(float x,float y) // Function for multiply operation

{

return(x*y);

}

float divide(float x,float y) // Function for devide operation

{

return(x/y);

}

void swap(float x,float y) // Function for swap operation

{

x -= y;

y += x; // value of x is stored in y

x = (y - x); // value of y is stored in x

}

void main()

{

float num1, num2;

char op1, +, -, *, /, s;




cout << " Welcome to the calculator program v.1.0 written by Mohit Kashyap"<<endl;

cout << "Please enter an operation which you would like to calculate (+, -, *, /): " <<op1<< endl;

cin >> op1;

cout <<"Please enter two numbers to apply your requested operation("<<op1<<"), 1st one :"<<num1<<endl;

cin>>num1;

cout <<" 2nd one :"<<num2<<endl;

cin>>num2;



switch (op1)

{

case '+':

cout<<"The addition of two numbers ("<<num1<<","<<num2<<")is: "<<add(num1,num2)<<endl;

break;



case '-':

cout<<"The subtraction of two numbers ("<<num1<<","<<num2<<"is: "<<sub(num1,num2)<<endl;

break;



case '*':

cout<<"The multipication of two numbers ("<<num1<<","<<num2<<"is: "<<product(num1,num2)<<endl;

break;



case '/':

cout<<"The devide of two numbers ("<<num1<<","<<num2<<"is: "<<divide(num1,num2)<<endl;

break;



case 's':

swap(num1,num2);

cout<<"The numbers after swaping are :"<<num1<<","<<num2<<endl;

break;



default:



cout<<"This is not a valid input"<<endl;

break;

}


return 0;


}

I will really apriciate it....
Last edited on
When declaring a variable, you only have to give the variable name. You don't need to give all possible values for the variable.

char op1, +, -, *, /, s;

This tells the compiler you want to create 6 different variables. One named "op1", one named "+", one named "-", etc.

Of course, +, -, *, etc are invalid names for variables because variable names can't have symbols. So the compiler is giving you an error.

Since all you want to do here is create a single variable named 'op1', just do this:

char op1;
closed account (zwA4jE8b)
following the last reply
 
op1 = '+'; //is valid 
Topic archived. No new replies allowed.