I am just starting to learn C++ with the Waite group primer. I wrote a program loosely based on the books instructions to display any entered ASCII character and its numeric value and the same for the next character in the table.

Heres the bug: The second character works fine, but the first always and without fail comes out as -52 which is impossible. Here is the code:

// morechar.cpp -- char and int types contrasted
#include <iostream>
int main()
{
        using namespace std;
        char ch;   //declare a variable ch
        int i = ch;     // stores same code in an int

        cout << "Enter any ASCII character:\n";
        cin >> ch;
        cout << " The ASCII code for " << ch << " is " << i << endl;

        cout << "Adding one to the character code takes you to the next character.\n";
        ch = ch + 1;    //change the character code in ch
		i = ch;
        cout << "The ASCII code for " << ch << " is " << i << endl;
        
        //using the cout.put member function to display a char
        cout << "Displaying  char ch using cout.put(ch): ";
        cout.put(ch);

        //using cout.put() to display a char constant
        cout.put('!');

        cout << endl << "done." << endl;
        return 0;
}

Thanks in advance.

Hi

Your code:
char ch;
int i = ch;
You didn't assign a value to ch. So i is undefined. Also this would not work if you do cin >> ch.

You may try this instead:

char xxx = 'x';
cout << "char: " << xxx << " as int: " << (int) xxx << endl;
cout << "next char: " << ++xxx << " as next int: " << (int) xxx << endl;

If you know pointers, there would also be a solution.

crs,
tesu

thanks but I still don't get why ch isn't defined by the input or why undefined gets me -52

The first time you print 'i', it has the initial unknown value it got from 'ch'.

If you add the green and red lines below, you'll probably get the idea ...

// morechar.cpp -- char and int types contrasted
#include <iostream>
int main()
{
        using namespace std;
        char ch;   //declare a variable ch

        // ch is not initialized to any known value ...
        cout << "uninitialized ch is: " << ch << endl;

        int i = ch;     // stores same code in an int

        cout << "i, being assigned the uninitialized value of ch, is: " << i << endl;

        cout << "Enter any ASCII character:\n";
        cin >> ch;
        // align i with ch ...
        i = ch;
        cout << " The ASCII code for " << ch << " is " << i << endl;

        cout << "Adding one to the character code takes you to the next character.\n";
        ch = ch + 1;    //change the character code in ch
		i = ch;
        cout << "The ASCII code for " << ch << " is " << i << endl;
        
        //using the cout.put member function to display a char
        cout << "Displaying  char ch using cout.put(ch): ";
        cout.put(ch);

        //using cout.put() to display a char constant
        cout.put('!');

        cout << endl << "done." << endl;
        return 0;
}

instead of: int i = ch; // stores same code in an int (wrongly)

you might try: int*i =(int*)ch;

then
cin>>ch; // enter A for example
cout << ch; // gives A
cout << i; // gives 0x41
cout << (int)i; // gives 65

krs,
tesu

Thanks people. I had something along the lines of mitrmkar's thing before I checked, but I still don't get the -52 stuff

Thanks people. I had something along the lines of mitrmkar's thing before I checked, but I still don't get the -52 stuff

When you introduce a variable and don't assign it any value, the variable exists but it's value is simply random because it is not initialized. In general, initialize the variables you use to a known value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.