tags:

views:

192

answers:

3

Right now i am doing an assignment but find it very hard to parse the user input in C. Here is kind of input user will input.

INSERT Alice, 25 Norway Drive, Fitzerald, GA, 40204, 6000.60

Here INSERT is the command (to enter in link list)
Alice is a name
25 Norway Drive is an address
Fitzerald is a city
GA is a state
40204 is a zip code
6000.60 is a balance

How can I use scanf or any other method in C to properly take this as input? The biggest problem in front of me is how to ignore these "," and store these values in separate variables of appropriate data types.

Thanks everyone, i have solve the issue and here is the solution:

   pch = strtok(NULL, ","); pch =
        substr(pch, 2, strlen(pch));  //substr is my custom funcition and i believe you can tell by its name what it is doing.
        strcpy(customer->streetAddress, pch);
A: 

scanf() may be a bit tricky in this situation, assuming that different commands with different parameters can be used. I would probably use fgets() to read in the string first, followed by the use of strtok() to read the first token (the command). At that point you can either continue to use strtok() with "," as the delimiter to read the rest of the tokens in the string, or you could use a sscanf() on the rest of the string (now that you know the format that the rest of the input will be in). sscanf() is still going to be a pain due to the fact that it appears that an unspecified number of spaces would be allowed in the address and possibly town fields.

Graphics Noob
@G. Noob: if you really mean a strtok(3) delimiter of ", " (comma followed by space), that tokenizes too aggressively, as the OP already discovered and commented on.
pilcrow
+4  A: 

Fast easy method:

Use fgets() to get the string from the user;
and strtok() to tokenize it.

Edit
After reading your comment:

Use strtok() with only the comma, and then remove trailing and leading spaces from the result.

Edit2
After a test run, I noticed you will get "INSERT Alice" as the first token. So, after all tokens have been extracted, run strtok() again, this time with a space, on the first token extracted. Or, find the space and somehow identify the command and the name from there.

pmg
Since you need to make a strtok() call for each token, just call it with space only for the first call, then switch to the comma for the rest.
Graphics Noob
Very good point, Graphics Noob
pmg
+1  A: 

If your input data format is fixed you can use something quick and dirty using [s]scanf().

With input of:

INSERT Alice, 25 Norway Drive, Fitzerald, GA, 40204, 6000.60

You might try, if reading from stdin:

char name[80], addr[80], city[80], state[80];
int zip;
double amt;

int res = scanf("INSERT %[^,], %[^,], %[^,], %[^,], %d, %f\n",
    &name, &addr, &city, &state, &zip, &amt);

Should return the number of items matched (i.e. 6).

Lee-Man
Is the %[^,] format standard? I don't think I've seen that before
Martin Beckett
The man page on my linux box says C89, C99, and POSIX.1-2001. I found it in the C89 Standard just now, so I assume it's been around a while. I did say it was quick and dirty ... worthy of a prototype, but ripe with possible problems, like the format of the input changing.
Lee-Man