1

While compiling simple cpp file I got an error. I want to write a function that changes celcius to farenheit.

double przelicznik(double n)
{
    n = 1,8 * n + 32;
    return n;
}

Also it doesn't give me a correct result.

noname
  • 23
  • 3
  • 2
    It's `n = 1.8 * n + 32;`. The decimal separator is a dot `.` not a comma `,`. – jabaa Jul 31 '21 at 10:09
  • You set `n=1`, then just calculate (and discard) `8 * n + 32`. As @jabaa mentioned it: use a `.` in `c++`. – Lala5th Jul 31 '21 at 10:11
  • 1
    Does this answer your question? [Different behaviour of comma operator in C++ with return?](https://stackoverflow.com/questions/39364263/different-behaviour-of-comma-operator-in-c-with-return) – Yunnosch Jul 31 '21 at 10:13
  • If you do want to use the `','` as the decimal (radix), you will need to set the proper `LOCALE` that uses the `','` for that purpose. Otherwise, you will need to use a period. It it was just a typo, then Ooops -- just change the `'','` to a `'.'` and your good. – David C. Rankin Jul 31 '21 at 10:51
  • To see the effect of the comma operator, try both `n = 1, 8 + 32;` and `n = (1, 8 + 32);` You will receive the `[-Wunused-value]` when compiling both, but the output will be very different -- why? – David C. Rankin Jul 31 '21 at 11:08
  • 2
    Wdavid are you claiming setting LOCALE changes which deckmal seperator C/C++ uses in source code? Can you provide a link explaining? I am unaware of such a language feature. – Yakk - Adam Nevraumont Jul 31 '21 at 11:24

1 Answers1

1

The code is.

  n = 1, (8 * n + 32)

The comma operator is a fairly uncommon mechanism where multiple expressions can be done in sequence.

correct code.

n = 1.8 * n + 32;
mksteve
  • 12,614
  • 3
  • 28
  • 50
  • I suspect the user may actually be trying to use a `','` from his LOCALE without the LOCALE properly set (or it could just as easily be a typo...) – David C. Rankin Jul 31 '21 at 10:58
  • 1
    @David I don't think changing locale would have an effect on how C++ is parsed. You have to use `.` – Aykhan Hagverdili Jul 31 '21 at 12:43
  • @DavidC.Rankin `,` is an operator within the c++ language. Changing locale doesn't change how the compiler parses data. You may want to look at this: https://en.cppreference.com/w/cpp/language/operator_other – Lala5th Jul 31 '21 at 12:46
  • Thank you, silly me. I get used to comma to separate variable types in function, for example double varname(double, double), and i forgot that period is used for math. – noname Jul 31 '21 at 12:52