Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

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.
 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 
NOTE: Must enter 7 or more letters: ";
    cin >> phoneNumber;
    cout << endl;
    phoneNumber = static_cast<char>(toupper(phoneNumber));
    while (phoneNumber != '%')
    {
        cout << "
To 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() != '
')
            ;
    }
    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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to change the phonenumber variable to "exit" it needs to be a string, not a char.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...