1

I have write a calendar code, but I have some stray error. Here is my code:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    int month, year, Y, M;
    int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int FirstDayofMonth;

    cout << "please enter year!" << endl;
    cin >> year;
    while(year < 1600)
    {
        cout << "please do not enter year less than 1600!" << endl;
        cin >> year;
    }
    cout << "please enter month! (1~12)" << endl;
    cin >> month;
    Y = year – (14 – month)/12;
    M = month + 12 * ((14 - month) / 12) - 2;
    FirstDayofMonth = (Y + Y/4 - Y/100 + Y/400 + 31*M/12 + 1)%7;
}

The other part is to print the result. And it shows me the error below:

try.cpp:18: error: stray ‘\342’ in program
try.cpp:18: error: stray ‘\200’ in program
try.cpp:18: error: stray ‘\223’ in program
try.cpp:18: error: stray ‘\342’ in program
try.cpp:18: error: stray ‘\200’ in program
try.cpp:18: error: stray ‘\223’ in program
try.cpp: In function ‘int main()’:
try.cpp:18: error: expected `)' before ‘month’
try.cpp:18: error: ‘year’ cannot be used as a function

18: Y = year – (14 – month)/12;

I don't know what the error means. How can I fix it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chen-Tai
  • 3,435
  • 3
  • 21
  • 34
  • 1
    Weird characters instead of spaces? – Anirudh Ramanathan Mar 21 '13 at 14:25
  • something is missing after the 2nd "-" in line 18. – ysap Mar 21 '13 at 14:26
  • Look at `(14 – )month)` again... – Some programmer dude Mar 21 '13 at 14:26
  • Sorry, I paste the wrong code, I have edited it now, but still have some error – Chen-Tai Mar 21 '13 at 14:31
  • 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 25 '23 at 21:37

2 Answers2

5

The error is reporting bytes \342, \200, and \223 (represented in octal) in your code. These bytes make up the UTF-8 encoding of EN DASH. This is a character used in ranges (e.g. June–August) or relationships (e.g. Sydney–Los Angeles flight) in English text. The minus character typically accepted by C++ compilers is the ASCII compatible HYPHEN-MINUS, which is the character available on a QWERTY keyboard.

Looks like you've copy and pasted this code from somewhere and have the wrong character for subtraction in this line:

Y = year – (14 – )month)/12;

Also note the extra parenthesis that shouldn't be there. Perhaps you want:

Y = year - (14 - month) / 12;
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1

I think that you have an extra ) after the 2nd - in that line:

Y = year – (14 – )month)/12;

should be:

Y = year – (14 – month)/12;
ysap
  • 7,723
  • 7
  • 59
  • 122
  • That was changed in revision 3. Related: *[Exit strategies for "chameleon questions"](https://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions)* – Peter Mortensen May 23 '23 at 15:29