[英]Possible operations error, random single digit output
I have written this program for a class that asks for a positive number and based on the number, calculations are performed. 我为要求正数的课程编写了该程序,并根据该数执行了计算。 I had some great help from stack folks last week but was asked by my professor to re-write it and simplify my code.
上周,我从堆栈专家那里得到了很大的帮助,但是我的教授要求我重写它并简化我的代码。 I have done that and now the math is not coming out right.
我已经做到了,现在数学不是正确的。 I have run the debugger but I dont see where the values being passed are incorrect.
我已经运行了调试器,但是看不到传递的值不正确的地方。 Also, all numbers are failing the "if (number > 0)" test.
此外,所有数字均未通过“ if(number> 0)”测试。
Could it be a compile error even though I get a successful build message? 即使收到成功的构建消息也可能是编译错误吗?
Thanks in advance! 提前致谢!
Here is the code. 这是代码。
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdexcept>
#include <cstdlib>
using namespace std;
int main ()
{
system ("color F0");
int number, countIf = 0, countWhile = 0, countDo = -1, h = 0, i = 0, x = 0, y = 0;
char repeat = 'y';
do {
cout << "Please enter a positive integer or zero to quit: ";
cin >> number;
x = number, y = number;
cin.ignore();
if (number < 0)
cout << "Error: The integer entered was either not positive or a zero.\n" << endl;
else if (number > 0)
{
if (number%2 == 0 && number%5 == 0)
for (y%5; countIf <= y; countIf +=5)
{
i = y / 10;
i += countIf;
cout << countIf << " ";
}
else if
(countWhile < x && number%2 == 0)
{
countWhile += 2;
cout << countWhile << " ";
}
else
{
countDo +=2;
cout << countDo << " ";
}
}
cout << "\n\nDo you wish to continue? (Y or N): ";
cin >> repeat;
}
while (number != 0 && repeat == 'y' || repeat == 'Y');
//cout << "\nThanks for playing!" << endl;
system ("pause");
return 0;
}
Output should be similar to: 输出应类似于:
What I am getting is: 我得到的是:
I modified your code like this: 我这样修改了您的代码:
else if
(countWhile < x && number%2 == 0)
{
for(countWhile+=2; countWhile<x; countWhile+=2)
cout << countWhile << " ";
}
else
{
for(countDo+=2; countDo<x; countDo+=2)
cout << countDo << " ";
}
And I get output like: 我得到像这样的输出:
Please enter a positive integer or zero to quit: 82
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80
Do you wish to continue? (Y or N): y
Please enter a positive integer or zero to quit: 75
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.