为什么最后一个else报错。error: ‘else’ without a previous ‘if’



```c++

#include<iostream>
using namespace std;
int main()
{
    char A,B,C,D;
    int P,x,ch;
    cin>>ch>>x;
    if(ch==A)
        P=19.99*x;
    else if(ch==B)
        P=17.99*x;
    else if(ch==C)
        P=15.99*x;
    else if(ch==D)
        P=11.99*x;
    cout<<P<<endl;
    else
        cout<<"No avialable"<<endl;
    return 0;
}

    else if(ch==D)
        P=11.99*x;
    cout<<P<<endl;
    //改为
    else if(ch==D)
{
        P=11.99*x;
    cout<<P<<endl;
}

最后一个else if内的输出没对齐,要么就把花括号加上,避免这种小错误

if...else使用时如果语句内只有一行代码,可以不用括号简写,超过一行代码需要用大括号括起来

用if else 语句时,要加上花括号,你把cout<<P<<endl; 去掉就不会报错了,要是不去掉,就需要加花括号
if()
{
}
else if()
{
}
else if()
{
}
else
{
}

#include<iostream>
using namespace std;
int main()
{
    char A,B,C,D;
    int P,x,ch;
    cin>>ch>>x;
    if(ch==A)
        P=19.99*x;
        cout<<P<<endl;
    else if(ch==B)
        P=17.99*x;
        cout<<P<<endl;
    else if(ch==C)
        P=15.99*x;
        cout<<P<<endl;
    else if(ch==D)
        P=11.99*x;
        cout<<P<<endl;
    else
        cout<<"No avialable"<<endl;
    return 0;
}