I am facing compiling errors.

The program code
#include<iostream>
#define WINDOWS 1
void console_clear_screen() {
#ifdef WINDOWS
system("cls");
#endif
#ifdef POSIX
system("clear");
#endif
}
using namespace std;
int main(){
a:
system("cls"); //IT'S CLEAR THE SCREEN IN THE CONSOLE ON WINDOWS
//system("clear"); //IT'S CLEAR THE SCREEN IN THE CONSOLE ON POSIX SYSTEM such as macOS and Linux //If you are compiling this on POSIX system such as macOS and Linux then remove forward slash of that code.
float a,a1,s,s1,m,m1,d,d1,t;
char x,y,ch,choice;
cout<<"Welcome to the Basic Simple Calculator Program"<<endl;
cout<<"1. Press a and enter for addition"<<endl;
cout<<"2. Press s and enter for subtraction"<<endl;
cout<<"3. Press m and enter for multiplication"<<endl;
cout<<"4. Press d and enter for division"<<endl;
cout<<"5. Press i and enter for to see about this program and version"<<endl;
cout<<"6. Press q and for exit"<<endl;
cout<<"Enter Choice : ";
cin>>x;
if((x>='a') || (x>='s') || (x>='m') || (x>='d') || (x>='i') || (x>='q') || (x>='A') || (x>='S') || (x>='M') || (x>='D') || (x>='Q'))
{
switch(x)
{
case 'a': //Addition
cout<<"Enter a First No : ";
cin>>a;
cout<<"Enter a Last No : ";
cin>>a1;
t=a+a1;
cout<<"The total is : "<<t <<endl;
break;
case 's': //Subtraction
cout<<"Enter a First No : ";
cin>>s;
cout<<"Enter a Last No : ";
cin>>s1;
t=s-s1;
cout<<"The total is : "<<t <<endl;
break;
case 'm': //Multiplication
cout<<"Enter a First No : ";
cin>>m;
cout<<"Enter a Last No : ";
cin>>m1;
t=m*m1;
cout<<"The total is : "<<t <<endl;
break;
case 'd': //Division
cout<<"Enter a Dividend No : ";
cin>>d;
cout<<"Enter a Divisor No : ";
cin>>d1;
t=d/d1;
if(d1<=0)
{
cout<<"Divide by zero is infinity"<<endl;
}
else
cout<<"The quotient is : "<<t <<endl;
break;
case 'i': //About the Program
cout<<"Simple Basic Calculator"<<endl;
cout<<"Version 1.0"<<endl;
cout<<"Maintained by : Aniruddha Ghosh"<<endl;
cout<<"This is my first basic program on C++"<<endl;
break;
case 'q': //Exit the Program
return 0;
case 'A': //Addition
cout<<"Enter a First No : ";
cin>>a;
cout<<"Enter a Last No : ";
cin>>a1;
t=a+a1;
cout<<"The total is : "<<t <<endl;
break;
case 'S': //Subtraction
cout<<"Enter a First No : ";
cin>>s;
cout<<"Enter a Last No : ";
cin>>s1;
t=s-s1;
cout<<"The total is : "<<t <<endl;
break;
case 'M': //Multiplication
cout<<"Enter a First No : ";
cin>>m;
cout<<"Enter a Last No : ";
cin>>m1;
t=m*m1;
cout<<"The total is : "<<t <<endl;
break;
case 'D': //Division
cout<<"Enter a Dividend No : ";
cin>>d;
cout<<"Enter a Divisor No : ";
cin>>d1;
t=d/d1;
if(d1<=0)
{
cout<<"Divide by zero is infinity"<<endl;
}
else
cout<<"The quotient is : "<<t <<endl;
break;
case 'I': //About the Program
cout<<"Simple Basic Calculator"<<endl;
cout<<"Version 1.0"<<endl;
cout<<"Maintained by : Aniruddha Ghosh"<<endl;
cout<<"This is my first basic program on C++"<<endl;
break;
case 'Q': //Exit the Program
return 0;
default:
cout<<"Invaild entry"<<endl; //Detect wrong entry
}
}
else
{
cout<<"Invaild entry"<<endl; //Detect wrong entry
goto a;
return 0;
}
cout<<"Do you want to continue? (y/n) : ";
cin>>choice;
if(choice == 'y')
goto a;
return 0;
else if(choice == 'n')
cout << "You pressed N! that means the program is end";
return 0;
else
cout <<"Invaild entry";
cin.get();
return 0;
}

Error in compiling is
In function 'int main()':
136 6 [Error] 'else' without a previous 'if'
139 6 [Error] 'else' without a previous 'if'
Last edited on
Hi there

few things
1. Use code tags "["code"]" "["/code"]" in order to make your code easier to read, as at the moment is fairly hard.

2. your error clearly state that you use statement else without statement if.
have a look here http://www.cplusplus.com/doc/tutorial/control/


hope this will help you.
Last edited on
Hello anighosh2017,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

In line 2 all you need is #define WINDOWS the 1 is not necessary. I would suggest using the #ifdef this way:

1
2
3
4
5
6
7
#define WINDOWS  // <--- Comment out for the POSIX version.

#ifdef WINDOWS
  #define CLS system("cls")  // <--- No semi-colon
#else
  #define CLS system("clear") // <--- No semi-colon
#endif // WINDOWS 

This method will eliminate the need for the function which I did not see used any where in the program. In the program all you would have to type is CLS;. The preprossor will change "CLS" to whichever version of the system call that is needed.

Line 14 can be replaced with CLS;.

Line 16 float works, double works better, whereas floats do not always store the decimal portion of a number correctly. The extra byte or two for a double does not make that much difference any more.

Line 27 your first if statement. It is not really need because the entire condition can be handled in the switch. As an idea:

1
2
3
4
5
6
7
8
9
case 'A':
case 'a': //Addition
	cout << "Enter a First No : ";
	cin >> a;
	cout << "Enter a Last No : ";
	cin >> a1;
	t = a + a1;
	cout << "The total is : " << t << endl;
	break;


The "case 'A':" will just fall through to "case 'a'" and execute those instructions until break is reached. The same concept can be used for the other letters leaving the "default" to catch anything that does not match.

The errors you listed:

1
2
136 6 [Error] 'else' without a previous 'if'
139 6 [Error] 'else' without a previous 'if'


Are because of the return 0; on lines 206 and 209. the return 0; is not part if the "if" so it comes between the "if" and the "else" leaving an "else" without an "if" error. Lines 206 and 209 are not needed. If they could ever be reached your program would end and I do not think that is what you want.

The last part I noticed goto a;. Bad form. A goto statement is very rare occurrence in a program these days because there are better accepted ways to loop back to the top like "do while" or "while" loops. I can have a solution to the goto statements when I work with the program shortly.

Hope that helps,

Andy
Topic archived. No new replies allowed.