有人可以解释为什么该程序显示我 6 和 4
Can someone explain why the program shows me 6 and 4?
在这个问题中,这是一个简单而简单的问题,我做总和和乘积。我想用一个"for"指令来理解这个东西是如何工作的。
#include < iostream >
using namespace std;
int main()
{
int n,i,s=0,p=1;
cin>>n;
for (i=1;i<=n;i++)
s=s+i;
p=p*i;
cout<<s<<" "<<p;
}
我无法解释为什么结果是总和"6"和乘积"4"......
有人可以解释我为什么代码显示这一点吗?如果我将"for"结构中的指令放在大括号之间,它显示"6"表示总和,"6"表示乘积。
首先,它不是< iostream >
,而是<iostream>
。那里不允许有空格。其次,尽管有缩进,p=p*i;
还是在for
环之外。打开编译器警告:
prog.cc:7:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
7 | for (i=1;i<=n;i++)
| ^~~
prog.cc:9:10: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
9 | p=p*i;
| ^
使用{}
解决此问题:
for (i=1;i<=n;i++)
{
s=s+i;
p=p*i;
}
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- 显示错误输出的简单数组排序程序
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 密码登录程序将永远循环并显示不正确的结果
- 为什么我的程序不能显示斐波那契级数?
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- 在 txt 文件中显示前两个数字的程序
- 为什么下面的Hello World程序在PowerShell上没有显示任何输出?同一程序在CMD上显示正确的输出
- C++程序工作,但 Windows 显示"程序.exe已停止工作"
- 终端不显示程序的警告.如何在终端中看到它们
- VS代码不会显示程序的输出
- C++ 快板第一个显示程序 - 平移旋转刻度
- 如何使用 C# 在网页 asp.net 显示 C 程序错误
- 在 Java 中显示C++程序
- QtC++编译是在没有显示程序的情况下完成的
- 显示C++程序中流程完成的百分比
- 显示程序中静态C++对象的初始化顺序
- C++探查器,它显示程序特定部分的调用路径
- 控制台中没有显示程序