我为一个要求正数的类编写了这个程序,并根据该数字执行计算。上周我从堆栈人员那里得到了一些很大的帮助,但我的教授要求我重写它并简化我的代码。我已经这样做了,现在数学计算不正确。我已经运行了调试器,但我看不到传递的值在哪里不正确。此外,所有数字都未通过“if (number > 0)”测试。
即使我收到成功的构建消息,这可能是编译错误吗?
提前致谢!
这是代码。
#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;
}
输出应类似于:
我得到的是: