如何通过使用诸如“退出"之类的词来停止循环.在C ++中? [英] How to stop a loop by using a word such as "exit" in C++?

查看:72
本文介绍了如何通过使用诸如“退出"之类的词来停止循环.在C ++中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个编码任务要完成,而我在完成它时遇到了麻烦.确切的说明如下:

为使电话号码更容易记住,一些公司使用字母显示其电话号码. 例如,使用字母,电话号码438-5626可以显示为GET LOAN.在某些情况下,为了使电话号码有意义,公司可能会使用七个以上的字母.例如,可以将225-5466显示为使用八个字母的CALL HOME.

编写一个执行以下操作的程序:

  • 提示用户输入Y或y开始转换,或退出任何其他输入.
  • 提示用户输入以字母表示的电话号码并以数字输出相应的电话号码
  • 如果用户输入的字母超过七个,则仅处理前七个字母.
  • 在第三位数字后输出–(连字符).
  • 允许用户同时使用大写和小写字母以及单词之间的空格.
  • 处理用户想要的电话号码,同时允许他们在每次转换后退出.

我目前已完成所有步骤,除了最后一个步骤(类似).

教师正在寻找单词"exit"以退出程序.目前,我已将其设置为%"以结束程序.从逻辑上讲,您会说只是将%"更改为"exit"并继续前进,但是这样做时会出现错误.

#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;
}

我认为它与char有关,这就是我所能找到的全部.我一直在互联网上寻找答案,但我还是空着.您能提供的任何帮助将不胜感激.

解决方案

程序的基本结构应为:

int main() {
    std::string line;

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

那几乎就变得简单了.由于存在关于如何用信号通知输入结束的广泛约定,因此退出"条件不是严格必需的.在Windows上是Ctrl + Z,而在类​​似UNIX的系统(Linux,NetBSD,macOS)上是Ctrl + D.

我也不明白您为什么首先输入"y"才能真正启动程序.这也是不必要的,但是您的老师可能仍然会坚持这样做.

接下来的事情是编写phone_digits函数并将其放置在main函数的上方 处.此函数以字符串作为参数,并返回一个字符串.

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;
}

要以这种形式编写函数,必须阅读std::string类的文档,以了解如何遍历输入中的所有字符以及如何将字符添加到结果字符串的末尾. /p>

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.

解决方案

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.

这篇关于如何通过使用诸如“退出"之类的词来停止循环.在C ++中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆