否则非法,不匹配如果

Illegal else without matching if

本文关键字:不匹配 如果 非法      更新时间:2023-10-16

我收到此错误消息,说非法否则不匹配if。我认为我的 else 陈述有问题,但我看不出在哪里。我有不需要的支架吗?它位于第 78 行第 2 列。谢谢

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
string service_code,            //hold the service code 
       account_number;          //hold the account number
double final_amount = 0,        // hold the final amount
       final_damount,           //hold the amount for the day minutes
       minutes = 0,             //hold the amount for minutes
       day_minutes = 0,         // hold the amount for fay minutes
       night_minutes = 0,      //hold the amount for night minutes
       final_namount = 0;        //hold the amount for night minutes

cout << "Please enter your account number: "; 
cin >> account_number;      //Entering the account number                       
cout << "Please enter your service code (r or R for regular service and p or P for premium service): "; 
cin >> service_code;        //Enteringthe service code
cout << "Please enter the amount of minutes used: ";
cin >> minutes;             //Entering the minutes you used
if(service_code == "r" || "R")
{
if(minutes <= 50)
    final_amount = 10;
    cout << "Your final amount is $: " << final_amount << endl;         //Displaying final amount when your minutes are less than or equal to 50
}
    {
    if(minutes > 50)
    final_amount = (minutes - 50) * 0.20 + 10;
    cout << "Your final amount is: $ " << final_amount << endl;         //Displaying final amount when your minutes are greater than 50
}
    {
     else if(service_code == "p" || "P")
        cout << "Please enter the amount of minutes used during the day: " << day_minutes << endl;              //Entering minutes used during the day
        cout << "Please enter the amount of minutes used during the night: " << night_minutes << endl;          //Entering minutes used during the night
    }
        {
if(day_minutes <=75)
    final_damount = 0;
    final_amount = final_damount + final_namount + 20;              //Calculating final amount for minutes used during the day
        }
        {
 if(day_minutes > 75)
    final_damount = day_minutes * 0.10;
    final_amount = final_damount + final_namount + 20;              //Calcuating final amount for minutes used during the day
}
    {
if(night_minutes <= 100)
    final_namount = 0;
    final_amount = final_damount + final_namount + 20;              //Calcuating final amount for minutes used during the night
    }
{
if(night_minutes > 100)
    final_namount = night_minutes * 0.05;
    final_amount = final_damount + final_namount + 20;              //Calcuating final amount for minutes used during the night
    cout << "Your final amount is: $ " << final_amount << endl;     //Displaying final amount
}
{
else
    cout << "Error, this program does not accept negative numbers.n";      //Displaying error message
    cout << "Account number: " << account_number << endl;           //Displaying account number
    cout << "Service code: " << service_code << endl;               //Displaying service code
    cout << "Service code: " << minutes << endl;                    //Displaying minutes
}
return 0;
}

您的大多数if语句都缺少其左大括号。这意味着完全错误的代码实际上是由您的程序执行的,因为如果条件表达式为 true,则只会执行每个 if 语句后的第一个语句,并且其余代码将始终执行。

。这与苹果臭名昭著的goto error错误相同:http://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/

C不是Python,编译器不尊重缩进,这并不意味着代码属于某个关键字或块。

我强烈建议阅读 C 的语法,并且(通常)在所有语句块(iffordowhile 等)中使用大括号是一个好主意,以帮助避免此类讨厌的内容。在内部,我在Microsoft的团队运行一个名为StyleCop的程序,如果它包含任何无括号的语句,它将不允许我们将代码签入到我们的中央存储库。