-2

I was given a formula in this format

Data  = [ (percentage / 15) – (var1 + var2)  / 20]  *  [ var3 +  var4];

If I put the above statement in, it will compile with

sample.cpp:131:1: error: stray ‘\342’ in program
sample.cpp:131:1: error: stray ‘\200’ in program
sample.cpp:131:1: error: stray ‘\223’ in program

sample.cpp:131:81: error: ‘(percentage/ 15)’ cannot be used as a function

How do I change the data statement to make the formula work in C++?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
baoky chen
  • 799
  • 2
  • 11
  • 20
  • 5
    Just replace the brackets with parens. – 001 Oct 02 '12 at 16:55
  • Seriously, is this a joke? Any introductory C++ material will cover this somewhere in the first pages. SO is not a substitute for research. – R. Martinho Fernandes Oct 02 '12 at 17:00
  • doesnt work. if i replace brackets with parens – baoky chen Oct 02 '12 at 17:00
  • 1
    @baokychen: Type the code, rather than cut-n-paste. The `stray '\xxx'` errors usually indicate that there are non-standard caracters in the input that the compiler does not like. – David Rodríguez - dribeas Oct 02 '12 at 17:03
  • You should show us what you tried and the errors you got (after replacing the `[]` with `()`.) – juanchopanza Oct 02 '12 at 17:03
  • For the first problem (stray characters), this is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 27 '23 at 16:31
  • There must be a canonical for the second problem as well (for such a basic problem, even then. It was 4 years after [Stack Overflow](https://stackoverflow.design/brand/copywriting/naming/) launched). Though admittedly it is nontrivial to find. – Peter Mortensen Apr 27 '23 at 16:45

1 Answers1

4

There are two problems here:

  1. You cannot use [ and ] for grouping expressions. Change them to ( and ).
  2. The minus sign you're using is actually an en-dash (unicode character U+2013) not the normal hyphen-minus character that you should be using (U+002D). The mapping from source file characters to the basic source character set is implementation defined and your compiler doesn't map en-dash to the minus character (like most compilers, I imagine).

The tip here: don't just copy text from a document to your code, especially when that other document isn't code.

Here's the correct expression:

Data  = ((percentage / 15) - (var1 + var2) / 20) * (var3 + var4);

The error you're getting shows that there are three bytes appearing that shouldn't be there. They are (in octal): 342 200 223. In binary, that's 11100010 10000000 10010011. A quick google search will tell you that that's the binary representation of U+2013.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324