简体   繁体   中英

How to stop a loop by using a word such as "exit" in C++?

So I have a coding assignment to complete, and I am having trouble finishing it. The exact instructions are as follows:

To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed as CALL HOME, which uses eight letters.

Write a program that does the following:

  • Prompts a user to enter Y or y to begin conversion, or any other input to quit.
  • Prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits
  • Processes only the first seven letters if the user enters more than seven letters.
  • Outputs the – (hyphen) after the third digit.
  • Allows the user to use both uppercase and lowercase letters as well as spaces between words.
  • Process as many telephone numbers as the user wants while allowing them to quit after each conversion.

I have currently completed all of the steps except for the last one (kind of).

The instructor is looking for the word "exit" to quit the program. Currently, I have it set up for "%" to end the program. Logically you would say to just change the "%" to "exit" and move on, but I get an error when I do this.

#include <iostream>
using namespace std;

int main()
{        
    int counter;        
    char phoneNumber;
    char cont;
    
    //  Prompts a user to enter Y or y to begin conversion, or any other input to quit. 
    cout << "Please enter 'Y' or 'y' to continue, otherwise the program with quit.\n Input: ";
    cin >> cont;
    if (cont == 'y' || cont == 'Y')
    {            
        // statement(s) will execute if the boolean expression is true
    }
    else
    {
        return 0;
        // statement(s) will execute if the boolean expression is false
    }
    
    cout << "To stop this program enter 'exit'." << endl;
    cout << "Enter a phone number in letters only \nNOTE: Must enter 7 or more letters: ";
    cin >> phoneNumber;
    cout << endl;
    phoneNumber = static_cast<char>(toupper(phoneNumber));
    while (phoneNumber != '%')
    {
        cout << "\nTo stop this program enter 'exit'." << endl;
        cout << "Enter a phone number in letters only." << endl;
        
        for (counter = 0; phoneNumber != '%' && 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;
                        break;
                    case 'D':
                    case 'd':
                    case 'E':
                    case 'e':
                    case 'F':
                    case 'f':
                        cout << 3;
                        break;
                    case 'G':
                    case 'g':
                    case 'H':
                    case 'h':
                    case 'I':
                    case 'i':
                        cout << 4;
                        break;
                        
                    case 'J':
                    case 'j':
                    case 'K':
                    case 'k':
                    case 'L':
                    case 'l':
                        cout << 5;
                        break;
                    case 'M':
                    case 'm':
                    case 'N':
                    case 'n':
                    case 'O':
                    case 'o':
                        cout << 6;
                        break;
                    case 'P':
                    case 'p':
                    case 'Q':
                    case 'q':
                    case 'R':
                    case 'r':
                    case 'S':
                    case 's':
                        cout << 7;
                        break;
                        
                    case 'T':
                    case 't':
                    case 'U':
                    case 'u':
                    case 'V':
                    case 'v':
                        cout << 8;
                        break;
                        
                    case 'W':
                    case 'w':
                    case 'X':
                    case 'x':
                    case 'Y':
                    case 'y':
                    case 'Z':
                    case 'z':
                        cout << 9;
                        break;
                }
        }
        while (cin.get() != '\n')
            ;
    }
    return 0;
}

I think it has something to do with the char, and that's all I could find out. I have been searching the internet for an answer, but I came up empty. Any help you could provide would be greatly appreciated.

如果您想将 phonenumber 变量更改为“退出”,它需要是一个字符串,而不是一个字符。

In C++, there is a big difference between a char and a string. A char is a single character while a string is a bunch of characters. For strings, a bunch can mean 0, 1 or more characters. The twos are differentiated by the use of apostrophe (for char) or quotation marks (for string). Since your program only uses char as input, when you get an input longer than one character, your program doesn't know how to handle this. You will have to make use of strings to include the case where you have to write "exit".

The basic structure of your program should be:

int main() {
    std::string line;

    while (std::getline(std::cin, line) && line != "exit") {
        std::cout << phone_digits(line) << "\n";
    }
}

That's almost as simple as it gets. The "exit" condition is not strictly necessary since there is a widespread convention about how to signal the end of the input. On Windows, it's Ctrl+Z, while on UNIX-like systems (Linux, NetBSD, macOS) it's Ctrl+D.

I also don't understand why you first have enter 'y' to actually start the program. That's unnecessary too, but your teacher will probably insist on that nevertheless.

The next thing is to write the phone_digits function and place it above the main function. This function takes a string as the argument and also returns a string.

std::string phone_digits(const std::string &input) {
    std::string result;

    // TODO: convert letters from input to digits in output
    // TODO: insert the hyphen in the result

    return result;
}

To write the function in this form, you have to read the documentation of the std::string class, to find out how to loop over all characters from the input and how to add characters to the end of the result string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM