1

I have a question about reading input from scanf.

The user enters one of two commands. Either a single worded command, or a two worded command separated by one space.

For example:

start

OR

begin program

I am trying to read from scanf. But I have a problem. If I do scanf("%s", input); I am assuming he will enter only the one worded command.

If I do scanf("%s %s", input1, input2); I am assuming he will enter a two worded command. But if he enters a one worded command, then the scanner will continuously consume white space until a different character is read.

For the two worded command I want to store each word in a separate string variable.

Please help.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
user2817240
  • 195
  • 1
  • 5
  • 13

2 Answers2

1

Remove space in format string, change:

scanf("%s %s", input1, input2);

as

scanf("%s%s", input1, input2);

To understand this behavior read manual: int scanf(const char *format, ...);:

A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.

So because of the space after first %s the scanner continuously consume white space until a different character is read.

Read: "C Printf and Scanf Reference" good tutorial.

Related Question: If you are interested read also "Store data in array from input" question and answer it will help your further.

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • When I read the question, I thought he was talking about how to parse one or two strings without knowing what was in the input. – Charlie Burns Oct 15 '13 at 15:45
  • @CharlieBurns Read linked answer and my reference to scanf and printf ..I sure you like that. – Grijesh Chauhan Oct 15 '13 at 15:47
  • This wasn't what I was looking for. Charlie burns is right, I do not know whether the input is two words or one. – user2817240 Oct 15 '13 at 17:54
  • Your answer assumes that it is already a two word answer. When I try to enter a one word command it waits for me to put in another character before it continues. – user2817240 Oct 15 '13 at 17:55
  • @user2817240 See I given you the reason of how your `scanf()` behaving and why? What exactly you wanted to ask is unclear. either post a new question. or ask in comment. – Grijesh Chauhan Oct 15 '13 at 17:57
  • Nvm, I figured it out. Look at Chuck Cottrill's answer. – user2817240 Oct 17 '13 at 00:09
1

Another way, read the first word, and then conditionally read the second word,

#include <stdio.h>
#include <string.h>
int main(void)
{
    char cmd[100];
    char cmd1[100],cmd2[100];
    printf("enter command:"); fflush(stdout);
    scanf("%s",cmd1);
    printf("%s\n",cmd1);
    if( strncmp(cmd1,"begin",strlen("begin"))==0 ) {
        printf("read second word\n");
        scanf(" %s",cmd2);
    }
    else strcpy(cmd2,"none");
    printf("%s,%s\n",cmd1,cmd2);
}

results,

$ ./scanf2 
enter command:start
start
start,none
$ ./scanf2 
enter command:begin command
begin
read second word
begin,command
ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42
  • I prefer fgets and then sscanf or strtok to parse input. – ChuckCottrill Oct 15 '13 at 18:08
  • @user2817240 Good that you find answer, now you may like to [accept this answer by click on right-mark](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work?answertab=votes#tab-top) preferable way to say thanks is by voting up :) – Grijesh Chauhan Oct 17 '13 at 04:11