I am so lost...
I am attempting to create a program that first asks to enter an
integer between 0 and 35. If the number is less than or equal to 9, the
program should output the number; otherwise it should output A for 10, B for
11, C for 12,...so on... and Z for 35. Then it tells me to use the cast operator, static_cast%26lt;char%26gt;(), for numbers %26gt;=10.
I am just lost period on how the static_cast function works...I have spent hours trying to find information...and figure it out!
I need help with C++ Programming. We are using Dev C++?
static_cast%26lt;int%26gt;("A") = 65
gives you the ASCII value of the character
Whole program:================================...
char array[25]={'A','B',.....'Z'}
cout %26lt;%26lt; "Enter number: ";
cin %26gt;%26gt; number
if(number %26lt;= 10){
cout %26lt;%26lt; number;
}
else
cout %26lt;%26lt; array[number- 10];
That is the basic program you need to write it will work just dress it up to look pretty
Reply:Okay.. So here's the thing.. Characters and integers are closely related. If you store the value "65" in a char you get the letter "A" if you store the value "65" in an integer you get the number 65. With that said, it should make a little more sense that you can cast an integer into a character and vise versa. Casting will work like this:
int myint = 65;
char ch = (char) myint; //casts myint to a character
cout %26lt;%26lt; "myint " %26lt;%26lt; myint %26lt;%26lt;"\n";
cout %26lt;%26lt; "myint casted to char" %26lt;%26lt; ch;
try that code in your program and see what happens.
The general form of casting is (%26lt;datatype%26gt;) %26lt;object%26gt;.
I'm a bit rusty with the C++ but hopefully this help.
garden
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment