I am having trouble getting multiple number vars from a read in line. For a single value I can do strtol(), but how can I get the float and long values of a sentence that is similar to as follows.
Please aim 3.567 degrees at a height of 5 meters.
I tried doing two different calls to my buffer sentence, however it got neither of my values. I have no issues with get single values, but with to, I get 0.000 from my strtof call and 0 from mmy strtol call.
CodePudding user response:
ASSUMING YOU KNOW IN WHICH ORDER THE VALUES WILL BE IN
Here is an example of how you can get multiple numeric values from a string:
#include <stdio.h>
#include <stdlib.h>
int main() {
// The string to parse
char *str = "Please aim 3.567 degrees at a height of 5 meters.";
// Variables to store the values
float degrees;
int height;
// Parse the string and store the values in the variables
// you can use sscanf, fscanf, scanf
sscanf(str, "Please aim %f degrees at a height of %d meters.", °rees, &height);
// Print the values
printf("degrees: %f\n", degrees);
printf("height: %d\n", height);
return 0;
}
In this code, the sscanf() function is used to parse the string and extract the values of the degrees and height variables. The sscanf() function takes the string to parse as the first argument, and a format string containing the conversion specifiers for the values to extract as the second argument. The & operator is used to pass the addresses of the degrees and height variables, so that the sscanf() function can store the extracted values in those variables.
After calling the sscanf() function, the degrees and height variables are printed to the console to verify that the values were extracted correctly.
-chatgpt
CodePudding user response:
Assuming that the input always starts with the string "Please aim "
, followed by a floating-point number, followed by the string " degrees at a height of "
, followed by an integer, followed by the string " meters."
, then you can use the functions strtof
and strtol
, but you must skip the strings first, for example like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
char *line = "Please aim 3.567 degrees at a height of 5 meters.";
char *part1 = "Please aim ";
char *part2 = " degrees at a height of ";
char *part3 = " meters.";
float degrees;
long height;
size_t part1_len = strlen( part1 );
size_t part2_len = strlen( part2 );
size_t part3_len = strlen( part3 );
//make p point to start of line
char *p = line;
char *q;
//verify that the line starts with part1 and skip it
if ( strncmp( p, part1, part1_len ) != 0 )
{
fprintf( stderr, "Unable to find part1!\n" );
exit( EXIT_FAILURE );
}
p = part1_len;
//attempt to convert degrees value and skip it
degrees = strtof( p, &q );
if ( p == q )
{
fprintf( stderr, "Error converting degrees!\n" );
exit( EXIT_FAILURE );
}
p = q;
//verify that the line continues with part2 and skip it
if ( strncmp( p, part2, part2_len ) != 0 )
{
fprintf( stderr, "Unable to find part2!\n" );
exit( EXIT_FAILURE );
}
p = part2_len;
//attempt to convert height value and skip it
height = strtol( p, &q, 10 );
if ( p == q )
{
fprintf( stderr, "Error converting height!\n" );
exit( EXIT_FAILURE );
}
p = q;
//verify that the line continues with part3 and skip it
if ( strncmp( p, part3, part3_len ) != 0 )
{
fprintf( stderr, "Unable to find part3!\n" );
exit( EXIT_FAILURE );
}
p = part3_len;
//verify that we have reached the end of the string
if ( *p != '\0' )
{
fprintf( stderr, "Unexpected character encountered!\n" );
exit( EXIT_FAILURE );
}
//print the results
printf(
"Results:\n"
"Degrees: %f\n"
"Height: %ld\n",
degrees, height
);
}
This program has the following output:
Results:
Degrees: 3.567000
Height: 5