Extremely confused with basic programming.

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
27
28
29
30
#include <iostream>
#include <cstdlib>
#include <errno.h>

using namespace std;

int main(int argc, char *argv[])
{
if (argc==3)
{
     if(isalpha(*argv[1]) && isalpha(*argv[2]))
     {
         cout<<"INVALID INPUT"<<endl;
     }

     else
     {

          double x =atof(argv[1]);
          double y =atof(argv[2]);
          double answer = x+y;
           cout<<answer;
         
        }


          return 0;

    }


So I'm trying to write a basic addition program that adds using parameters RATHER than asking for user input,
what the code does so far,
1.checks if the entered values are numbers and if true, continues with addition.
2. if not returns with invalid input.

What I want it to further do.
1. Check if the parameters used is within the values of 10000 and -10000
2. If not, return wth the message "OUT OF RANGE, PLEASE USE VALUES BETWEEN 10000 and -10000".

due to my limited knowledge of C++ im struggling to do such things, Help would BE HIGHLY APPRECIATED.
Last edited on
You'll want to turn the arguments passed to it into integers. There is a function made available in the cstdlib header that does just that: http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

Read up on that and try it out on your own, feel free to comment back here if you have any issues O:
1
2
3
4
5
if (argc) == 3
{
   int a = atoi(argv[1]), b = atoi(argv[2]);
   //Now do whatever you want to.
}
Topic archived. No new replies allowed.