Codeblocks not printing £

I ran a program that had a £ in a string but it's printing a 'u' instead.
In the string the £ is underlined in red.
Anyone know what's causing this?
closed account (zb0S216C)
Post the relevant code.

Wazzak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cstdlib>
#include <cstdio>

class Books
{
public:
    char Cisbn[128];
    int Cnumbersold;
    double Cprice;
};

    Books InputBooks()
    {
        Books details;
        std::cout << "\nEnter book number: ";
        std::cin >> details.Cisbn;
        std::cout << "\nEnter quantity sold: ";
        std::cin >> details.Cnumbersold;
        std::cout << "\nEnter price: ";
        std::cin >> details.Cprice;
        return details;
    }

    int AskForInput(Books counter[], int MaxSize)
    {
        int index;
        for(index = 0; index < MaxSize; index++)
        {
            char letter;
            std::cout << "Enter another book? (Y or N)";
            std::cin >> letter;
            if (letter != 'Y' && letter != 'y')
            {
                break;
            }
            counter[index] = InputBooks();
        }
        return index;
    }

     void printBook(Books details)
        {
        std::cout << "Book ISBN: " << details.Cisbn << std::endl;
        std::cout << "Number of books sold: " << details.Cnumbersold  <<std::endl;
        std::cout << "Price of Book: " << details.Cprice << std::endl;
        std::cout << "Total Sales for book £" << details.Cprice * details.Cnumbersold;
        }

        void displaybooks(Books counter[], int NumOfBooks)
        {
            for(int ind = 0; ind < NumOfBooks; ind++)
            {
                printBook(counter[ind]);
            }
        }

         void sortbooks(Books counter[], int NumOfBooks)
        {
            int swaps = 1;
            while(swaps != 0)
            {
                swaps = 0;
                for(int n = 0; n < NumOfBooks - 1; n++)
                {
                    if (counter[n].Cisbn > counter[n+1].Cisbn)
                    {
                        Books temp = counter[n+1];
                        counter[n+1] = counter[n];
                        counter[n] = temp;
                        swaps++;
                    }
                }
            }
        }

int main()
{
    Books counter[128];
    std::cout << "Enter ISBN/Quantity sold/And Price of book\n";
    int NumOfBooks = AskForInput(counter, 128);
    sortbooks(counter, NumOfBooks);
    std::cout << "Here is the list of book sorted by price" << std::endl;
    displaybooks(counter, NumOfBooks);
    return 0;
}
Most likely you need to change the code page to 1252. The code page controls which set of glyphs are displayed for each character.

The following sample program changes to code page 1252 and then displays the character set. I think the code page can also be set via a console command, maybe in a .bat file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <iomanip>
#include <windows.h>

    using namespace std;

int main()
{
    int codepage = GetConsoleCP();
    cout << "Code page = " << codepage << endl;
    SetConsoleOutputCP(1252);  // default is dos 850.
    SetConsoleCP(1262);        // FOR INPUT


    char eof       = 0;
    char bell      = 7;
    char backspace = 8;
    char tab       = 9;
    char creturn   = 13;
    char linefeed  = 10;

    for (int i=0; i<256; i++)
    {
        if (i%16 == 0)
            cout << endl << endl;

        cout << setw(4) << right << i << " ";

        if (i==eof || i==bell || i==backspace || i==tab || i==creturn || i==linefeed)
            cout << ' ';
        else
            cout << (char) i;
    }

    return 0;
}
Last edited on
In case you happen to use other compilers, here's a bit of exposure on how this works across platforms

First, of all, simplifying the example:

1
2
3
4
5
#include <iostream>
int main()
{
    std::cout << "Total Sales for book £" << 10 << '\n';
}

On Unicode-friendly OS's, such as Linux, this works as written: http://ideone.com/IdbyLh

The standard, portable C++ approach is to use wide streams, since '£' is not an ASCII character:

1
2
3
4
5
6
7
8
#include <iostream>
#include <locale>
int main()
{
    std::locale::global(std::locale("en_US.utf8")); // any unicode works
    std::wcout.imbue(std::locale());
    std::wcout << L"Total Sales for book £" << 10 << '\n';
}

This works on any OS that has standard Unicode support of C and C++: http://ideone.com/q63Mvr

Windows has no standard Unicode support, so you have to resort to OS-specific API calls to get anything done. I would avoid touching console codepages for being too incompatible with the rest of the world. You can actually use standard C++, only the magic words to turn on wcout are different:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <fcntl.h>
#include <io.h>
using namespace std;
int main()
{
    _setmode(_fileno(stdout), _O_WTEXT);
    std::wcout << L"Total Sales for book £" << 10 << '\n';
}

tested on Visual Studio 2012, I don't have C::B
Just some comments not related to your problem - just trying to provide a little education to show a better way of doing things:

Your functions should be part of the book class. Ideally you should have the class declaration in a .h file with the function definitions in a .cpp file

I personally hate (just ugly with the &&'s and !='s, and not scalable for other things) constructs like line 33 - much better to make use of the toupper function to avoid testing twice.

HTH
Thank's for the replies,

1
2
3
4
5
6
7
8
[code]#include <iostream>
#include <fcntl.h>
#include <io.h>
using namespace std;
int main()
{
    _setmode(_fileno(stdout), _O_WTEXT);
    std::wcout << L"Total Sales for book £" << 10 << '\n';
[/code]

Would'nt work inCodeblocks :(
Topic archived. No new replies allowed.