Printing ASCII Value
in CS 102 / Coding Challenges on C++
Variations
- A solution entirely in
main
- A solution partially in
main
- A solution modularized outside of
main
Variation 1
#include <iostream>
using namespace std;
int main(){
char c;
cout << "Enter a symbol: ";
cin >> c;
cout << "The ASCII value is " << (int)c;
return 0;
}
SAMPLE OUTPUT
> Enter a symbol: R
> The ASCII value is 82
Variation 2
#include <iostream>
using namespace std;
int getASCII(char sym); // just returns the ASCII value
int main(){
char c;
cout << "Enter a symbol: ";
cin >> c;
cout << "The ASCII value is " << getASCII(c);
return 0;
}
int getASCII(char sym) {
return (int)sym;
}
SAMPLE OUTPUT
> Enter a symbol: R
> The ASCII value is 82
Variation 3
#include <iostream>
using namespace std;
char enterSymbol(); // handles input of a symbol
int getASCII(char sym); // just returns the ASCII value
int main(){
cout << "The ASCII value is " << getASCII(enterSymbol());
return 0;
}
char enterSymbol() {
char c;
cout << "Enter a symbol: ";
cin >> c;
return c;
}
int getASCII(char sym) {
return (int)sym;
}
SAMPLE OUTPUT
> Enter a symbol: R
> The ASCII value is 82