C++ Loops - Flow bug? -
in programming class, have been tasked writing program convert word or phrase phone number, evaluating each character , translating corresponding number.
here's code far:
#include <iostream> #include <string> using namespace std; int main() { char letter; int noofletters; char response; cout << "enter y/y convert telephone number " << "form letters digits.\n" << "enter other letter terminate program: "; cin >> response; cout << endl; while (response == 'y' || response == 'y') { cout << "enter telephone number using letters: "; cin >> letter; noofletters = 0; cout << "the corresponding telephone number is: "; while (noofletters != 7) { //cout << "[" << noofletters << "]"; noofletters++; switch (toupper(letter)) { case 'a': case 'b': case 'c': cout << 2; break; case 'd': case 'e': case 'f': cout << 3; break; case 'g': case 'h': case 'i': cout << 4; break; case 'j': case 'k': case 'l': cout << 5; break; case 'm': case 'n': case 'o': cout << 6; break; case 'p': case 'q': case 'r': case 's': cout << 7; break; case 't': case 'u': case 'v': cout << 8; break; case 'w': case 'x': case 'y': case 'z': cout << 9; break; default: cout << "[invalid]"; } if (noofletters == 3) { cout << '-'; } else if (noofletters > 7) { cin.ignore(); } cin >> letter; //noofletters++; } cout << endl; cin.ignore(100, '\n'); cout << "\nto process telephone number, enter y/y \n" << "enter other letter terminate program: "; cin >> response; } }
everything works fine, provided type more 7 characters. issue is, if type 7 characters, messes up.
say type "honk honk" (which measures 8 characters excluding spaces) , run through program. output follows:
enter y/y convert telephone number form letters digits. enter other letter terminate program: y
enter telephone number using letters: honk honk corresponding telephone number is: 466-5466
to process telephone number, enter y/y enter other letter terminate program: n press key continue . . .
so, works fine. if input "seventy" (exactly 7 characters), program forces me enter character (excluding spaces , newlines) before continuing, so:
enter y/y convert telephone number form letters digits. enter other letter terminate program: y
enter telephone number using letters: seventy corresponding telephone number is: 738-3689 h
to process telephone number, enter y/y enter other letter terminate program: n press key continue . . .
i'm wanting modify program able enter 7-character phrase program , have run correctly without errors, such when enter measuring 8 characters or more.
i have tried can do, no luck. make suggestion me fix bug?
i applied following patch (with aim minimal):
*************** *** 83,91 **** { cout << '-'; } ! else if (noofletters > 7) { ! cin.ignore(); } cin >> letter; --- 83,91 ---- { cout << '-'; } ! else if (noofletters == 7) { ! break; } cin >> letter;
what perhaps not realizing input buffered: until press enter program not see anything. hangs on first cin >> letter
, outside loop, until press enter. subsequent cin >> letter
in loop read buffer, therefore contains newline last character.
the way read buffer, skipping whitespace (the default), never see newline.
after loop have cin.ignore(100, '\n');
ignores (remaining) uptil , including newline (note function not skipping whitespace: 100 including whitespace characters). so, simplest thing leave loop processed 7 characters.
with original code, when type precisely 7 characters, read , processed @ point noofletters == 7, not > 7.. try read 1 more character hangs because remaining new-line skipped whitespace , there nothing else read yet: have type @ least 1 non-white space character (to processed) followed enter key in order return cin >> letter;
@ all.
Comments
Post a Comment