非法的其他没有匹配的if

时间:2014-04-04 22:42:13

标签: c++

我收到此错误消息,说明非法其他没有匹配if。我认为我的其他陈述有问题,但我不知道在哪里。我有一个不需要的支架吗?它位于第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;

}

1 个答案:

答案 0 :(得分:4)

您的大多数if语句都缺少其开头的大括号。这意味着程序实际上正在执行完全错误的代码,因为如果条件表达式为真且只执行其余代码,则只会执行每个if语句之后的第一个语句。

...这与Apple臭名昭着的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的语法,并且(通常)总是使用大括号括起所有语句块(iffordo,{ {1}}等)以帮助避免像这样的恶意。在我的团队内部,我的团队运行了一个名为StyleCop的程序,如果它包含任何无支撑语句,它将不允许我们将代码签入我们的中央存储库。