C++ 中的预期表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15037787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
expected expression in C++?
提问by user2101748
#include <iostream>
using namespace std;
void num7()
{
int numRows;
cin >> numRows;
for (int x = 0; x < numRows/2; x++) {
for (int y = 0; y <= x; y++) {
cout << "*";
}
cout << "\n";
}
float numRowsfloat = numRows;
double cos = numRowsfloat / 2;
int tan = numRowsfloat / 2;
double sin = tan;
if (cos == sin)
cout << "\n";
for (int x = 0; x < numRows/2; x++) {
for (int y = numRows/2; y >0; y--) {
cout << "*";
}
}
else
for (int x = 0; x < numRows/2+1; x++) {
for (int y = x; y >0; y--) {
cout << "*";
}
cout << "\n";
}
}
In the else column, it says expected expression. This is trying to make a triangular shape. like
在 else 列中,它表示预期的表达式。这是试图制作一个三角形。喜欢
*
**
***
***
**
*
for inputed 6 or
对于输入 6 或
*
**
***
**
*
for inputed 5
对于输入 5
回答by Some programmer dude
You problem is this:
你的问题是这样的:
if (cos == sin)
cout << "\n";
for (int x = 0; x < numRows/2; x++) {
for (int y = numRows/2; y >0; y--) {
cout << "*";
}
}
Here only the cout
is part of the if
statement. The loop is not. You need to add braces around the whole block.
这里仅cout
是if
声明的一部分。循环不是。您需要在整个块周围添加大括号。
回答by Tarandeep Gill
You forgot the braces for the if statement. try this:
你忘记了 if 语句的大括号。尝试这个:
if (cos == sin) {
cout << "\n";
for (int x = 0; x < numRows/2; x++) {
for (int y = numRows/2; y >0; y--) {
cout << "*";
}
}
} else
for (int x = 0; x < numRows/2+1; x++) {
for (int y = x; y >0; y--) {
cout << "*";
}
cout << "\n";
}