command line parameter

Hey guys, I'm working on a program on ubuntu that will count the number of 1s in the binary representation of a number. The program has one command line parameter, which is the base 10 number to be processed. I was wondering whether someone could tell me whether or not this code will accept from a command line parameter, as well as explain to me how input into the program using a command line parameter.
Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <stdlib.h>
#include <assert.h>
using namespace std;

int numOfOnes(int num){
  int res = 1;
  if(num == 1){
    return res;
  }
  else if (num%2==1){
    res = numOfOnes(num/2)+1;
  }
  else{
    res = numOfOnes(num/2);
  }
}

int main(int argc, char**argv){
  int input;
  char ic;
 assert (argc==2);
 input=atoi(argv[1]);
  cout<< numOfOnes(input)<<endl;
  return 0;
}
Last edited on
It will work only if you will return anything in you numOfOnes function
Topic archived. No new replies allowed.