对于循环无法正常工作(c ++)
For Loop not working properly (c++)
我一直在努力让这个程序正常工作。它确实可以编译,但是它不是提示用户输入,而是声明不正确的内容。
提示:
要求用户输入 10 个课程分数(从 0 到 100),然后(在标签中)说明有多少是通过分数。
我的代码:
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int mark;
int passinggrades = 0;
for(int i = 0; i > 10; i++)
{
cout << "Enter Mark:";
cin >> mark;
while(mark >= 50)
{
passinggrades++;
}
}
cout << j << " of your marks were passing grades.";
return 0;
}
循环条件错误:
for (int i=0; i>10;i++)
反转比较符号
for (int i=0; i<10;i++)
另外,您可能希望if
此行:
while(mark >= 50)
更改为
if(mark >= 50)
甚至输出线也是错误的。代码中没有j
。这样做:
cout << passinggrades << " of your marks were passing grades.";
for (int i=0; i>10;i++)
您从 i = 0
开始,并在i > 10
时执行循环(从不,因为它为 0)。
我想你想要
if(mark >= 50)
而不是无限循环
while(mark >= 50)
另外,i<10
就像其他人说的那样。
三个问题:一:当你说:
while(mark >= 50)
{
passinggrades++;
}
50+ 的输入会让你的程序永远停留在外观上。而是使用:
if(mark >= 50)
{
passinggrades++;
}
二:
不要在 for 循环中i>10
,而是使用 i<10
三:(感谢ctor的评论)
j
未定义,应替换为 passinggrades
。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- C++程序已停止工作-求解常微分方程