new here, trying to figure out how to repeat my program. I need to understand how to insert a loop, i think a "do while" loop will work for this, but I am unsure because I have tried a few places of insertion and cannot get it to work right.
So my program is a telephone program, I am sure everyone here has done this in school, I am learning to do this and this is the part that I am confused on. My code is below.
I just need to make it possible for the user to keep entering phone numbers, over and over again.
I feel like I should be inserting a "do" before line14 "for (counter = 0... Then insert the "while" portion at line 94 between the brackets. For some reason, that doesn't work for me and I am now stumped.
NOTE This is an assignment for school, so please explain to me rather than just show me. Thanks for everyones help.
#include <iostream> using namespace std; int main() { int counter; char phoneNumber; cout << "\nEnter a phone number in letters only." << endl; for (counter = 0; counter < 7; counter++) { cin >> phoneNumber; if (counter == 3) cout << "-"; if (phoneNumber >= 'A' && phoneNumber <= 'Z' || phoneNumber >= 'a' && phoneNumber <= 'z') switch (phoneNumber) { case 'A': case 'a': case 'B': case 'b': case 'C': case 'c': cout << 2; // keypad starts with 2 for letters ABC, abc break; case 'D': case 'd': case 'E': case 'e': case 'F': case 'f': cout << 3; //for letter DEF, def break; case 'G': case 'g': case 'H': case 'h': case 'I': case 'i': cout << 4; //for letters GHI, ghi break; case 'J': case 'j': case 'K': case 'k': case 'L': case 'l': cout << 5; //for letter JKL, jkl break; case 'M': case 'm': case 'N': case 'n': case 'O': case 'o': cout << 6; //for letters MNO, mno break; case 'P': case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case 's': cout << 7; //for letters PQRS, pqrs break; case 'T': case 't': case 'U': case 'u': case 'V': case 'v': cout << 8; //for letters TUV, tuv break; case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y': case 'Z': case 'z': cout << 9; //for letters WXYZ, wxyz break; } } return 0; }
phoneNumberto lower or upper case before theifand theswitchwill reduce a lot of the work you have to do testing both cases. There is also a library function to tell if a character is a letter. This is much safer because nothing guarantees that the character set will be organized in any sane fashion and there are no surprises inserted between'a'and'z'.