当我在终端上运行它时工作正常,但循环。 for循环根本不做任何事情。我正在学习C ++,所以我不太了解。
#include <iostream>
#include <cstring>
using namespace std;
int main( int argc, char *argv[] ) {
if (argc == 2) {
cout << "The first argument is " << argv[0] << endl;
cout << "The second argument is " << argv[1] << endl;
} else if (argc > 2) {
cout << "Too many arguments" << endl;
exit(0);
} else {
cout << "Only one argument" << endl;
cout << "The argument is " << argv[0] << endl;
exit(0);
}
if (atoi(argv[1]) < 0) {
cout << "Error negative number" << endl;
exit(0);
}
// this loop does not work, everything else does.
for (int i = 1; i >= atoi(argv[1]); i++){
int count = atoi(argv[1]--);
cout << count << endl;
int sum = sum + i;
}
cout << "The sum is: " << endl;
return(0);}
我认为这可能是if语句正在搞乱循环。
答案 0 :(得分:1)
我认为你在for循环中犯了错误。 你显示使用&#34;&lt; =&#34;而不是&#34;&gt; =&#34;在for循环中。
希望这可能会对你有所帮助。
答案 1 :(得分:0)
我猜您的代码未达到for loop
,因为您exit()
的每个条件都有if
条件。如果在运行代码时在终端中传递2个参数,则代码仅到达循环