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.