0

So I compile the program and run it like ./beetle int int, basically the executable and 2 ints follow, I want to make a test for if theres more than 2 arguments after the executable, if there is an argument that is not an int (like 300b) and if there is an argument that is a float, not an int and if the argument overflows

Im stuck on how to make the test for the non ints and the overflow, can someone point me in the right direction?

   #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265
void beetleSimulation(int, int);


int main ( int argc, char *argv[] )
{
    char * pEnd;
    if ( argc != 3 ) // argc should be 2 for correct execution 
    {
        // If the number of arguments is not 2
        fprintf(stderr, "Error: Program has %d arguments ", argc);
        return 0;
    }
    else 
    {
       //run the beetle simulation
        beetleSimulation(strtol(argv[1], &pEnd, 10), strtol(argv[2], &pEnd, 10 ));
        if (!*pEnd)
            printf("Success!");
        else
            printf("Failed!");
        return 0;
        }
    }


void beetleSimulation(int size, int iterations){
    int i;
    double xCount;
    double yCount;
    int timeCount;
    int overallCount = 0;
    int degree;
    double radian;
    int border;
    for(i=0; i < iterations; i++){
        xCount = 0;
        yCount = 0;
        timeCount = 0;
        border -= size;
        while(xCount < size && xCount > border  && yCount <size && yCount >border ){
            timeCount += 1;
            degree = rand() % 360;
            radian = degree / (180 * PI);

            xCount += sin(radian);
            yCount += cos(radian);
            printf("Radian is %f\n", radian);

            printf("X and Y are %f and %f\n", xCount, yCount);
        }

        //when beetle has died, add time it took to overall count, then go through for loop again
        overallCount += timeCount;
        printf("Time for this run is %d, overall its %d\n",timeCount, overallCount);
    }
    //calculate average time
    double averageTime = overallCount/iterations;
    printf("Average Time is %f",averageTime);
}
FullCombatBeard
  • 323
  • 1
  • 11

2 Answers2

0

String to int conversion function.

bool str2int(const char *s, int *dest) {
    char *endptr;
    errno = 0;
    long lnum = strtol(s, &endptr, 10);
    if (s == endptr) return 1;                               // fail - no conversion
    if (*endptr) return 1;                                   // fail - extra garbage
    if (errno || lnum < INT_MIN || lnum > INT_MAX) return 1; // fail - overflow
    *dest = (int) lnum;
    return 0; // success
  }
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • I dont see how this will account for if the argument is not an int, such as 40b or 40.0 – FullCombatBeard Feb 03 '15 at 03:21
  • @FullCombatBeard `if (*endptr) return 1;` `endptr` points to the `char` that stopped the `strtol()` conversion. With "40b", `*endptr` points to `'b'`. So unless `*endptr` points to `'\0'`, there was something after the converted `long`. – chux - Reinstate Monica Feb 03 '15 at 13:42
0

suggest getting the strlen() value of an argv[x]

Then performing the strtol() call

then comparing the argv[x]+strlen() values to the value set in the second parameter of the strtol()

if they match, then the argument was a good int,

other wise the argument was (probably) not a good int.

Suggest doing all this comparing before the call to beetleSimulation()

as both calls to strtol() are using the same variable for the end address, so the second call to strtol() would mask the value set in the first call.

user3629249
  • 16,402
  • 1
  • 16
  • 17