0

I have 2 questions.

Here in the C++ reference

#include <stdio.h>

int main ()
{
  char sentence []="Rudolph is 12 years old";
  char str [20];
  int i;

  sscanf (sentence,"%s %*s %d",str,&i); <---
  printf ("%s -> %d\n",str,i);

  return 0;
}

Question 1. What exactly is %*s doing?

My program I'm building a hash table.

It queries the user to either type in

q- quit
i <int> - inserts integer //must be on same line
d <int> - deletes integer //must be on same line
etc....

For example:

in order to insert "35" I would have to type:

i 35

Question 2. Would the C++ reference work for both 'q' and 'i 35' since 'q' has no integer with it?

char choice[10];
char option;
int i;

sscanf(choice, "%c %d", &option, &i);

Would this work if 'q' was entered(no integer attached) as well as if "i 35" was entered (w/integer attached)?

1 Answer 1

2

It means it should skip that type from the stream. So for example the input stream is:

"Rudolph is 12 years old"

The first %s will capture "Rudolph", the %*s will "read but ignore" "is" (i.e. not store it in a variable), and then the %d will read and capture the 12.

I'm not quite sure what you mean with your second question. I believe you're asking if the "%s %*s %d" format will work with reading "q" from the input. In that case, you should use a different format, like "%s" or "%c" for a single character.

In response to your updated question, you can easily try it and see. In my tests, it does work with an input of "q": sscanf leaves the int alone but does read in the "q".

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

Comments

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.