Is it possible to scan strings with spaces, and then other unlike int variables. For instance, if I wanted to input the name of a tool, then it's price, how would I differentiate between the two in the scan line?

My problem is that I can't get scanf to differentiate beween the variables for 'toolname' and 'quantity'.

07-11-2009

BEN10

Quote:

Originally Posted by ginom71

Is it possible to scan strings with spaces, and then other unlike int variables. For instance, if I wanted to input the name of a tool, then it's price, how would I differentiate between the two in the scan line?

If I got your question right, yes it is possible to scan strings with spaces, using fgets. For differentiating the name will be string and price would be int or float. In that case using a structure is advisable.

07-11-2009

Sebastiani

>> My problem is that I can't get scanf to differentiate beween the variables for 'toolname' and 'quantity'.

Why? Aren't you entering the values separated by spaces? It should work fine. Oh and one more thing, you should be checking return values (scanf, fseek, fread, etc).

07-11-2009

ginom71

I am, but the problem is with input like "Sledge Hammer" and then 7.50 for cost. I need to figure out how to enter strings that have spaces, and then input value for other variables.

07-11-2009

Sebastiani

>> the problem is with input like "Sledge Hammer" and then 7.50 for cost.

I see. In that case, you'll either need to input them separately (eg: one variable per line) or else use fgets and split the input "manually".

07-11-2009

quzah

1 - Read the FAQ.
2 - Read a line from the file using fgets.
3 - Split it using sscanf.

Actually... it looks like you're reading records one whole structure at a time. In which case, you shouldn't need to do any of this. Just fwrite the whole structure, and then later fread it back in.

Enter your choice
1 - store a formatted text file of the inventory called
"storeinv.txt" for printing
2 - update a record
3 - add a new record
4 - delete a record
5 - end program
? 3
Enter new record (1-100): 3
Enter name, quantity, and cost: Name:

Quantity: 0
Cost: 0.000000

Enter your choice
1 - store a formatted text file of the inventory called
"storeinv.txt" for printing
2 - update a record
3 - add a new record
4 - delete a record
5 - end program
?

So what happens between the time I enter a new record number and the time I'm supposed to enter data for the record?

Enter your choice
1 - store a formatted text file of the inventory called
"storeinv.txt" for printing
2 - update a record
3 - add a new record
4 - delete a record
5 - end program
? 2
Enter record to update(1-100): 3
Record has no information.

Enter your choice
1 - store a formatted text file of the inventory called
"storeinv.txt" for printing
2 - update a record
3 - add a new record
4 - delete a record
5 - end program
?

07-12-2009

ginom71

Sorry, I forgot the code to write the new data into the file. It works now.