Printing ASCII Value

Example solutions for the getASCII() classroom coding exercise

Variations

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

© 2020. Some rights reserved.