+ 1

write a c++ to ASCII code in given given Character.

#include<iostream> using namespace std; int main() { char ch; int ASCII; cout<<"Enter a character: "<<endl; cin>>ch; ASCII=ch; cout<<"ASCII code is: "<<ASCII<<endl;; return 0; } output Enter a character: A ASCII code is: 65

12th Jul 2016, 5:55 PM
meherDev
meherDev - avatar
12 Réponses
+ 2
@Vedant: Your example is almost perfect. Just don't use the nasty C casting operator. Using static_cast is more expressive, less error-prone and easier to read.
12th Jul 2016, 9:49 PM
Stefan
Stefan - avatar
+ 1
use conio header functions to store the entered value in an integer data type variable and then display it it will be shorter and may grab you better marks if you write it in exams
12th Jul 2016, 8:31 PM
Vedant Patadia
Vedant Patadia - avatar
+ 1
and plus there is no need to store ch into ASCII just explicitly convert it into int while writing the cout object operation
12th Jul 2016, 8:32 PM
Vedant Patadia
Vedant Patadia - avatar
+ 1
//assuming you know the basics part jumping directly to logic char ch; cout<<"Enter a character: "<<endl; cin>>ch; cout<<"ASCII code is: "<<((int)ch)<<endl;; return 0;
12th Jul 2016, 8:34 PM
Vedant Patadia
Vedant Patadia - avatar
0
nice
12th Jul 2016, 5:59 PM
Äákàsh Páñtà
Äákàsh Páñtà - avatar
0
I want to learn
12th Jul 2016, 5:59 PM
Äákàsh Páñtà
Äákàsh Páñtà - avatar
0
can you teach me
12th Jul 2016, 5:59 PM
Äákàsh Páñtà
Äákàsh Páñtà - avatar
0
thanks.. my english is not good,so I can't properly to teach you
12th Jul 2016, 6:04 PM
meherDev
meherDev - avatar
0
and I'm new user of c++..
12th Jul 2016, 6:04 PM
meherDev
meherDev - avatar
0
thank you Vadant Patadia bro
12th Jul 2016, 8:38 PM
meherDev
meherDev - avatar
0
@Stefan...the C operators are error prone only when fiddled in wrong...in here I'm just converting char to int type meaning smaller to bigger which is safe plus sometimes C operators are short and take less space in the make hence for big programs like 8k to 30k lines of code you can use them to shorten the code for less compile time
12th Jul 2016, 9:57 PM
Vedant Patadia
Vedant Patadia - avatar
0
static_cast<char>(ch);
13th Jul 2016, 5:06 AM
Rafael Marroquin
Rafael Marroquin - avatar