0

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;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Converting `phoneNumber` [to lower](https://en.cppreference.com/w/cpp/string/byte/tolower) or [upper](https://en.cppreference.com/w/cpp/string/byte/toupper) case before the `if` and the `switch` will 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](https://en.cppreference.com/w/cpp/string/byte/isalpha). 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'`. – user4581301 Dec 07 '18 at 04:25
  • Some things I would do differently: Read into a string stream. Convert to lower/upper case to avoid comparing against both character sets. Iterate the length of the actual user input, not assuming phone numbers are a certain length. Consider reading https://github.com/googlei18n/libphonenumber/blob/master/FALSEHOODS.md as well to understand edge cases. For example, what if I used Japanese or Arabic letters, how would you map those? – Claus Jørgensen Dec 07 '18 at 04:27

4 Answers4

1

As already said by pb772 an infinite loop of type

do { //Stuff you'd like to do } while(1);

would be fine, especially since it's a school assignment, but not ideal as always stated by pb772. I've seen advises to cycle a finite number of times and then exit but I would instead do something like a special character like '#' or '!' that will trigger a condition to exit the loop. I would see this like an exit/escape character. In the end is up to you, if you want you can do anything and what I'm proposing is just an idea to inspire you. For example another idea would be, if you'd like to go deeper, wait for another input to define what action to perform, you trigger the "command console" with '!' and then type 'q' to exit or also read the characters into a string at first so you could do complex "commands" like "!q".

Here's the simple version:

bool loop_condition = true;
do
{
   if(input == '!')
   {
      loop_condition = false;
   }
   else
   {
      //Stuff you'd like to do if the read character is not !
   }while(loop_condition == true);

Just to provide context here is what's happening:

  1. I declare a variable named loop_condition
  2. Inside the loop I check if the typed character is !
  3. If so set the variable loop_condition to false with subsequent exit from the loop
  4. Else just execute your code and loop

As I already said this is a very simple example just to give you an idea and can be improved a lot.

0

I suggest wrapping the for (counter=0... loop with a while (!cin.eof()) { block. This will allow the user to continue to enter in characters, until an EOF character (e.g. ctrl-D).

You may find you want to output a newline after every 7th character, to make the display look nice.

levis501
  • 4,117
  • 23
  • 25
  • 2
    https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong?noredirect=1&lq=1 – Mat Dec 07 '18 at 04:29
0
do {
    //your code here;
} while (1);

This will repeatly infinitely which is not a good practice.

int number_of_phones = 10; // total number of phones you want
int i = 0;
do {
    //your code here;
    i=i+1;
} while (i<number_of_phones);

This will make it run 10 times for example

pb772
  • 539
  • 1
  • 3
  • 14
0

You can have whatever condition you want in a for loop, including nothing at all, which is treated as true.

for(;;) {
    // code
}

is the same as

while (true) {
    // code
}

is the same as

do {
    // code
} while (true)

It sounds like you mixed up the placement of braces when you tried do { ... } while (true). You may want to move your big switch into a function, so that it's more obvious what scope a partiular } ends.

#include <iostream> 

int phone_key(char key)
{
    switch (key)
    {
    case 'A':
    case 'a':
    case 'B':
    case 'b':
    case 'C':
    case 'c':
        return 2;
    case 'D':
    case 'd':
    case 'E':
    case 'e':
    case 'F':
    case 'f':
        return 3; 
    case 'G':
    case 'g':
    case 'H':
    case 'h':
    case 'I':
    case 'i':
        return 4; 
    case 'J':
    case 'j':
    case 'K':
    case 'k':
    case 'L':
    case 'l':
        return 5; 
    case 'M':
    case 'm':
    case 'N':
    case 'n':
    case 'O':
    case 'o':
        return 6; 
    case 'P':
    case 'p':
    case 'Q':
    case 'q':
    case 'R':
    case 'r':
    case 'S':
    case 's':
        return 7;
    case 'T':
    case 't':
    case 'U':
    case 'u':
    case 'V':
    case 'v':
        return 8; 
    case 'W':
    case 'w':
    case 'X':
    case 'x':
    case 'Y':
    case 'y':
    case 'Z':
    case 'z':
        return 9; 
    }

    return 0;
}

int main() {
    for (;;)
    {
        std::cout << "\nEnter a phone number in letters only." << std::endl;

        for (int counter = 0; counter < 7; counter++)
        {
            char phoneNumber;
            cin >> phoneNumber;

            if (counter == 3)
                std::cout << "-";
            std::cout << phone_key(phoneNumber);
        }
    }
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
  • This does solve my problem but presents another one. It carries any extra numbers entered as you start over and it should disregard them and just start over completely. Thanks for your help and explanation! – Brandon Ness Dec 08 '18 at 14:40