English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i cant figure out the code to take an inputed character and write a code to give the corresponding character after it in the ascii chart.
i tried saying ch + 1 but that gave me 66 and i need it to say b heh heh

2006-09-30 18:08:23 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

if im stating outfile << ch + 1 < how would i make that in the end state the chararcter i represesents rather than 66

2006-09-30 18:27:17 · update #1

3 answers

You are best off making another char variable to hold the calculation in before printing it off, even if you use the same variable 'ch'. Write one of these line before printing out
ch += 1;
or
ch++;
or
++ch;
then print out with
outfile << ch << endl;

I am assuming ch is of type char, like
char ch;
and not of type int, like
int ch;

2006-10-04 03:10:14 · answer #1 · answered by Mark aka jack573 7 · 0 0

I don't program with C++'s OOP libraries, as well as C's standard libraries.
However, I can show you some code to help you. Consider this example:

#include
char number;
unsigned int foo;
unsigned int main(void)
{
cin >> number;
foo=(unsigned int) number;
cout << foo; // output ASCII value of 'number'
return 0;
}
The trick to print out an inputted char's ASCII equivalent is to COPY the char to another variable, and then print the variable.
Cin always inputs to a string when getting data from the keyboard. What cin does with the string depends on the data type that was passed to it.
When cin is used, the compiler looks at what data type was given to cin and selects the proper cin class. If a char or array of chars was given to cin, then cin stores the ASCII string into the variable that was passed to it.
If a char variable is specified to cin, and the user typed in, '23453', then only the ASCII value of '2' is put into the char variable. If an integer or greater variable is given to cin, then the compiler selects another version of the cin class to use. The inputted ASCII string is converted to a real number. Cout sort of works the opposite way when printing a data type.

You can modify my example to do what you're trying to do for your program........I'll leave that up to you.

2006-09-30 20:20:14 · answer #2 · answered by Balk 6 · 0 0

The compiler is right. If you print the char with "%d" it will show tou the number. if you want to see the letter it represents use "%c" eg:
printf("%c", ch);

2006-09-30 18:22:33 · answer #3 · answered by Bigi Bal 3 · 0 0

fedest.com, questions and answers