Cesar Pawlik
Cesar Pawlik

Reputation: 21

Beginner question/problem: Scanf not working as intended in the MacOS terminal

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!

Upvotes: 1

Views: 484

Answers (2)

S Dao
S Dao

Reputation: 573

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.

Upvotes: 0

th33lf
th33lf

Reputation: 2275

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.

Upvotes: 2

Related Questions