0

I am making a program that needs to receive the following input:

{Time1 Time2 Appointment} as {hour1:minute1 hour2:minute2 Appointment}

The "Appointment" is a useless string input and i would like to ignore it

scanf("%d:%d %d:%d",&hour1,&minute1,&hour2,&minute2);

I am able to get the hours and minutes but after that i can't use "scanf" again, how can i just ignore the "Appointment" input?

kaya3
  • 47,440
  • 4
  • 68
  • 97
  • 1
    If your intent is to really write this in C++, as your question's tag suggests, why are you using the ancient C library's `scanf`(), instead of modern C++ input streams? – Sam Varshavchik Mar 02 '20 at 12:51
  • maybe you can try scanf("%d:%d %d:%d %*s",&hour1,&minute1,&hour2,&minute2); see https://stackoverflow.com/questions/34874347/what-does-an-asterisk-in-a-scanf-format-specifier-mean – ieio Mar 02 '20 at 13:01
  • I am using the library in c++, is there any way to have this input entered with {cin}? – joão malheiros Mar 02 '20 at 13:17
  • 1
    If you want to ignore the rest of the input, then just close the input file. If you want to ignore only the remainder of input up to the next newline, you can use `*` in the appropriate format specifier. (eg, if Appointment is guaranteed to be simple string with no white space, you could just use `"%d:%d %d:%d %*s "`) – William Pursell Mar 02 '20 at 13:59
  • Whenever the question is "How can I do this complicated thing with `scanf`?", one good answer is always "Stop using `scanf`". It's really not very good for complicated things. See [What can I use for input conversion instead of scanf?](https://stackoverflow.com/questions/58403537) – Steve Summit Aug 13 '21 at 22:24

1 Answers1

0

You can do %*s thing, and also you can use an input buffer after the first scanf. Something like this:

int minute1 = 0, hour1 = 0, minute2 = 0, hour2 = 0;
char buffer[100];

scanf("%d:%d %d:%d %99[^\n]s",&hour1, &minute1 ,&hour2 ,&minute2, buffer);
printf("%d:%d %d:%d\n", hour1, minute1, hour2, minute2);
akrilmokus
  • 169
  • 9
  • %[^\n]s this will ignore space in string – akrilmokus Mar 02 '20 at 15:13
  • 4
    Allocating an input buffer like this is problematic: it is possible that the buffer will not hold the input (a generously sized buffer should work for trusted input), and it is prone to implementation mistakes (as in the posted code). Your code has a buffer overflow issue. `buffer` points to a single `char`; `scanf()` always writes a `\0` with `%s`, so only an empty string can be stored here without overflow. Even with a properly sized allocation, _always_ specify a max field width with `%s` or `%[]`to avoid buffer overflow (e.g., `%99[^\n]`, the `s` is not part of the `%[]` directive). – ad absurdum Mar 02 '20 at 15:23
  • Better to ignore with `%*[^\n]` or just discard unwanted input in the input stream. – ad absurdum Mar 02 '20 at 15:24
  • I'm not sure if I was clear: this answer has _undefined behavior_ if there is any "Appointment" input string following the integer inputs, i.e. any non-whitespace characters, due to buffer overflow in writing a string into a buffer array of only one `char`. – ad absurdum Mar 02 '20 at 15:57
  • 1
    Ignoring the issue of `buffer` being only large enough to hold the null terminator, there's no need to use malloc/free. You would get the same (problematic) results if you did `char buffer; scanf(" ... ", ..., &buffer);` – William Pursell Mar 02 '20 at 16:52