Ok I got it,
I've never dealt with anything over an int - for formatting printf for long, return value I needed to change for long, and well its in the code. not cleaned up to show work ... and what I used to help me figure it out.
I was fiddling with the code so much I forgot about my return value,
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include "data.h"
long int_or_ch(char *data)
{
int len = 0, i, base;
char str1[1+strlen(data)];
char *ptr, *endptr;
long val = 0;
strcpy(str1, data);
len = strlen(str1);
ptr = str1;
printf("ptr %s\n", ptr);
printf("\ngot length %dand input is %s\n", len, str1);
// if (len > 11 )
// return -2;
for ( i = 0; i < len - 1; i++)
{
printf("%c\n", ptr[i]);
// if not a digit return error code -1
if ( isdigit(ptr[i]) == 0)
return -1;
}
printf(" 2 ptr %s\n", ptr);
// base = (len > 11) ? atoi(str1) : 10;
// printf("base %d\n", base);
base = 10;
val = strtol(ptr, &endptr, base);
printf("#@# val = %li\n",val);
return val;
}
void using_strtoll(int argc, char **argv)
{
int base;
char *endptr, *str;
long val;
if (argc < 2) {
fprintf(stderr, "Usage: %s str [base]\n", argv[0]);
exit(EXIT_FAILURE);
}
printf(" argv[1] %s\n", argv[1]);
str = argv[1];
printf(" str %s \n argv[1] %s\n", str, argv[1]);
base = (argc > 2) ? atoi(argv[2]) : 10;
printf("argc %d base %d\n",argc, base);
errno = 0; /* To distinguish success/failure after call */
val = strtol(str, &endptr, base);
printf("1. strtol() returned %ld\n", val);
/* Check for various possible errors */
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
|| (errno != 0 && val == 0)) {
perror("strtol");
exit(EXIT_FAILURE);
}
if (endptr == str) {
fprintf(stderr, "No digits were found\n");
exit(EXIT_FAILURE);
}
/* If we got here, strtol() successfully parsed a number */
printf("strtol() returned %ld\n", val);
if (*endptr != '\0') { /* Not necessarily an error... */
printf("Further characters after number: %s\n", endptr);
exit(EXIT_SUCCESS);
}
}
main
Code:
#include <stdio.h>
#include <stdlib.h>
#include "data.h"
int main (int argc, char **argv)
{
if ( argc > 1)
printf("argv == %s\n",argv[1]);
using_strtoll(argc, argv);
printf("\n\n");
char *buffer = NULL;
size_t len;
int flag = 0;
long results = 0;
printf("input a number\n");
while ( flag != 1 )
{
getline(&buffer, &len, stdin);
results = int_or_ch(buffer);
if (results == -1)
printf("reenter a number\n");
else
{
// printf("you entered %d\n", results);
printf("you entered %li\n", results);
flag = 1;
}
}
free(buffer);
return 0;
}
Now clean up the code to make it look pretty. then add this function to my other ( real ) program I have
thanks whiteflags for your inspiration. !
that seems to work but because I already know I want base 10 I think I'll just assign base = 10; or plug 10 into that strtol function.
Code:
base = (len < 10) ? atoi(str1) : 10;
if len less then 10 just use atoi to get base to equal the value else assign base = 10; which would requite more if statements before the return. what am I thinking , I can work with that.