Strange output with consecutive characters in cout

Hi.

I'm experiencing a strange issue with cout. What it seems to boil down to is that if I have a bunch of consecutively numbered characters output next to each other in cout, the first two are displayed incorrectly --- they show up as the venus/mars symbols (♀ and ♂), which seems to be how the display shows the ASCII values 12 and 11 respectively.

I'm using Dev-C++ on Windows 7, and I haven't noticed anything like this on any other occasion.

Here is a minimal example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    char x = 92;    // corresponds to '\' 

    while(x > 10) 
    {
        cout << x;
        x--;
    }

    return 0;
}


The output begins ♀♂ZYXWVUTSRQPONM..., whereas I would expect it to be \[ZYXWVUTSRQPONM...

I have tried several things, and observe the following:
- x = 92 is the highest number with this problem, starting at any value higher gives the output I would expect. Any number

below is again incorrect in the first two outputs
- Having the condition in the while loop be x > 11 gives an error only in the first output character, ♀[ZYXWVUTSRQPONM..., and is the number is higher than 11 gives the output I would expect.
- New lines in between characters (such as including a line cout << "\n"; in the loop) seems to make things output ok.
- For every starting value which causes the issue, if I output the starting character enough times first the output is fine,

e.g. if I have something like
1
2
3
4
5
    int y = 10;
    x = x - y;
    
    for(int i = 0; i <= y; i++) 
        cout << x;

before the loop the output is fine, for any positive value of y.
- This doesn't occur if I output to a file.

I've tried searching on the web, and dev-cpp and windows forums, but so far haven't been able to find anything. Apologies if this is something really basic, but I can't seem to figure out what the issue is.
Last edited on
The venus and mars symbols are sometimes displayed for chars with value 11 and 10 ( http://www.theasciicode.com.ar/ascii-control-characters/vertical-tab-male-symbol-mars-ascii-code-11.html )

Some ascii characters are meant to be instructions to the output device (for example, FORM FEED and CARRIAGE RETURN). I wonder if your terminal is interpreting something as instructions to go back to the start of the line, and is then writing out these symbols for 11 and 10, or if your terminal is simply becoming internally confused (by which I mean, was never meant to deal with these control characters so isn't displaying sensible behaviour).

If this is the case, you should be able to experiment with outputting various char values and identify which of the control values are sending the output back to the start of the line.

It would also be worth running the same program under a different terminal (although I don't even know if windows has such things; possibly you've only got the win32 console available to you).
Last edited on
The problem is that all ASCII characters below 32 (space) are control characters, and some of those characters can manipulate the position of the console's "writing head". What's happening is that character 13 (carriage return) puts the head right back to the start of the line, then the program proceeds to output characters 12 (form feed) and 11 (vertical tabulation), which don't have any special functions in your console, so they are displayed as symbols. The characters that you previous wrote there are thus overwritten.
Simply don't print ASCII characters lower than 32.

EDIT: Darn. Moschops beat me to it.
Last edited on
Thanks so much both of you, that makes a lot of sense, that seems to be what's happening.

I wouldn't ordinarily use these characters, it was just an issue that had come up testing another program.
Of course, other output devices will behave quite differently, which is all part of the fun.
For example, you could redirect the output to a file.
Topic archived. No new replies allowed.