Illegal else without matching if, but I have an if after else. :( And how can I round off the results? I mean, if it shows 4.5 it would display 5 and if it's 4.5 it would display 4? Please help me. :(((

Edited 3 Years Ago by pritaeas: Mind your language.

And how can I round off the results? I mean, if it shows 4.5 it would display 5 and if it's 4.5 it would display 4? Please help me.

ceil() and floor() functions will result in what you want.

lets see the example :-

cout<<floor(4.5);
it will round down the number . and so 4

cout<<ceil(4.5)
it will round up the numbere. and so 5

before using these function you are required to include one header file called math.h

And

Illegal else without matching if, but I have an if after else.

perhaps but not sure , did you add curly braces if there is more than one statement to be executed.
so make sure that you have used curly braces for if .. else part.

hope this helps you(however i am not belong to c++ programing) . . .

#include<iostream>
#include<conio.h>
using namespace std;

void main()
{
    int t,de;
    float du,r;
    char a;

menu:
    system("cls");
    cout<<"\n\n\n\t\t\tWelcome to our Loading Central!"<<endl;
    cout<<"\t\t      Powered by all the networks avilable!"<<endl;
    cout<<"\n\tEnter your Time Code. Choose your code below."<<endl;
    cout<<"\n\t\tCODE\t\tTIME"<<endl;
    cout<<"\t\t 1\t\tDay"<<endl;
    cout<<"\t\t 2\t\tNight"<<endl;

    cout<<"\n\tEnter Time Code : ";
    cin>>t;


if (t==1)
{m1:
    system("cls");
    cout<<"\n\tB. Enter your Destination Code. Choose your code below, rates included."<<endl;
    cout<<"\n\t\tCODE\t\tREGION\t\tRATE PER MINUTE"<<endl;
    cout<<"\t\t 1\t\tAmerican\t\t25"<<endl;
    cout<<"\t\t 2\t\tEuropean\t\t20"<<endl;
    cout<<"\t\t 3\t\tAfrican \t\t23.33"<<endl;
    cout<<"\t\t 4\t\tAsian   \t\t20"<<endl;
    cout<<"\n\tEnter Destination Code : ";
    cin>>de;

    if (de==1)
    cout<<"Enter duration in minutes : ";
    cin>>du;
    r=du*25;                
    cout<<"Your total bill is : Php "<<r;

    else if (de==2)
    cout<<"Enter duration in minutes : ";
    cin>>du;
    r=du*20;
    cout<<"Your total bill is : Php "<<r;

    else if (de==3)
    cout<<"Enter duration in minutes : ";
    cin>>du;
    r=du*23.33;
    cout<<"Your total bill is : Php "<<r;

    else if (de==4)
    cout<<"Enter duration in minutes : ";
    cin>>du;
    r=du*20;
    cout<<"Your total bill is : Php "<<r;

    else
    cout<<"YOUR OUTPUT IS INVALID!";
}

else if (t==2)
{m2:
    system("cls");
    cout<<"\n\tB. Enter your Destination Code. Choose your code below, rates included."<<endl;
    cout<<"\n\t\tCODE\t\tREGION\t\tRATE PER MINUTE"<<endl;
    cout<<"\t\t 1\t\tAmerican\t\t20"<<endl;
    cout<<"\t\t 2\t\tEuropean\t\t18.33"<<endl;
    cout<<"\t\t 3\t\tAfrican \t\t20"<<endl;
    cout<<"\t\t 4\t\tAsian   \t\t17.5"<<endl;
    cout<<"\n\tEnter Destination Code : ";
    cin>>de;

    if (de==1)
    cout<<"Enter duration in minutes : ";
    cin>>du;
    r=du*20;
    cout<<"Your total bill is : Php "<<r;

    else if (de==2)
    cout<<"Enter duration in minutes : ";
    cin>>du;
    r=du*18.33;
    cout<<"Your total bill is : Php "<<r;

    else if (de==3)
    cout<<"Enter duration in minutes : ";
    cin>>du;
    r=du*20;
    cout<<"Your total bill is : Php "<<r;

    else if (de==4)
    cout<<"Enter duration in minutes : ";
    cin>>du;
    r=du*17.5;
    cout<<"Your total bill is : Php "<<r;

    else
    cout<<"YOUR OUTPUT IS INVALID!";
    goto m2;
}
else
cout<<"YOUR OUTPUT IS INVALID!";
goto menu;

}

Consider lines 36-42
line 37 belongs to the if (no curly braces used)
So the else if on line 42 will give you an error.
I ALWAYS use curly braces, even if my if else if constructs only contain one line.

This article has been dead for over six months. Start a new discussion instead.