Hello, I have to make a program that takes in a 2 character & out puts their eqivalent in ASCII Decimals.
For example; input = j b , output = 106 98 (ie, j= 106 in ASCII table, b= 98)
eg 2; input = J B , output = 74 66

so as you can see, an uppercase A has a different value than a lowercase a.

What is the most efficient way to do this?

I thought the best way would be:
1st to check whether the character is uppercase or lower case ( is there a way to do that?)
2nd use a for loop to calculate the decimal of the character

#include <iostream>

using namespace std;

int main()
{
	char ch1 = 'a', ch2 = 'a';          // ch1 is used for the first letter of the first name
	// ch2 is used for the first letter of the surname
	int n1 = 97, n2 = 97, n3, n4, n5; 

	cout << "Enter your first name and surname: " << flush;
	cin >> ch1;           // read the first letter of the first name
	cin.ignore(100, ' '); // ignore the rest characters in the first name
	cin >> ch2;           // read the first letter of the surname

	for (char i = 'a'; i != ch1; i ++) {
		n1 ++;
	}
	cout << ch1 << " " << n1 << endl;

	for (char j = 'a'; j != ch2; j ++) {
		n2 ++;
	}
	cout << ch2 << " " << n2 << endl;

	return 0;                                   
}

I dont want to create another 2 variables & 2 more for loops for if the character is uppercase because I think there may be a simpler / more efficient way to do it. Do you know of one?

Recommended Answers

All 3 Replies

all you have to do is typecast the character to int cout << 'A' << " = " << (int)'A' << "\n"; or

char a = 'A';
cout << a << " = " << (int)a << "\n";

all you have to do is typecast the character to int cout << 'A' << " = " << (int)'A' << "\n"; or

char a = 'A';
cout << a << " = " << (int)a << "\n";

I am new to programming, what does this code actually do? Where would I put it?

>>I am new to programming, what does this code actually do? Where would I put it?
You are trying too hard. It's a lot simpler than what you posted.

Every character has an ascii value associated with it. See an ascii chart for the values. Internally, a C or C++ program does not know a thing about the letters 'A', 'B', etc. only numeric ascii values shown in that chart. When you type 'A' with your keyboard you are really seeing the ascii numeric value converted to a graphic that is displayed in the font you have selected. That's why an 'A' may look differently with each font you choose. Behinds the scens an 'A' is 65 regardless of the font.

You will understand this better if you write a short program that displays all the characters of the alphabet.

#include <iostream>
using namespace std;

int main()
{
    for(char i = 'A'; i < 'Z'; i++)
    {
        cout << i << " = " << (int)i << "\n";
    }
    for(char i = 'a'; i < 'z'; i++)
    {
        cout << i << " = " << (int)i << "\n";
    }
}

For the purpose of your assignment, just prompt for two characters then display their ascii values as illustrated in the above program. It's really no more difficult than that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.