0

My code currently reads both arguments and i need to add a check for it to read 1 argument if someone put in one number such as 100 and to read the second argument if entered 100 3. right now it reads both arguements everytime and and gives an error if one argument is entered.

#include <iostream>
#include <cstring>

using namespace std;
int perfectnumber(int number)
{

   int sumofdivisor = 0;
   for (int i = 1; i < number; i++)
   {
       if (number % i == 0)  
       sumofdivisor += i;
   }

   return abs(sumofdivisor - number);
}

int main(int argc, char *argv[])
{
   int count = atoi(argv[2]); 
   int upper_limit = atoi(argv[1]);

   for (int start = 2; start <= upper_limit; start++)
    {
        int difference = perfectnumber(start);
        if (difference <= count)
        {
            cout << start << " ";
        }  
    }
    cout << endl;
}
Mat
  • 202,337
  • 40
  • 393
  • 406
B.smith
  • 9
  • 2
  • Just check argc: int count = argc > 2 ? atoi(argv[2]) : 0; // should do – Thomas G. Aug 16 '19 at 06:22
  • where exactly does that need to be added? – B.smith Aug 16 '19 at 06:27
  • @B.smith You seem to have difficulties with making use of the information provided by argc. Let me know what you want to happen when only one argument is given. What should your program do? Warn and exit? Guess at the second argument, based on a default? I could extend my answer. – Yunnosch Aug 16 '19 at 06:34

2 Answers2

2

The parameter argc is your friend, it tells you how many arguments there are.

Here is an example of how to use argc.

#include "stdio.h"
int main(int argc, char* argv[])
{

    printf("Number: %d\n", argc);
    printf("0: %s\n", argv[0]);
    if (1<argc)
    {
        printf("1: %s\n", argv[1]);
    }
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • @Scheff It is not link only, the first version was link free and good enough, but I thought OP needs some help with actually using it, because of the comment. And I teetered between linking (non-externally) and copying the same thing here again. And I do not expect the other answer (or question) to be deleted, it is too old. But OK, with your feedback I will go for copying. – Yunnosch Aug 16 '19 at 07:26
2

You can use argc to see how many arguments are provided. The first argument is the name of the executable1, so you have to compare with 3. Here is a simple example:

#include <iostream>
#include <cstdlib>


int main(int argc, char *argv[])
{
  if (argc < 3) {
    std::cerr << "Too few arguments\n";
    return EXIT_FAILURE;
  }
  std::cout << "Args: " << argv[1] << " and " << argv[2] << '\n';
}

1 This is not entirely correct. According to this reference: "argv[0] is the pointer to the initial character of a null-terminated multibyte string that represents the name used to invoke the program itself (or an empty string "" if this is not supported by the execution environment)." But as a comment points out, this is not entirely accurate either. This is a convention that implementations usually follow but are free to not to.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • 1
    _The first argument is the name of the executable_: This is actually a convention only: [exec (3)](http://man7.org/linux/man-pages/man3/exec.3.html): _The first argument, by convention, should point to the filename associated with the file being executed._ [\_exec, \_wexec Functions](https://docs.microsoft.com/en-us/cpp/c-runtime-library/exec-wexec-functions): _this parameter is `argv[0]` of the new process. Usually, this parameter is a copy of cmdname. (A different value does not produce an error.)_ However, not following that convention is probably like shooting in your own foot. ;-) – Scheff's Cat Aug 16 '19 at 06:57
  • @Scheff Thank you for the feedback! Added a link to this more complete explanation in the answer. – Aykhan Hagverdili Aug 16 '19 at 07:03