1

The following code fails to compile using GCC:

int main()
{
    for(int j=0; j<80; j++) //for every column,
    { //ch is ‘x’ if column is
        char ch = (j%8) ? ‘ ‘ : ‘x’; //multiple of 8, and
        cout << ch; //‘ ‘ (space) otherwise
    }
    return 0;
}

It should print: x x x x x x x x x

But I get the following error:

error: extended character ‘ is not valid in an identifier

wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • Please specify the exact error you get. You probably need to replace `‘` and `’` with `'`. – wohlstad Mar 05 '23 at 09:55
  • **error: extended character ‘ is not valid in an identifier** – Jena Travolta Mar 05 '23 at 09:57
  • (1) The error should be a part of your question - please [edit] it. (2) As I wrote, replace `‘` and `’` with `'` (the single quote character you used is not the proper one). – wohlstad Mar 05 '23 at 09:58
  • this should print : **x x x x x x x x x** As j cycles through the numbers from 0 to 79, the remainder operator causes the expression (j % 8) to become false—that is, 0—only when j is a multiple of 8. So the conditional expression (j%8) ? ‘ ‘ : ‘x’ has the value ‘ ‘ (the space character) when j is not a multiple of 8, and the value ‘x’ when it is. You may think this is terse, but we could have combined the two statements in the loop body into one, eliminating the ch variable: cout << ( (j%8) ? ‘ ‘ : ‘x’ ); – Jena Travolta Mar 05 '23 at 10:01
  • 1
    Your last comment is still using the wrong single quote character. Try to use `'`. – wohlstad Mar 05 '23 at 10:02
  • I can't find this to fix it, lol – Jena Travolta Mar 05 '23 at 10:14
  • What can't you find ? – wohlstad Mar 05 '23 at 10:15
  • @wohlstad the wrong single quote character – Jena Travolta Mar 05 '23 at 10:17
  • 1
    Instead of `‘ ‘ : ‘x’`, you should use `' ' : 'x'`. Note the different single-quote. – wohlstad Mar 05 '23 at 10:18
  • @wohlstad yes this was actually the issue, your comment is the solution to my problem. – Jena Travolta Mar 05 '23 at 10:21

1 Answers1

3

In this line:

char ch = (j%8) ? ‘ ‘ : ‘x’;

You are using the wrong single-quote.
The proper one is ', i.e.:

//----------------V-V---V-V-
char ch = (j%8) ? ' ' : 'x';

This is specified in the Character literal documentation.

wohlstad
  • 12,661
  • 10
  • 26
  • 39