1

First off I want to mention that I´m completely new to programming, I started my first course this week. This problem seems odd however, here´s my code in C calculating the area of a triangle:

#include <stdio.h>

int main(void) {

  double base, height;

  printf("Type the base of the triangle\n");
  scanf("%lf\n", &base);
  printf("Type the height of the triangle\n");
  scanf("%lf\n", &height);

  printf("Here is the area of the triangle: %.2lf\n", (base * height) / 2);

  return 0;
}

It looks alright to me, however in the terminal I get the following result:

The terminal doesn´t "let me" continue to the next scanf unless I type in another number and press return. The value I choose for the height variable doesn´t matter aswell, as the result is (55)/2 instead of (56)/2. It ignores the value '6' and instead uses the second '5' that I typed in under "Type the base of the triangle".

Is anyone familiar with what the problem might be? I´m using MacOS High Sierra, if there are any more details required please let me know, and I appreciate any help I can get!

underscore_d
  • 6,309
  • 3
  • 38
  • 64
  • scanf adds a line feed `\n` character in stdin when you press enter after typing something. When you call scanf again, it might read that line feed instead of the actual typed value. Simplest way to discard that line feed is to add an empty `getchar();` line after each `scanf` line. This is a very common beginner problem that everyone faces at some point - I'll link some posts with more info. – Lundin Sep 04 '20 at 08:43
  • 2
    That is the wrong dup IMO. Newlines are filtered out by `scanf` when using the `%lf` format specifier. Please remove the newline from `scanf("%lf\n", &base);` and from `scanf("%lf\n", &height);` to make them `scanf("%lf", &base);` and `scanf("%lf", &height);` – Weather Vane Sep 04 '20 at 08:46
  • 1
    Eh, fair enough, here is the link for the OP anyway [scanf() leaves the new line char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Lundin Sep 04 '20 at 08:48
  • 1
    Welcome to SO. Please do not add pictures of plain text. Instead just copy&paste it into your question as you did with your code. – Gerhardh Sep 04 '20 at 09:38

2 Answers2

2

Let me start by urging you to read this article to know why you shouldn't use scanf and also how you should use it, in case you end up using it anyway: http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html

Now, putting \n in a scanf format string does not mean to expect a newline, but to read and discard any number of whitespace characters. Since %lf already discards leading whitespace, you don't need explicit whitespace in the format string anyway.

The \n in your case causes scanf to read characters until it finds a non-whitespace character, and it may need to read another line before it can find that non-whitespace character. This is why you are 'forced' to input another number (or any non-whitepace) before the code lets you move on. The fix here is just to use %lf, without the \n.

#include <stdio.h>

int main(void) {

  double base, height;

  printf("Type the base of the triangle\n");
  scanf("%lf", &base);
  printf("Type the height of the triangle\n");
  scanf("%lf", &height);

  printf("Here is the area of the triangle: %.2lf\n", (base * height) / 2);

  return 0;
}

However, there are caveats with this approach too, as the article I've linked to above will tell you.

th33lf
  • 2,177
  • 11
  • 15
0

The \n on the format string of scanf tells the the scanf to get Newline character also, but since Newline is part of Whitespace which will be skipped by scanf which means that after matching first number, the scanf look forward for \n or a Non-Whitespace character (since Newline character is ignored, scanf stops when matching 2nd 5).

Let's say the input buffer is 5\n\5\n\6\n, 2 scanf parsing input same buffer(stdin), the first one stopped when reading first input 5, result saved to base , the second one will continue at the point where the first one stopped -> means 2nd input 5 will be read to height therefore 3rd input 6 will be ignored(it can be any Non-Whitespace key to stop the scanner). The result will be (5*5)/2 = 12.5.

S Dao
  • 555
  • 4
  • 7