0

I'm wondering in C is there a way to prompt the user to enter 2 different values, and then store those two values separately, all in one user entry. For example:

Enter your age and blood type : 34 AB

Then we store the two enties separately like

fgets(string,string, 64, 64, stdin);

Clearly THIS won't work, but is there a way possible in C. I'm very new to C (2 days). I know in Java you can use the args[] defined in the main and grab command line entries by index, where each space in the user's input would be a different element in the args array.

4
  • Check the scanf() function (and its siblings). My C is a bit rusty so I won't chance my arm at an example. Commented Sep 23, 2016 at 5:46
  • Command-line arguments Commented Sep 23, 2016 at 5:47
  • "Yes, as many programs do". Commented Sep 23, 2016 at 5:49
  • @BLUEPIXY: The OP most likely refers to data entered via the terminal and not to arguments passed to a program on start-up. Commented Sep 23, 2016 at 6:17

3 Answers 3

8

args in main works in C too, though the conventional name is argv (argument vector), as in int main(int argc, char **argv).

You can also use scanf, as in scanf("%d %s", &age, blood_type);.

A third, and usually recommended way when processing user input, is to separate input from analyzing the input, as in:

fgets(line, sizeof line, stdin);
sscanf(line, "%d %s", &age, blood_type);

A more complete version of the above code, with error checking:

char line[100];
int age;
char blood_type[100];
if (fgets(line, sizeof line, stdin) == NULL ||
    sscanf(line, "%d %s", &age, blood_type) != 2) {
    fprintf(stderr, "Couldn't read age and blood type. Sorry.\n");
    exit(EXIT_FAILURE);
}

Since line contains at most 99 characters, plus the '\0' that marks the end of the string, we cant get an overflow in the variable blood_type. Otherwise, we could use %99s instead of just %s to limit the number of characters that can be put into blood_type.

Sign up to request clarification or add additional context in comments.

1 Comment

I'd add a note about checking result of scanf, because if there is parse error, the input variables are left unmodified (so possibly uninitialized, leading to UB). I'd also add a note about providing buffer size for %s to avoid buffer overflow by long user input.
0

The best way by far is

char string[100];

if (fgets(string, sizeof(string), stdin) != NULL) {
    // split the string here to extract the "n" values
}

10 Comments

You don't actually answer the question.
Dear Ingo Bürk, what you are doing by downvoting is wrong! You are demotivatiing the others to invest their time to the problem and recommend another solutions which may also be in scope of the interest of the author. Always remember it is not always easy to identify a need and form a proper question. This is not a binary wold yes no to judje like you do. I personally disrespect so non-constructive comments like yours.
@dmi We have a voting feature on SO for a reason. It's there to be used, so please don't argue that it shouldn't be used. If you care about silencing downvotes, please bring it up over in meta where it belongs. This discussion is getting out of hand – I've explained my reasons for the downvote and as long as the answer doesn't change, neither will my vote.
@dmi: the community gets to decide what is useful or not, by voting. Which is what Ingo did here, and even provided feedback as to why they think a post is not useful. Voting (either way) is an important task everyone plays. However, comments are not for extended discussion; take that to a chatroom or Meta Stack Overflow.
@IngoBürk I don't want you to remove your downvote, I am not a dictator and your are free to express what you think. I just, don't want to delete the answer because I don't consider it a bad answer. Not because it's my answer, but because I don't think it's a bad answer.
|
-1

One additional option (depends on your environment) - man getopt.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.