-1

Trying to write a program to work on a triangle, the user inputs information in the format P0 x y, P1 x y ... but if the user inputs Q then the program terminates. I understand the scanf function will be used, along with if else statements with logical operators, however i'm not sure how to accomplish it.

When i run the code and type 'Q' it still asks for 2 more prompts. I was thinking about using a string array but not sure how, and suggestions would be great, thanks.

#include <stdio.h>

void main()
{
    float x, y;
    char Q, input;

    scanf("%c %f %f", &input, &x, &y);

    if (input == 'Q')
        return;
    else
        printf("points are %f %f", x, y);

    return(0);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    `scanf("%c %f %f", &input, &x, &y);` does not only read one line of input. More complex than that. Recommend to read 1 line of user input with `fgets()`. Then parse with `sscanf()` or others. – chux - Reinstate Monica May 09 '20 at 04:24
  • 1
    When reading characters, it's usually a good idea to use `" %c %f %f"` with a space before the `%c` conversion specification. That will skip over the newline left behind by the `%f` operations when you attempt to read a second line. You're not looping here, so that won't immediately affect you, but once you try to process a second and subsequent lines, it matters. – Jonathan Leffler May 09 '20 at 04:24
  • OT; regarding: `void main()` There are only two valid signatures for `main()` ) regardless of what visual studio allows) there are `int main( void )` and `int main( int argc, char *argv[])` However the posted code says the returned type is `void`, to the statement; `return(0);` will cause the compiler to complain. – user3629249 May 09 '20 at 05:17

2 Answers2

2

Since you have 3 format operators in the format string, scanf() won't return until it fills in all 3 variables or gets an error or EOF.

Call scanf() twice. Once for the character and then for the numbers if the character isn't Q.

#include <stdio.h>

void main()
{
    float x, y;
    char input;

    scanf(" %c", &input);

    if (input == 'Q') {
        return;
    }

    scanf("%f %f", &x, &y);
    printf("points are %f %f", x, y);

    return;
}

When you turn this into a loop that reads multiple inputs, make sure you put a space before %c so it will skip over whitespace before reading the character. Otherwise it will parse the newline after the last y input. See The program doesn't stop on scanf("%c", &ch) line, why?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • How does the second scanf() come into it, i understand scanf() to request user input, would two request the user to input the data twice? –  May 09 '20 at 04:20
  • The second scanf reads the rest of the input. It doesn't care if it's on the same line or a different line, it skips over any whitespace until it gets to the start of a number. – Barmar May 09 '20 at 04:21
  • So because the user inputs three variables in the first scanf, and because theres two placeholders in the second scanf, it gets scanned in? –  May 09 '20 at 04:34
  • `scanf()` stops reading from standard input as soon as it fills in all the formats that it's asked for. So the first scanf reads the first input, the second scanf reads the the next two. – Barmar May 09 '20 at 04:37
  • So its entirely possible to set up multiple scanf functions to catch a single scanf that receives multiple inputs? –  May 09 '20 at 04:49
  • There's little difference between reading two inputs with one scanf or two scanf's, except that in the first case it won't return until it has read both input, while the second case will return after each input. – Barmar May 09 '20 at 16:09
0

In addition to the other answer, you might also want to check for character 'q' along with 'Q'.

try if (input == 'Q' || input =='q')