告诉计算机在打印答案后清除它
Tell computer to clear the answer after it is being printed
程序旨在将从1到我输入的数字的所有数字平方并相加。如果我只输入一个数字,答案将是正确的,但如果我不停止它并重新打开我的程序,我输入的第二个数字的答案也将添加第一个数字的结果。我错过了什么?请帮忙。
#include<iostream>
using namespace std;
int main()
{
int inNum;
int loopCount = 1;
int count;
int sum = 0;
cout << "Enter a number greater than 0 (less than 1 to quit): " << endl;
cin >> inNum;
while ( inNum >= loopCount) {
for (count=0 ; count <= inNum ; count++ ){
sum = sum + count * count;
}
cout << "The sum of the squares from 1 to " << inNum << " is " << sum << endl;
cout << "Enter a number greater than 0 (less than 1 to quit): " << endl;
cin >> inNum;
}
}
一个解决方案是将sum
的声明移动到while块中
// int sum = 0;
while ( inNum >= loopCount) {
int sum = 0; // <-- or just sum = 0; and keep the declaration outside the loop
另一个(如注释中所述)是在循环体的末尾重置sum
。
相关文章:
- 欧拉项目#8答案是大以获得有效答案
- 首要问题的答案让值班员搞错了
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 清除前检查矢量
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- ifstream文件在从行中读取时被清除
- 使用 bfs 解决连接组件问题时得到错误的答案
- WINAPI 注册应用程序重新启动时不清除打开的套接字
- 同一对象的"sizeof"的不同答案
- 递归求和任务的错误答案
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 为什么C++程序在太大时返回代码而不是答案?
- 如何清除/清空已打开的文件C++
- 不理解为什么代码没有产生所需的答案
- 为什么我在代码厨师的 CMPRSS 问题中得到 WA(错误答案)?
- 如何在 c++ 中清除矢量容量?
- 可以清除递归函数中的变量吗?
- 在清除 istream 之前,我不应该需要取消获取它吗?
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 告诉计算机在打印答案后清除它