C - read string with spaces [closed]
I'm a student and I have to create a program that will be something as automatic cash register, like in shops. And the price that 'customer' have to pay will be randomly generated and also the 'money' that he pays with. So for example customer has to pay '25.48' (this will be randomly generated). And money for example ' 20.00 5.00 0.20 0.20 0.05 0.02 0.01 '. And the only problem is, that I don't know how to read this 'string' (money). I know how to read that total price, bcause it's without spaces and I basically know lenght of that input. But I really need help with that string with spaces. I tried to search the internet but I wasn't succesfull.
Thanks for any help.
c string
closed as off-topic by OldProgrammer, Mathieu Bunel, chux, qrdl, kfx Nov 12 at 16:23
This question appears to be off-topic. The users who voted to close gave these specific reasons:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – qrdl, kfx
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – OldProgrammer, Mathieu Bunel
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 1 more comment
I'm a student and I have to create a program that will be something as automatic cash register, like in shops. And the price that 'customer' have to pay will be randomly generated and also the 'money' that he pays with. So for example customer has to pay '25.48' (this will be randomly generated). And money for example ' 20.00 5.00 0.20 0.20 0.05 0.02 0.01 '. And the only problem is, that I don't know how to read this 'string' (money). I know how to read that total price, bcause it's without spaces and I basically know lenght of that input. But I really need help with that string with spaces. I tried to search the internet but I wasn't succesfull.
Thanks for any help.
c string
closed as off-topic by OldProgrammer, Mathieu Bunel, chux, qrdl, kfx Nov 12 at 16:23
This question appears to be off-topic. The users who voted to close gave these specific reasons:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – qrdl, kfx
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – OldProgrammer, Mathieu Bunel
If this question can be reworded to fit the rules in the help center, please edit the question.
tutorialspoint.com/c_standard_library/c_function_strtok.htm
– OldProgrammer
Nov 12 at 15:04
3
" I tried to search the internet but I wasn't succesfull" :: All I can say is search properly because their is no way you can't find this thing!!
– Rahul Agarwal
Nov 12 at 15:05
To read data, usefread. Are you saying that you don't know how to parse the string? Check the documentation forstrtolandstrtod(eg,man strtod)
– William Pursell
Nov 12 at 15:07
1
I'm a student..more or less everyone is.
– Sourav Ghosh
Nov 12 at 15:10
If the "money" is just input (whether file orstdin), just simplywhile (scanf ("%lf", &amount) == 1)) { /* do something with amount */ }It all depends on what else you are reading and how you are getting the input. (of coursefscanffor a file). Ordinarily, you want to read a line at a time and then parse (e.g.fgetsthensscanf), but if it is just a line with an unknown number of numbers, this is one circumstance you justscanfuntil you run out of numbers and do something with the number in between.
– David C. Rankin
Nov 12 at 15:16
|
show 1 more comment
I'm a student and I have to create a program that will be something as automatic cash register, like in shops. And the price that 'customer' have to pay will be randomly generated and also the 'money' that he pays with. So for example customer has to pay '25.48' (this will be randomly generated). And money for example ' 20.00 5.00 0.20 0.20 0.05 0.02 0.01 '. And the only problem is, that I don't know how to read this 'string' (money). I know how to read that total price, bcause it's without spaces and I basically know lenght of that input. But I really need help with that string with spaces. I tried to search the internet but I wasn't succesfull.
Thanks for any help.
c string
I'm a student and I have to create a program that will be something as automatic cash register, like in shops. And the price that 'customer' have to pay will be randomly generated and also the 'money' that he pays with. So for example customer has to pay '25.48' (this will be randomly generated). And money for example ' 20.00 5.00 0.20 0.20 0.05 0.02 0.01 '. And the only problem is, that I don't know how to read this 'string' (money). I know how to read that total price, bcause it's without spaces and I basically know lenght of that input. But I really need help with that string with spaces. I tried to search the internet but I wasn't succesfull.
Thanks for any help.
c string
c string
asked Nov 12 at 15:03
BlackWolf
166
166
closed as off-topic by OldProgrammer, Mathieu Bunel, chux, qrdl, kfx Nov 12 at 16:23
This question appears to be off-topic. The users who voted to close gave these specific reasons:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – qrdl, kfx
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – OldProgrammer, Mathieu Bunel
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by OldProgrammer, Mathieu Bunel, chux, qrdl, kfx Nov 12 at 16:23
This question appears to be off-topic. The users who voted to close gave these specific reasons:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – qrdl, kfx
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – OldProgrammer, Mathieu Bunel
If this question can be reworded to fit the rules in the help center, please edit the question.
tutorialspoint.com/c_standard_library/c_function_strtok.htm
– OldProgrammer
Nov 12 at 15:04
3
" I tried to search the internet but I wasn't succesfull" :: All I can say is search properly because their is no way you can't find this thing!!
– Rahul Agarwal
Nov 12 at 15:05
To read data, usefread. Are you saying that you don't know how to parse the string? Check the documentation forstrtolandstrtod(eg,man strtod)
– William Pursell
Nov 12 at 15:07
1
I'm a student..more or less everyone is.
– Sourav Ghosh
Nov 12 at 15:10
If the "money" is just input (whether file orstdin), just simplywhile (scanf ("%lf", &amount) == 1)) { /* do something with amount */ }It all depends on what else you are reading and how you are getting the input. (of coursefscanffor a file). Ordinarily, you want to read a line at a time and then parse (e.g.fgetsthensscanf), but if it is just a line with an unknown number of numbers, this is one circumstance you justscanfuntil you run out of numbers and do something with the number in between.
– David C. Rankin
Nov 12 at 15:16
|
show 1 more comment
tutorialspoint.com/c_standard_library/c_function_strtok.htm
– OldProgrammer
Nov 12 at 15:04
3
" I tried to search the internet but I wasn't succesfull" :: All I can say is search properly because their is no way you can't find this thing!!
– Rahul Agarwal
Nov 12 at 15:05
To read data, usefread. Are you saying that you don't know how to parse the string? Check the documentation forstrtolandstrtod(eg,man strtod)
– William Pursell
Nov 12 at 15:07
1
I'm a student..more or less everyone is.
– Sourav Ghosh
Nov 12 at 15:10
If the "money" is just input (whether file orstdin), just simplywhile (scanf ("%lf", &amount) == 1)) { /* do something with amount */ }It all depends on what else you are reading and how you are getting the input. (of coursefscanffor a file). Ordinarily, you want to read a line at a time and then parse (e.g.fgetsthensscanf), but if it is just a line with an unknown number of numbers, this is one circumstance you justscanfuntil you run out of numbers and do something with the number in between.
– David C. Rankin
Nov 12 at 15:16
tutorialspoint.com/c_standard_library/c_function_strtok.htm
– OldProgrammer
Nov 12 at 15:04
tutorialspoint.com/c_standard_library/c_function_strtok.htm
– OldProgrammer
Nov 12 at 15:04
3
3
" I tried to search the internet but I wasn't succesfull" :: All I can say is search properly because their is no way you can't find this thing!!
– Rahul Agarwal
Nov 12 at 15:05
" I tried to search the internet but I wasn't succesfull" :: All I can say is search properly because their is no way you can't find this thing!!
– Rahul Agarwal
Nov 12 at 15:05
To read data, use
fread. Are you saying that you don't know how to parse the string? Check the documentation for strtol and strtod (eg, man strtod)– William Pursell
Nov 12 at 15:07
To read data, use
fread. Are you saying that you don't know how to parse the string? Check the documentation for strtol and strtod (eg, man strtod)– William Pursell
Nov 12 at 15:07
1
1
I'm a student ..more or less everyone is.– Sourav Ghosh
Nov 12 at 15:10
I'm a student ..more or less everyone is.– Sourav Ghosh
Nov 12 at 15:10
If the "money" is just input (whether file or
stdin), just simply while (scanf ("%lf", &amount) == 1)) { /* do something with amount */ } It all depends on what else you are reading and how you are getting the input. (of course fscanf for a file). Ordinarily, you want to read a line at a time and then parse (e.g. fgets then sscanf), but if it is just a line with an unknown number of numbers, this is one circumstance you just scanf until you run out of numbers and do something with the number in between.– David C. Rankin
Nov 12 at 15:16
If the "money" is just input (whether file or
stdin), just simply while (scanf ("%lf", &amount) == 1)) { /* do something with amount */ } It all depends on what else you are reading and how you are getting the input. (of course fscanf for a file). Ordinarily, you want to read a line at a time and then parse (e.g. fgets then sscanf), but if it is just a line with an unknown number of numbers, this is one circumstance you just scanf until you run out of numbers and do something with the number in between.– David C. Rankin
Nov 12 at 15:16
|
show 1 more comment
1 Answer
1
active
oldest
votes
Take a look at the strtok and strtof functions. You can use strtok to parse the string in a loop using space as a delimiter and then strtof to convert each value string to floating point.
(hopefullystrtodat minimum if working with money:)There is no need to callstrtokto tokenize numbers (that's what theendptrparameter tostrtodis for)
– David C. Rankin
Nov 12 at 15:22
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Take a look at the strtok and strtof functions. You can use strtok to parse the string in a loop using space as a delimiter and then strtof to convert each value string to floating point.
(hopefullystrtodat minimum if working with money:)There is no need to callstrtokto tokenize numbers (that's what theendptrparameter tostrtodis for)
– David C. Rankin
Nov 12 at 15:22
add a comment |
Take a look at the strtok and strtof functions. You can use strtok to parse the string in a loop using space as a delimiter and then strtof to convert each value string to floating point.
(hopefullystrtodat minimum if working with money:)There is no need to callstrtokto tokenize numbers (that's what theendptrparameter tostrtodis for)
– David C. Rankin
Nov 12 at 15:22
add a comment |
Take a look at the strtok and strtof functions. You can use strtok to parse the string in a loop using space as a delimiter and then strtof to convert each value string to floating point.
Take a look at the strtok and strtof functions. You can use strtok to parse the string in a loop using space as a delimiter and then strtof to convert each value string to floating point.
answered Nov 12 at 15:11
John BC
111
111
(hopefullystrtodat minimum if working with money:)There is no need to callstrtokto tokenize numbers (that's what theendptrparameter tostrtodis for)
– David C. Rankin
Nov 12 at 15:22
add a comment |
(hopefullystrtodat minimum if working with money:)There is no need to callstrtokto tokenize numbers (that's what theendptrparameter tostrtodis for)
– David C. Rankin
Nov 12 at 15:22
(hopefully
strtod at minimum if working with money :) There is no need to call strtok to tokenize numbers (that's what the endptr parameter to strtod is for)– David C. Rankin
Nov 12 at 15:22
(hopefully
strtod at minimum if working with money :) There is no need to call strtok to tokenize numbers (that's what the endptr parameter to strtod is for)– David C. Rankin
Nov 12 at 15:22
add a comment |
tutorialspoint.com/c_standard_library/c_function_strtok.htm
– OldProgrammer
Nov 12 at 15:04
3
" I tried to search the internet but I wasn't succesfull" :: All I can say is search properly because their is no way you can't find this thing!!
– Rahul Agarwal
Nov 12 at 15:05
To read data, use
fread. Are you saying that you don't know how to parse the string? Check the documentation forstrtolandstrtod(eg,man strtod)– William Pursell
Nov 12 at 15:07
1
I'm a student..more or less everyone is.– Sourav Ghosh
Nov 12 at 15:10
If the "money" is just input (whether file or
stdin), just simplywhile (scanf ("%lf", &amount) == 1)) { /* do something with amount */ }It all depends on what else you are reading and how you are getting the input. (of coursefscanffor a file). Ordinarily, you want to read a line at a time and then parse (e.g.fgetsthensscanf), but if it is just a line with an unknown number of numbers, this is one circumstance you justscanfuntil you run out of numbers and do something with the number in between.– David C. Rankin
Nov 12 at 15:16