[c++] getting the ASCII value of a char

H

Haas

Hello folks,

I just started a C++ course. An exercise is to give the ASCII value of
a character read from keyboard. I place the input from the keyboard in
a char-variable. How can i get the ASCII value of this char? Is there
a built-in function? Or should i check the full table?

Thanks.
Jasper
 
W

wittempj

This is it...
-#include <iostream>
-
-int main()
-{
- char a;
- std::cin >> a;
- std::cout << a << " " << int(a) << std::endl;-

- return 0;
-}
 
R

Rolf Magnus

Haas said:
Hello folks,

I just started a C++ course. An exercise is to give the ASCII value of
a character read from keyboard.

I assume your implementation uses ASCII or something based on that as a
character set? C++ doesn't define the character set to be used.
I place the input from the keyboard in a char-variable. How can i get the
ASCII value of this char?

It already has that value. char is nothing else than a small integer.
Is there a built-in function?

No such function needed.
Or should i check the full table?

Not needed if your system uses an ASCII character set.
 
I

Ioannis Vranos

Haas said:
Hello folks,

I just started a C++ course. An exercise is to give the ASCII value of
a character read from keyboard. I place the input from the keyboard in
a char-variable. How can i get the ASCII value of this char? Is there
a built-in function? Or should i check the full table?


char c;

int i= c;
 
J

Jonathan Arnold

Ioannis said:
char c= 'a';

int i= c;

You don't need to do this, unless there is an overwhelming
desire to have the value as a signed int. char is merely
a (usually) smaller int.
 
I

Ioannis Vranos

Jonathan said:
You don't need to do this, unless there is an overwhelming
desire to have the value as a signed int. char is merely
a (usually) smaller int.


This is a nice way with which a newcomer in C++ can "get the ASCII value of a character".
I think it is better than be told to cast the char to int when passed to cout.
 
Joined
Oct 26, 2011
Messages
2
Reaction score
0
#include<iostream.h>
main()
{
int a='b' ; // U may write any character in place of 'b', i.e.'C','H','s',etc.
cout<<a;
}
??try this....
 
Joined
Oct 26, 2011
Messages
2
Reaction score
0
#include<iostream.h>
main()
{
int a='b' ; // U may write any character in place of 'b', i.e.'C','H','s',etc.
cout<<a;
}
//??try this....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top