| | |
need help with ASCII
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 12
Reputation:
Solved Threads: 0
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:
Thanks in advance.
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:
C++ Syntax (Toggle Plain Text)
// 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.
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
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
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
•
•
Join Date: Nov 2007
Posts: 980
Reputation:
Solved Threads: 210
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 ...
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;
}•
•
Join Date: Nov 2007
Posts: 980
Reputation:
Solved Threads: 210
•
•
•
•
Thanks people. I had something along the lines of mitrmkar's thing before I checked, but I still don't get the -52 stuff
![]() |
Similar Threads
- ASCII values and characters (C)
- String to integer to ascii (C)
- Prog to list ASCII codes (beginners' stuff) (C)
- Save a text file in an csv.file with ascii? (PHP)
- ASCII to BINARY, & VICA VERCA (C++)
- Read and write to an ASCII Text file (Java)
Other Threads in the C++ Forum
- Previous Thread: Dll Loader/Injector
- Next Thread: ERROR_NOT_SUPPORTED in BiDiSpl.h using IBiDiSpl Interface
Views: 1075 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





