Hey, I am trying to read in input from the user such as a character that is followed by an integer, and depending on what that character is , perform different tasks.
I have come up with the following code which works fine for one char - one int:
Assume that the string that is passed to this function is: "a 53"
Code:
void tokenizeAstring(char string[])
{
char *string1;
int value1;
string1 = (char *)strtok(string," ", "\n", "\t"); /*break up the string for the first time, now only number values will remain*/
while( string1 != NULL) { /*while string is not empty*/
value1 = atoi(string1); /*using the function atoi to convert a string into an integer*/
string1 = (char *)strtok(NULL," ", "\n", "\t"); /*assigns another piece to string, if this function is called propertly,*/
/*it will be NULL and end the loop*/
}
printf("The value of val is: %d\n", value1); /*debugging check. If input was "a int", then value1 equals that int*/
}
The integer value1 holds the value 53 from the above sample input, and so the printf statement prints "The value of val is: 53".
My question is, how do i go about receiving two integers from the user (for example input would be "a 53 19") and store them in two different variables, the value1 that already exists and say to value2 that I would declare etc. When I try it on a modified version of the above example I get fragmantation errors, or the values equal zero. I have no idea what to do.