I need help with a ASCII value program

I want to write a program that will ask the user for two characters; symbol1, and symbol2. The program will then print out which character is the larger of the two, with the ASCII value in parenthesis next to the symbol.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//char_compare.cpp


#include <iostream.h>
#include <iomanip.h>

           
int main()
           
   
{ 
   
int choice;
char symbol1, symbol2;
   
choice=1;
while (choice==1)
{
cout<<"\nEnter in the first character: ";
cin>>symbol1;
cout<<"\nEnter in the second character: ";
cin>>symbol2;
}


Where do I go from here? I want the ASCII values and then to find which one is larger, I just don't know how to necessarily do that..
Last edited on
You can just treat them like any other integers and compare them:
1
2
3
4
if (symbol1 > symbol2)
    // 'symbol1' is larger
else
    // 'symbol2' is larger (or they're the same character) 

To get the actual numerical value of the character, cast it to an int, e.g. static_cast<int>(symbol1).
To get the actual numerical value of the character, cast it to an int, e.g. static_cast<int>(symbol1).


Where would I put this line? Would it be included in the cout?
Last edited on
Topic archived. No new replies allowed.