Question marks in console output of char(int).

I've got a task (School) to print a Ascii table of the integer values: 32-255.
For some reason everything after 127 is question marks.

Any help will be greatly appreciated.

Here's my current code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<iomanip>
using namespace std;

int main(){


	int n;	
		for(int m=0;m<=(255-32)/7;m++){
		n=m;
			while(n<=255-32){
				char character = (char) n+32;
				cout << setw(5) << n+32 << setw(3) << (character); //displaying downwards with 7 columns
			n+=32;
			}
		cout << endl;	
		}
}
Last edited on
ASCII uses only 7 bits so 127 is the highest ASCII code. There exist many different character encoding that are built on top of ASCII. They use more bits to extend the number of characters but often use the same code values for the characters in the range 0-127 as ASCII does.

On Linux the standard character encoding is UTF-8 (Unicode). It is backward compatible with ASCII so a file written in ASCII is also valid UTF-8. The eighth bit, which is unused in ASCII, is used in UTF-8 to signal that a character uses more than one byte. The more unusual characters can use up to 4 bytes. You can read more about UTF-8 here if you're interested: https://en.wikipedia.org/wiki/Utf-8#Description

I don't think your teacher is expecting you to output Unicode above 127 so if I were you I would either reduce the range to 0-127 and leave a comment stating that ASCII is only 7 bits or ask them to clarify. My suspicion is that they assume you use the command prompt on Windows which I think uses some kind of 8-bit character encoding. I think this is what's sometimes referred to as "extended ASCII" but I'm not sure.
Last edited on
Thanks for the clear answer :)

We are specifically asked to output the range 32-255 so i suspect the teacher assumed everyone used Windows prompt.

In actuality this should mean that on the teacher's computer (Probably Windows) the Ascii characters will display correctly?

But just for the kicks. Is there any workaround that you know of?
(Maybe truncating the last bit of the digits?)

Thanks again.
Hergeirs wrote:
In actuality this should mean that on the teacher's computer (Probably Windows) the Ascii characters will display correctly?

Yes, I would think so, but to be sure you probably want to test it on a Windows machine before handing it in.

Is there any workaround that you know of?

Not that I know of.
if in Windows, your can write system("chcp xxx");
see:https://technet.microsoft.com/en-us/library/cc733037(v=ws.11).aspx
Topic archived. No new replies allowed.