need help with ASCII

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2008
Posts: 12
Reputation: NervousWreck is an unknown quantity at this point 
Solved Threads: 0
NervousWreck NervousWreck is offline Offline
Newbie Poster

need help with ASCII

 
0
  #1
Jun 23rd, 2008
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:

  1. // morechar.cpp -- char and int types contrasted
  2. #include <iostream>
  3. int main()
  4. {
  5. using namespace std;
  6. char ch; //declare a variable ch
  7. int i = ch; // stores same code in an int
  8.  
  9. cout << "Enter any ASCII character:\n";
  10. cin >> ch;
  11. cout << " The ASCII code for " << ch << " is " << i << endl;
  12.  
  13. cout << "Adding one to the character code takes you to the next character.\n";
  14. ch = ch + 1; //change the character code in ch
  15. i = ch;
  16. cout << "The ASCII code for " << ch << " is " << i << endl;
  17.  
  18. //using the cout.put member function to display a char
  19. cout << "Displaying char ch using cout.put(ch): ";
  20. cout.put(ch);
  21.  
  22. //using cout.put() to display a char constant
  23. cout.put('!');
  24.  
  25. cout << endl << "done." << endl;
  26. return 0;
  27. }

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: need help with ASCII

 
0
  #2
Jun 23rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 12
Reputation: NervousWreck is an unknown quantity at this point 
Solved Threads: 0
NervousWreck NervousWreck is offline Offline
Newbie Poster

Re: need help with ASCII

 
0
  #3
Jun 24th, 2008
thanks but I still don't get why ch isn't defined by the input or why undefined gets me -52
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 980
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 210
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: need help with ASCII

 
0
  #4
Jun 24th, 2008
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;
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: need help with ASCII

 
0
  #5
Jun 24th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 12
Reputation: NervousWreck is an unknown quantity at this point 
Solved Threads: 0
NervousWreck NervousWreck is offline Offline
Newbie Poster

Re: need help with ASCII

 
0
  #6
Jun 24th, 2008
Thanks people. I had something along the lines of mitrmkar's thing before I checked, but I still don't get the -52 stuff
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 980
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 210
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: need help with ASCII

 
0
  #7
Jun 25th, 2008
Originally Posted by NervousWreck View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1075 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC