C++ 不匹配if的else非法
所以我试着建立一个简单的计算器,但我不断得到同样的错误“非法的其他不匹配如果”在我把第二个其他我搜索了其他答案,但似乎没有帮助,如果有人能给我任何提示来修复这个问题,我将不胜感激C++ 不匹配if的else非法,c++,C++,所以我试着建立一个简单的计算器,但我不断得到同样的错误“非法的其他不匹配如果”在我把第二个其他我搜索了其他答案,但似乎没有帮助,如果有人能给我任何提示来修复这个问题,我将不胜感激 #include "stdafx.h" #include <iostream> using namespace std; int main() { int number_1; cout << "" << endl; cin &g
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
int number_1;
cout << "" << endl;
cin >> number_1;
int number_2;
cout << "" << endl;
cin >> number_2;
int number_3 = number_1 + number_2;
int number_4 = number_1 - number_2;
int number_5 = number_1 * number_2;
int number_6 = number_1 / number_2;
if (number_1 + number_2) {
cout << "" << number_3 << endl;
}
else {
if (number_1 - number_2) {
cout << "" << number_4 << endl;
}
}
else {
if (number_1 * number_2){
cout << "" << number_5 << endl; 4
}
}
else {
if (number_1 / number_2) {
cout << "" << number_6 << endl;
}
}
cout << "Press enter to continue" << endl;
cin.ignore(10, '\n');
cin.get();
return 0;
}
#包括“stdafx.h”
#包括
使用名称空间std;
int main(){
整数_1;
cout数_1;
整数_2;
cout数_2;
整数3=整数1+整数2;
int number_4=number_1-number_2;
int number_5=number_1*number_2;
int number_6=number_1/number_2;
如果(编号1+编号2){
cout这是因为每个else
语句(不包括最后一个块)都必须与if
块相关联。如果您想像尝试一样不断迭代这些选项,则需要将代码更改为:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
int number_1;
cout << "" << endl;
cin >> number_1;
int number_2;
cout << "" << endl;
cin >> number_2;
int number_3 = number_1 + number_2;
int number_4 = number_1 - number_2;
int number_5 = number_1 * number_2;
int number_6 = number_1 / number_2;
if (number_1 + number_2)
cout << "" << number_3 << endl;
else if (number_1 - number_2)
cout << "" << number_4 << endl;
else if (number_1 * number_2)
cout << "" << number_5 << endl; 4
else if (number_1 / number_2)
cout << "" << number_6 << endl;
cout << "Press enter to continue" << endl;
cin.ignore(10, '\n');
cin.get();
return 0;
}
请记住:if(/*somecondition*/)
后面跟着您需要的else if(/*somecondition*/)
,然后完成您的else
块
而且
需要注意的是,您实际上并不是在测试if
语句中的任何条件,而是在完成操作。您必须检查某种类型的值(布尔值、数字值或其他值),例如:
if (number_1 + number_2 != 0)
cout << "" << number_3 << endl;
else if (number_1 - number_2 != 0)
cout << "" << number_4 << endl;
else if (number_1 * number_2 != 0)
cout << "" << number_5 << endl; 4
else if (number_1 / number_2 != 0)
cout << "" << number_6 << endl;
else cout << "Invalid number." << endl;
if(数字1+2!=0)
cout当我看到你的直接问题和你提供的代码,以及我看到已经回答了你的问题的答案和评论时,这里有几件事需要首先记住,这是一条简单的规则,也适用于这样做的风格
- if…else if…else{}块语句的规则。
- 第一条语句以if(条件| |一组条件)开头,该条件将通过条件布尔比较或逻辑数学计算比较计算为true或false
- 示例:
- 布尔值:
if(某物(x)为真或假)
- 逻辑:
如果(somevalue(x)是,=,!=,==somevalue(y)或对(x)或(y)或两者都执行的函数或操作)
- 下一步是指定block语句的开头,这可能略有不同:
- 当且仅当If语句后面有一行执行时,可以省略开头和结尾大括号
{}
,后面可以是If-else(…)
或else
语句
- 如果需要在这些
块语句的范围内执行多行代码,则打开和关闭大括号{}
需要存在以定义封闭块语句,以便定义其范围。*如果不确定,请查找范围
和块
:所有块-以{
开始并以}
结束的代码段有其自己的局部范围
- 返回:-当在不返回值的函数中找到这些
if…else if…else
语句时,应调用的唯一内容是return;
语句(当条件给出从该函数返回的原因时);并非所有if else if else
状态都会导致函数终止,但ny do,否则,如果该函数确实具有返回值
,并且执行if else if else
语句范围内的某个代码块会导致从该函数返回,则该语句的所有块也应返回该函数的数据类型或引发某种类型的错误或异常
- 风格
- 打开和关闭支架的缩进和使用
{}
- 间距
- 嵌套循环
// Here are a couple of different styles
// Tip about indentation - single tab normally 4-5 white spaces
// with 4 being most common sometimes 3, but very rare:
// I prefer 4. Which ever indent spacing you decide to choose
// make sure all of your code follows that convention.
// Style 1a: Single Execution with omitted braces
if ( some condition ) // Braces {} omitted
// single execution; // Single Execution without any following
// else if...else blocks
// Next statement of execution that doesn't belong to the above if;
// Style 1b: Single Execution with braces
if ( some condition )
{ // Beginning Brace Started on separate line.
// single execution;
}
// next statement not belonging to if statement;
// Style 1c: Single Execution with braces
if ( some condition ) { // Opening Brace on Same Line as if statement.
// some execution;
}
// next statement not belonging to if statement;
// Style 2: Concerning else, if else, and if else else when using braces and indentation
// Style 2a: Opening and closing block statement as well as conditions on separate lines.
if ( condition )
{
// Statement or statements of execution;
}
else if ( some other condition )
{
// Statement or statements of execution;
}
else
{
// Statement or statements of execution;
}
// Style 2b: More compact
if ( condition ) {
// Statement or statements of execution;
} else if ( some other condition ) {
// Statement or statements of execution;
} else {
// Statement or statements of execution;
}
// These can be applied to nested loops as well.
我更喜欢带有起始大括号的紧凑版本,这些大括号定义了以下任何一项的新范围:命名空间、类声明、函数定义、枚举、switch语句及其case语句、if…else if…else语句、while和do while循环、for循环以及try和catch块语句。我更喜欢默认选项卡或4个空格,用于所有嵌套代码块语句或new(嵌套范围)的所有缩进。我更喜欢将我的else if和else保持在初始if语句右括号的同一行,以便知道它属于该代码分支。示例
MyStyle.cpp
int main() {
if ( condition || conditions ) {
// Do Work;
} else if ( some other condition || conditions ) {
// Do This Instead;
} else {
// Otherwise; Finish with this;
}
// Nested If else if else
if ( ... ) {
if ( ... ) {
// code;
} else if ( ... ) {
// code;
if ( ... ) {
// code;
} else {
// code;
}
// maybe more code;
} else {
// some other code;
}
} else if ( ... ) {
if ( ... ) {
// code;
}
if ( ... ) {
// code;
}
if ( ... ) {
// code;
} else {
// code;
}
} else {
// code;
}
return 0;
}
关于您选择的样式的一般想法应该是:选择适合您的样式,只要该样式保持可读性并且保持简单,就坚持使用该样式,以便其他读者或用户能够轻松地遵循代码块或您的学校、工作或团队使用的样式
关于你的代码和你的if语句的条件,考虑这个。
if ( 3 + 6 ) {
// print 3+6;
}
// The 3 + 6 either if using hard coded numbers or variable
// is an expression and it will evaluate to some value.
// Does this make sense to you?
if ( 9 ) {
// do something;
}
// A constant value or variable, a constant expression will
// always evaluate to true if used as a condition.
// How can 9 every return false?
// If statements are designed to handle true or false conditions or comparisons.
// Boolean
bool imHappy = true;
if ( imHappy ) {
// print( smiley face );
} else {
// print ( frown );
}
// Or it could be written this way as well
if ( !imHappy ) {
// print( frown );
} else {
// print( smiley face );
}
// Logical Comparison
const int adultAge = 18;
const int legalAge = 21;
int consumersAge = 0;
std::cout << "Can I see your ID?" << std::endl;
std::cin >> consumerAge; // Read of from DOB on ID
if ( consumerAge > 0 && consumerAge < adultAge ) {
std::cout << "Sorry we can not do any of the following: \n"
<< "Sell you any tobacco products\n"
<< "Serve you alcohol\n"
<< "Sell you lottery or allow you to gamble." << std::endl;
else if ( consumerAge >= adultAge && consumerAge < legalAge ) {
std::cout << "Sorry we can not do any of the following: \n"
<< "Serve you Alcohol or Allow You To Gamble. \n"
<< "You are able to purchase: Tobacco and Lottery Tickets." << std::endl;
} else if ( consumerAge >= legalAge ) {
std::cout << "You may purchase everything we sell and you are allowed to gamble." << std::endl;
} else {
std::cout << "Invalid age entered: Are you even born yet?\n"
<< "You can not be " << consumerAge << " years old!" << std::endl;
}
if(3+6){
//打印3+6;
}
//如果使用硬编码数字或变量,则为3+6
//是一个表达式,它将计算为某个值。
//这对你有意义吗?
如果(9){
//做点什么;
}
//对于常量值或变量,常量表达式将
//如果用作条件,则始终计算为true。
//9每一个怎么能归假?
//如果语句设计用于处理真或假条件或比较。
//布尔值
bool imHappy=true;
如果(我很高兴){
//印花(笑脸);
}否则{
//打印(皱眉);
}
//或者也可以这样写
如果(!我很高兴){
//打印(皱眉);
}否则{
//印花(笑脸);
}
//逻辑比较
成年常数=18;
const int legalAge=21;
int consumersAge=0;
std::cout consumerAge;//从ID上的DOB读取
if(消费率>0&&consumerAge std::如果{
构造,那么你的else{
构造应该是else。只需检查{
和}
的数量,你就会看到
if ( 3 + 6 ) {
// print 3+6;
}
// The 3 + 6 either if using hard coded numbers or variable
// is an expression and it will evaluate to some value.
// Does this make sense to you?
if ( 9 ) {
// do something;
}
// A constant value or variable, a constant expression will
// always evaluate to true if used as a condition.
// How can 9 every return false?
// If statements are designed to handle true or false conditions or comparisons.
// Boolean
bool imHappy = true;
if ( imHappy ) {
// print( smiley face );
} else {
// print ( frown );
}
// Or it could be written this way as well
if ( !imHappy ) {
// print( frown );
} else {
// print( smiley face );
}
// Logical Comparison
const int adultAge = 18;
const int legalAge = 21;
int consumersAge = 0;
std::cout << "Can I see your ID?" << std::endl;
std::cin >> consumerAge; // Read of from DOB on ID
if ( consumerAge > 0 && consumerAge < adultAge ) {
std::cout << "Sorry we can not do any of the following: \n"
<< "Sell you any tobacco products\n"
<< "Serve you alcohol\n"
<< "Sell you lottery or allow you to gamble." << std::endl;
else if ( consumerAge >= adultAge && consumerAge < legalAge ) {
std::cout << "Sorry we can not do any of the following: \n"
<< "Serve you Alcohol or Allow You To Gamble. \n"
<< "You are able to purchase: Tobacco and Lottery Tickets." << std::endl;
} else if ( consumerAge >= legalAge ) {
std::cout << "You may purchase everything we sell and you are allowed to gamble." << std::endl;
} else {
std::cout << "Invalid age entered: Are you even born yet?\n"
<< "You can not be " << consumerAge << " years old!" << std::endl;
}