How to accept a full and partial input String from the user?

Fred

I am working on a C program which accepts a string from the user.

add, show, printall, find, delete, and quit.

The program must allow the user to type a partial or full option name (listed above) to choose an option.

For example, if the user gives 'a' or 'ad', the program must accept it as an option for add. (I AM HAVING TROUBLE WITH THIS PART)

My code below:

while(strcmp(input, "quit") != 0)
{

printf("Add a record");

printf("\nShow a record");

printf("\nDelete a record");

printf("\nFind a record");

printf("\nPrint all records");

printf("\nQuit");

scanf("%s", &choice);

  
/*Above it says it should accept a or ad for 'add'. In my actual code it only accepts add. That's what I need help with. */  
if(strcmp(choice, "add") == 0)
{
     addMenu(&start);
}
else if(strcmp(choice, "delete") == 0)
{
   deleteMenu(&start);
}
else{
    printf("\nInvalid menu choice!");
}... other options
}
Siguza

The easiest way to do this is probably to combine strlen and strncmp:

size_t len = strlen(choice);
if(strncmp(choice, "add", len) == 0)
{
    addMenu(&start);
}
// likewise for the rest

If there is only "a", it will only compare the first char.
With "add" it will compare three characters.
With "adda" it will try to compare four, which will hit the null terminator of the "add" literal, and fail.

You might want to make sure that len != 0 though, otherwise it would match every command.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to accept user input from within a function?

How to accept input from user (console) in erlang

Accept user input from Activity

How to accept an input in the console from the user using JavaScript?

Accept YAML input from website user

How to accept user input for the size of a vector?

How to accept user input inside while loop

Bash: how to properly accept user input?

How to accept user input for a constant and use writeprocessmemory?

How safe $mysqli->real_escape_string for controlling one select query that accept user input

Extract partial string from a pattern based on input

How can I accept only a specific number of lines from user input using scanner in java

If statement needs to accept multiple forms of user input String data type

Making a user input only accept int but store in String

How to accept graph from input file

Unable to take full String input from console

Changing a string from user input

Select2: How to allow user to select from a default list but accept a new string?

ARRAY- Accept user input and output the corresponding choice from array

Accept number with "/" from input user and exception when it is a letter in python

Accept 5 numbers from user input and output the largest and smallest

imaplib finds the full subject string but not partial - how to fix?

How to pass an input string from user to a MIPS program

how to get the string from user input in alert textfield

How to compare user input (from std::cin) to a string?

How to count number of words in a text string from user input

How do I replace a certain string in a list from user input?

How to split each character on a String from user input?

How do I draw string from user input in a JFrame Java