Input
void sum(int a, int b) {
a b;
}
void quit() {
exit(0);
}
int main() {
char str[10];
int x, y;
printf("Call a function");
scanf("%s %d %d", str, &x, &y); //<-this is the part I cannot resolve
//tried to call a function but failed
if (strcmp(str, "quit") == 0)
quit();
else if (strcmp(str, "sum") == 0)
sum(x, y);
}
Summary:
sum(int a, int b) and quit() function requires different scanf. If I want to call a function regardless of the arguments how should I fix it? I want the program to identify what function to be called according to user input.
plus: the code only works for sum() function because the scanf() only accepts ["%s %d %d", str, &x, &y] input. However, I want program to scan sum() and quit() both.
CodePudding user response:
When dealing with line-based user input, it makes sense to always read one line at a time. This is not what the function scanf
does. For example, if you use the "%s %d %d"
format string, then scanf
will continue reading input until it is able to match both numbers, or until it encounters a character that makes the match impossible (such as an alphabetical character). It will continue reading input past the end of the line if the user enters "quit"
without entering two numbers on the same line, which is probably not what you want.
For reading exactly one line of input, I recommend that you use the function fgets
. You can then, if you want, use sscanf
on that single line of input. That way, you won't have the problem of your program attempting to read more than one line of input. The function sscanf
will simply report that it was only able to match the first specifier, if the user does not enter any numbers afterwards.
With the functions scanf
and sscanf
, you can check the return value of the function to determine how many specifiers in the format string were matched.
It is unsafe to use the result of scanf
or sscanf
without first checking the return value of that function, in order to determine how many arguments were matched. Otherwise, you may be using a non-existant result. See this guide for further information:
A beginners' guide away from scanf()
Also, when using the %s
format specifier in scanf
or sscanf
(but not printf
), it is generally a good idea to limit the number of characters written. Otherwise, a buffer overflow will occur if the user enters more than 9 characters (10 characters including the terminating null character), because the array str
only has room for 10 characters. A buffer overflow should always be prevented, because it can cause bugs that are very hard to find.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sum( const int a, const int b )
{
printf( "The sum of %d and %d is %d.\n", a, b, a b );
}
void quit()
{
printf( "Quitting.\n" );
exit( EXIT_SUCCESS );
}
int main( void )
{
char line[100];
char str[10];
int x, y;
int num_matched;
printf( "Call a function: " );
if ( fgets( line, sizeof line, stdin ) == NULL )
{
fprintf( stderr, "Input error!\n" );
exit( EXIT_FAILURE );
}
num_matched = sscanf( line, "%9s %d %d", str, &x, &y );
if ( num_matched >= 1 && strcmp( str, "quit" ) == 0 )
quit();
else if ( num_matched >= 3 && strcmp( str, "sum" ) == 0 )
sum( x, y );
else
printf( "Bad input!\n" );
}
This program has the following behavior:
Call a function: sum 5
Bad input!
Call a function: sum 5 7
The sum of 5 and 7 is 12.
Call a function: quit
Quitting.
CodePudding user response:
You can scan the integer inputs separately, after you've established that you need them (in its if
block):
int main() {
char str[10];
int x, y;
printf("Call a function");
scanf("%s", str); // you only need to know the function name for now
if (strcmp(str, "quit") == 0)
{
quit();
}
else if (strcmp(str, "sum") == 0)
{
scanf("%d %d", &x, &y);
sum(x, y);
}
}
CodePudding user response:
Im a little confused with the question statement. Take a look at the modifications and tell me if it's fixed your problem or made it clearer. If you want to read what void sum()
computes then you need to change it to a different return value like int sum()
#include <stdio.h>
#include <stdlib.h>
void sum(int a, int b) {
a b;
printf("sum was called"); //this just confirms that sum was used since no return value
}
void quit() {
exit(0);
}
int main() {
char str[10];
int x, y;
printf("Call a function");
scanf("%s %d %d", str, &x, &y);
if (strcmp(str, "quit") == 0)
quit();
else if (strcmp(str, "sum") == 0)
sum(x, y);
quit();
return 0;
}
CodePudding user response:
Store char str1[]="quit"
and
char str2[]="sum"
strings in different array then compare it using strcmp(str,str1);
or strcmp(str,str2);
accordingly.
Because during each comparison result will be 1 (if done without string array) and conditional statements will not execute (since \0
will not be there if compared directly)
since array of string will store \0
at the end so comparison will be successful.