# Thread: Convert char array to int

1. ## Convert char array to int

Hey guys, I'm having a problem with converting characters from a char array to int variables. I've searched over the forums but haven't been able to find an answer. What I'm trying to do is write a program that adds two fractions, where the fractions will be entered in by the user as 1/8 or 12/10 for example.

The code below works fine for single digit fractions (eg: 1/8) but I cant get it to work for double digit ones.

Here's what I have so far:

Code:
``` printf("Enter fraction #1: ");
fgets(fractionOne, FR_MAX_INPUT+1, stdin);

/* test to makesure that we have ALL of the line */
if(fractionOne[strlen(fractionOne) - 1] != '\n') {
/* the newline must be in the buffer */
} else { /* get rid of the new line from the input */
fractionOne[strlen(fractionOne) - 1] = '\0';
}

origFracOneNumerator = atoi (fractionOne);
origFracOneDenominator = atoi (fractionOne+2);```
Now I thought it would be simply a matter of changing the line
Code:
`origFracOneDenominator = atoi (fractionOne+2);`
to
Code:
`origFracOneDenominator = atoi (fractionOne+3);`
But when I do this it tells me the value of origFracOneDenominator is 0 and not the second half of the fraction.

Any ideas?

2. Your job would be a lot easier if you used sscanf() or even strtol() instead of atoi(). And I'd recommend switching functions anyway, because atoi() doesn't do any error checking.

strtol() can set a pointer to the character beyond the number it processed, which you can then increment until you pass a '/', then you can call strtol() again. Something like
Code:
```char f[] = "12/34";
char *p;
long x, y;
x = strtol(f, &p, 0);
while(isspace(*p || *p == '/')) p ++;
y = strtol(p, NULL, 0);```
With sscanf(), you can use
Code:
```char f[] = "12/34";
int x, y;
if(sscanf(f, "&#37;d/%d", &x, &y) != 2) { /* error */ }```
http://www.cplusplus.com/reference/c...ib/strtol.html
http://www.cplusplus.com/reference/c...io/sscanf.html

3. Thanks for that. I've only been programming C for about 8 weeks so I'm still a little unsure on what functions are best to use and when.

Will give strtol a try and let u know how it goes.

Thanks again.

4. Hmm, that seems to be doing the same thing. When I type in 12/13 it tells me x is 12 and y is 0.

Am I missing something here?

6. Code:
```char fractionOne[FR_MAX_INPUT+1];
char fractionTwo[FR_MAX_INPUT+1];
char *p;
long x, y;

printf("Fractions\n");
printf("------------------\n");

printf("Enter fraction #1: ");
fgets(fractionOne, FR_MAX_INPUT+1, stdin);

/* test to makesure that we have ALL of the line */
if(fractionOne[strlen(fractionOne) - 1] != '\n') {
/* the newline must be in the buffer */
} else { /* get rid of the new line from the input */
fractionOne[strlen(fractionOne) - 1] = '\0';
}

x = strtol(fractionOne, &p, 0);
while(isspace(*p || *p == '/')) p ++;
y = strtol(p, NULL, 0);

printf("x is %d\n: ", x);
printf("y is %d\n: ", y);```

7. isspace(*p || *p == '/')
shouldn't it be
Code:
`isspace(*p) || *p == '/'`

8. Nah, I thought that at first but it still produces the same output.

9. There won't necessarily be a newline on the end of text entered into stdin, the FAQ demonstrates a good method on determining if there is - and to remove it if it's there.

10. Works for me - note the change to the printf's at the end.
Code:
```#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define FR_MAX_INPUT 100

int main ( void ) {
char fractionOne[FR_MAX_INPUT+1];
char *p;
long x, y;

printf("Fractions\n");
printf("------------------\n");

printf("Enter fraction #1: ");
fgets(fractionOne, FR_MAX_INPUT+1, stdin);

/* test to makesure that we have ALL of the line */
if(fractionOne[strlen(fractionOne) - 1] != '\n') {
/* the newline must be in the buffer */
} else { /* get rid of the new line from the input */
fractionOne[strlen(fractionOne) - 1] = '\0';
}

x = strtol(fractionOne, &p, 0);
while( isspace(*p) || *p == '/' ) p ++;
y = strtol(p, NULL, 0);

printf("x is %ld\n", x);
printf("y is %ld\n", y);
return 0;
}

\$ ./a.exe
Fractions
------------------
Enter fraction #1: 12/34
x is 12
y is 34```