Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
C++ Program: A Conversion Glitch?
I'm a beginner at C++
Short version of my problem:
Why would cout << tolower(user_input) result in a three-digit numeric value being output to the screen when user_input has been declared/defined as char user_input = 0; ?
Long version of my problem:
I had an assignment to create a program that prompts the user for a letter, determines whether the letter is a vowel or a consonant, determines whether it is uppercase or lowercase, and outputs the lowercase form as well as the binary value of the user's input. So I coded the program, but I had a little snafu with the second to the last section of it, the section titled "OUTPUT THE LOWERCASE VALUE OF THE LETTER IN ITS EXPLICIT FORM"
#include <iostream>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::isalpha;
using std::islower;
using std::tolower;
int main() {
// EXPLAIN THE PROGRAM TO THE USER
cout << "\n\tThis program solicits a letter from you, determines whether the letter is"
<< "\n\ta vowel or a consonant, determines whether it is lowercase or uppercase,"
<< "\n\toutputs thelowercase form of the letter, and outputs the binary value of
<< "\n\tthe letter."
<< endl;
// DECLARE/DEFINE VARIABLE IN WHICH TO STORE USER'S INPUT
char user_input = 0;
// SOLICIT USER'S INPUT
cout << "\n\tPlease enter a letter:\t";
cin >> user_input;
// DETERMINE WHETHER THE LETTER IS A VOWEL
if(isalpha(user_input)) {
switch(user_input) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
cout << "\n\tThe letter you entered is a vowel.";
}
// DETERMINE WHETHER THE LETTER IS A CONSONANT
switch(tolower(user_input)) {
case 'b': case 'c': case 'd': case 'f': case 'g': case 'h': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'p': case 'q': case 'r':
case 's': case 't': case 'v': case 'w': case 'x': case 'y': case 'z':
cout << "\n\tThe letter you entered is a consonant.";
}
// DETERMINE WHETHER THE LETTER IS IN ITS LOWERCASE FORM OR ITS UPPERCASE FORM
if(isupper(user_input)) {
cout << "\n\tYou entered the uppercase form of the letter.";
} else {
cout << "\n\tYou entered the lowercase form of the letter.";
}
// OUTPUT THE LOWERCASE VALUE OF THE LETTER IN ITS EXPLICIT FORM
if(isalpha(user_input)) {
char glitch = tolower(user_input);
cout << "\n\tThe lowercase form of the letter is:\t"
<< glitch;
}
// My original layout of the preceding section, which is where I had a problem,
// follows the 'return' statement below.
// OUTPUT THE BINARY VALUE OF THE LETTER
if(isalpha(user_input)) {
cout << "\n\tThe binary code for the letter \'" << user_input << "\' is:\t"
<< ((user_input & 0x80) ? 1 : 0) << ((use r_input & 0x40) ? 1 : 0)
<< ((user_input & 0x20) ? 1 : 0) << ((user_input & 0x10) ? 1 : 0)
<< ((user_input & 0x08) ? 1 : 0) << ((user_input & 0x04) ? 1 : 0)
<< ((user_input & 0x02) ? 1 : 0) << ((user_input & 0x01) ? 1 : 0);
}
} else {
cout << "\n\tThe character you entered is not a letter!";
}
cout << endl << endl;
return 0;
}
*/
* The original version of the problematic section was:
*
* // OUTPUT THE LOWERCASE VALUE OF THE LETTER IN ITS EXPLICIT FORM
* if(isalpha(user_input)) {
* cout << "\n\tThe lowercase form of the letter is:\t"
* << tolower(user_input);;
* }
*
* But this version kept outputting a three-digit numeric value, rather than an explicit
* alphabetic character. I resorted to creating a new variable of the data type 'char'
* and assigning to it the value of the original user-input value of 'user_input'
* because it was the only way I could get the original user-input character to appear
* on the screen.
*
* All the rest of the program did as I expected, and the only compile-time problems that
* I had were forgotten semicolons and parentheses, which I fixed in accordance with the
* compiler's error notifications.
*/
For the record, I used the text-editor Kate to code the program and I used the g++ front-end of the GCC 4.5.2 to compile it --- using a terminal (command prompt, if you prefer).
3 Answers
- peteamsLv 78 years agoFavorite Answer
The problem is to lower is a function that returns an int rather than a char, so what you are seeing is the numeric encoding of the lowercase letter.
The reason for the int return value is in the function's C heritage and the need to return EOF as well as character values.
Use a C cast (char)tolower(ch) or a C++ cast static_casr<char>(tolower(ch)) to resolve your problem.
- 5 years ago
The main issue statement tells you to use an int for hour and minute, and that is a just right inspiration. You declared jap and significant as double for some intent, however you don't want these variables in any respect. You should also ensure the enter is legitimate, and make it easier for the person to enter the time. Simply adding/subtracting 1 to/from the hour is not excellent adequate both. You have got to account for the midnight boundary. The drawback announcement additionally only says to transform from important to eastern, however then talks about the C/E timezone designation, which is complicated. Your interpretation was to enable conversion both method, and that is a good option. So, placing all of it together, your program should appear something like this: #incorporate <iostream> #incorporate <string> #incorporate <sstream> #comprise <iomanip> #comprise <cctype> utilising namespace std; const char delim = ':'; const int hoursPerDay = 24; const int minutesPerHour = 60; enum jap = 'E', imperative = 'C', NumZones = 2 Zone; bool isValidTime(int, int); bool isValidZone(char); int essential(int argc, char *argv[]) string in; stringstream ss; int hours, minutes, timeChange[] = 1, -1 , z; char c, zone, *zTxt[NumZones] = "central", "eastern" ; cout << "Time format: <HH>:<MM> <zone>" << endl; cout << " the place: HH = hours, 0 .. 23" << endl; cout << " MM = minutes, zero .. Fifty nine" << endl; cout << " zone = 'C' for important, or 'E' for japanese" << endl << endl; whilst (true) E) : "; getline(cin,in); ss.Clear(); ss.Str(in); if ((ss >> hours >> c >> minutes >> zone) && (c == delim) && (isValidTime(hours, minutes) == proper) && (isValidZone(zone) == real)) z = (toupper(zone) == japanese); cout << setw(2) << setfill('zero') << hours << ":" << setw(2) << setfill('zero') << minutes << ' ' << zTxt[z] << " = "; hours += timeChange[z]; if (hours < zero) hours += hoursPerDay; else if (hours >= hoursPerDay) hours -= hoursPerDay; cout << setw(2) << setfill('0') << hours << ":" << setw(2) << setfill('0') << minutes << ' ' << zTxt[(z + 1) % NumZones] << endl << endl; return zero; bool isValidTime(int h, int m) return (h >= zero) && (h < hoursPerDay) && (m >= zero) && (m < minutesPerHour); bool isValidZone(char z) return (toupper(z) == japanesevital); #if 0 pattern run: Time layout: <HH>:<MM> <zone> the place: HH = hours, 0 .. 23 MM = minutes, 0 .. Fifty nine zone = 'C' for valuable, or 'E' for jap Enter time (HH:MM Cimperative = thirteen:30 easternE) : 23 : 20c 23:20 critical = 00:20 japE) : 00 : 15 E 00:15 eastern = 23:15 primary Enter time (HH:MM Cninety nine.Ninety nineE) : 9:20 C 09:20 significant = 10:20 jap Enter time (HH:MM C13E) : thirteenE) : 13 : 20 E thirteen:20 eastern = 12:20 imperative #endif