1
我已经为要求正数的类编写了该程序,并根据数字进行了计算。上周我从堆栈人员那里得到了很多帮助,但是我的教授请求重写它并简化我的代码。我已经这样做了,现在数学并没有出现。我已经运行调试器,但我没有看到值传递的位置不正确。此外,所有数字都无法通过“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;
}
输出应该类似于:
我所得到的是:
当您简化代码时,您应该在每一步都进行测试。这样,如果它停止工作,你会确切地知道麻烦在哪里。 – Beta 2013-04-09 01:49:38
这不是编译错误,它是一个逻辑错误。尝试用铅笔和纸做一个例子。顺便说一句,'&&'比'||'具有更高的优先级,所以'A && B || C'表示(A && B)|| C'。 – MRAB 2013-04-09 02:04:35
你的代码很长。制作一份副本,并开始删除部件,直到不再能够重现问题,或者直到获得仍然存在问题的“最简单”代码。在后一种情况下,您可以回来并提供该代码片段,并且我保证您会更容易回答。 – nneonneo 2013-04-09 02:54:22