在c++中,如果不能被5整除,则移动到while循环
Moving to a while loop if not divisible by 5 in c++
我正在为一个类编写一个程序,其中用户输入一个数字,该数字被检查为偶数,检查它是否能被5整除,如果这两个都满足,则该数字将被while循环使用。澄清一下,如果数字是偶数且能被5整除,那么代码运行,如果它是偶数且能被2整除,那么代码运行。第一组代码需要在for循环中,第二组代码(可被2整除)需要在while循环中。
我被困在取cin并在第二组代码中使用它,第一组想先操作它。
任何帮助弄清楚这将是伟大的!
代码:#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main ()
{
system ("color F0");
int number = 0, countIf = 0,countWhile = 0, i=0;
cout << "Please enter a positive integer or zero to quit: ";
cin >> number;
cin.ignore();
if (number < 0 || number%2 != 0)
cout << "Error: The integer entered was either not positive or a zero. Try again." << endl;
//else if (number%5 || (number%2) = 0)
for (number%5; countIf < number; countIf +=5)
{
i = number / 10;
i += countIf;
cout << i << " ";
}
cin.ignore();
while (countWhile <= i && i%2)
{
int x = 0;
cout << x << " ";
x += 2;
}
system ("pause");
return 0;
}
我在看你原来的else if语句
else if (number%5 || (number%2) = 0)
你想要的是
else if (number%5 == 0 && number%2 == 0)
并在else if块中执行代码
在操作数据之前将其复制到另一个变量
cin>>a;
b=a;
并使用b作为第二组。如果一个数是偶数,它显然能被2整除。所以你不需要检查一个数是否为偶数以及它是否能被2整除
相关文章:
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- 如何从具有移动语义的类对象中生成共享指针
- 将shared_ptr移动到<StructA>shared_ptr<变体<结构A、结构 B>>
- C / C++ 移位/偏移/向左或向右移动位图?
- MSVC将仅移动结构参数解释为指针
- 自定义先决条件对移动分配运算符有效吗
- 返回值优化:显式移动还是隐式
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 可以使用移动语义更改或改进此C++代码吗?
- 使lambda不可复制/不可移动
- c++在使用指针时移动语义
- 将QGraphicsItem的移动区域限制在多边形区域内
- SendInput()鼠标移动计算
- 按值 C++ 返回时进行双倍移动
- 移动二维数组中的字符
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- 将整数移动到最接近的可被 4 整除的 C++
- 在c++中,如果不能被5整除,则移动到while循环