0

Please help me figure this out. The issue is explained in the description. Thank you.

The problem I'm having is if you enter more than one letter it will output the conversion for the first letter you enter. I want to limit the input to one letter. I want the else statement at the end to be the output if you enter more than one letter. Please help! P.s. If any of you know a simpler way without so much writing to create the same thing I did here please let me know, I would like to get better at this stuff. Thank you. If you want to try the program out or try out possible fixes you can find it in the code playground on the "Most Recent" tab. It is named "Converts letters to decimal, binary & hex" int main() { char letter; cout <<"Please enter a single letter\n"<<endl; cin>>letter; cout<<"You entered "<<letter<<endl<<endl; cout<<"The conversions for "<<letter<<" are:"<<endl; if(letter == 'a' || letter == 'A'){ cout<<"Decimal=1 \n"<<"Binary=00001 \n"<<"Hexadecimal=0x01\n\n"<<"ASCII:\n"<<"For A\n"<<"Decimal=65 \n"<<"Binary=1000001 \n"<<"Hexadecimal=0x41\n\n"<<"For a \n"<<"Decimal=97 \n"<<"Binary=1100001 \n"<<"Hexadecimal=0x61"; } else if(letter == 'b' || letter == 'B'){ cout<<"Decimal=2 \n"<<"Binary=00010 \n"<<"Hexadecimal=0x02\n\n"<<"ASCII:\n"<<"For B\n"<<"Decimal=66 \n"<<"Binary=1000010 \n"<<"Hexadecimal=0x42\n\n"<<"For b \n"<<"Decimal=98 \n"<<"Binary=1100010 \n"<<"Hexadecimal=0x62"; /*The same thing is repeated for letters c through z*/ else cout<<"Error: You did not enter a letter or you entered too many \n"<<"Please enter a single letter"<<endl; return 0; }

13th Sep 2016, 1:17 AM
David
3 Respuestas
+ 2
RED ALERT: MASSIVE CODE REPETITION DETECTED INITIATING FACTORISATION PROTOCOL... DONE http://code.sololearn.com/ccs77R0WovFW #include <iostream> #include <cctype> #include <bitset> #include <iomanip> using namespace std; void printNum(int n) { cout << "Decimal = " << n << endl; cout << "Binary = 0b" << bitset<5>(n) << endl; cout << "Hexadecimal = 0x" << setfill('0') << setw(2) << hex << n << endl; } int main() { char letter, lletter; lletter = 0; while ((lletter < 'a')||(lletter > 'z')) { cout << "Please enter a single letter: "; cin >> letter; cout << letter << endl; lletter = tolower(letter); if ((lletter < 'a')||(lletter > 'z')) { cout << "Error: You must enter a letter." << endl; } } printNum(lletter-'a'+1); cout << "ASCII: " << endl; printNum(int(letter)); */return 0; }
13th Sep 2016, 10:04 AM
Zen
Zen - avatar
0
Instead of char you can use string and then just check the length :-) string word; cin >> word; Then you can check the length and use if statement. To get first letter in word use wort.at(0)
13th Sep 2016, 8:57 AM
MarcinZx
0
Thank you Marcin and Zen for reaponding. I am very new at this which is why I dont know some of these work arounds. I tried the ine from your link Zen and when I hit run and entered a letter it showed an error. And Marcin I may be misunderstanding you, the program already gives the answer for the first letter in a word. I was wanting to make it to where it will only give you an answer if you enter one letter, and if you enter more than one letter it will give you the error message. Again thank you both for responding this is the best way for me to learn.
13th Sep 2016, 1:42 PM
David