Home > Enterprise >  C int main(int argc, char* argv[])
C int main(int argc, char* argv[])

Time:01-10

how do i save int argc, char* argv in to int someting.

i am trying to get the arguments from a test program and save it into int ****;

#include <iostream> 
#include <cstdlib>
using namespace std;

int main(int argc, char* argv[]) {
 int limit = argc;
 cout<< limit <<endl;
 for (int candidate = 2; candidate < limit; candidate  ) {
    int total = 1;
    for (int factor = 2; factor * factor < candidate; factor  ) {
        if (candidate % factor == 0)
            total  = factor   candidate / factor;

    }
    if (total == candidate) {
        cout << candidate << ' ';
    }
}
return 0;
} 

and the program is pre-set the arguments is 100, and it just can't save in to int limit

CodePudding user response:

Something like this

int main(int argc, char* argv[]) {
    if (argc < 2) {
        cerr << "Not enough arguments\n";
        return -1;
    }
    int limit = atoi(argv[1]); // convert first argument to integer
    ...
  • Related