否则非法,不匹配如果
Illegal else without matching if
我收到此错误消息,说非法否则不匹配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 的语法,并且(通常)在所有语句块(if
、for
、do
、while
等)中使用大括号是一个好主意,以帮助避免此类讨厌的内容。在内部,我在Microsoft的团队运行一个名为StyleCop的程序,如果它包含任何无括号的语句,它将不允许我们将代码签入到我们的中央存储库。
相关文章:
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 如果在开关情况下不匹配,为什么此函数返回 ASCII 值?
- 如果枚举标签与类型不匹配,编译器是否应该发出警告?
- 否则非法,不匹配如果
- 如果字符位于引号之间,则不匹配(AKA具有编程字符串模式)