【问题标题】:C++ how to input by word and count how much word i inputC ++如何按单词输入并计算我输入了多少单词
【发布时间】:2021-11-25 23:51:10
【问题描述】:

我确实尝试过使用这个程序

#include <iostream>

using namespace std;

int main()
{
    string x;
    string a = "black";
    string b = "red";
    string c = "white";

    int e, f, g = 0; //e = black; f = red; h = white;

    cout << "input car color :";
    cin >> x;
    if (x == a) {
        cout << "continue input car color?" << endl;
    }
    else if (x == b) {
        cout << "continue input car color?" << endl;
    }




        return 0;
}

但我不知道最后一个显示用户输入了多少颜色

这是我的程序的结果,我该如何做?顺便说一句,它在 c++ 中

input car color: black                //black,red,white=user input
continue input car color? (y/n)? y
input car color: black
continue input car color? (y/n)? y
input car color: black
continue input car color? (y/n)? y
input car color: red
continue input car color? (y/n)? y
input car color: white
continue input car color? (y/n)? n

detail car color
black      3 car
red        1 car
white      1 car

【问题讨论】:

  • 你标记了loops。你知道循环是如何工作的吗?
  • 不是用于循环输入汽车颜色的问题> 还是 goto , while, for?
  • 不要使用goto。如果您提前知道要运行多少次迭代,请使用for 循环。当您不知道要运行多少次迭代时,请使用 while 循环。不过,在这种情况下,do..while 循环会更合适。
  • goto 很难正确。要正确并获得易于阅读和易于维护的代码更难。更难说服其他需要使用您的代码的人goto 是正确的选择。我发现使用goto 注定你必须不断地使用goto 进行防御,而且这比在没有goto 的情况下编写代码或重写代码以不使用goto 来快速吸收更多的时间。

标签: c++ loops while-loop do-while goto


【解决方案1】:

您需要使用循环来继续提示用户进行更多输入。而且您需要在检测到的每个匹配输入上增加整数。

试试这样的:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    string input;
    int num_black = 0, num_red = 0, num_white = 0;

    do
    {
        cout << "input car color :";
        cin >> input;

        if (input == "black") {
            ++num_black;
        }
        else if (input == "red") {
            ++num_red;
        }
        else if (input == "white") {
            ++num_white;
        }

        cout << "continue input car color?" << endl;
        cin >> input;
    }
    while (input == "y");

    cout << endl;
    cout << "detail car color" << endl;
    cout << setw(11) << left << "black" << num_black << " car" << (num_black != 1 ? "s" : "") << endl;
    cout << setw(11) << left << "red" << num_red << " car" << (num_red != 1 ? "s" : "") << endl;
    cout << setw(11) << left << "white" << num_white << " car" << (num_white != 1 ? "s" : "") << endl;

    return 0;
}

Online Demo

【讨论】:

    【解决方案2】:

    Below 是一个工作示例。

    #include <iostream>
    #include <iomanip>//needed for formatting
    using namespace std;
    
    int main()
    {
        string x;
        string black_string = "black";
        string red_string = "red";
        string white_string = "white";
    
        int count_black = 0, count_red =0, count_white = 0; //e = black; f = red; g = white;
    
        std::string yes_or_no;
        
        while(true)
        {
            cout << "input car color :";
            cin >> x;
        
            if (x == black_string) 
            {
                ++count_black;
                cout << "continue input car color(y/n)?";
                std::cin >> yes_or_no;
                if(yes_or_no == "n")
                {
                    break;
                }
            }
            else if (x == red_string) 
            {
                ++count_red;
                cout << "continue input car color?(y/n)";
                std::cin>>yes_or_no;
                if(yes_or_no == "n")
                {
                    break;
                }
            }
            else if (x == white_string) 
            {
                ++count_white;
                cout << "continue input car color?(y/n)" ;
                std::cin>>yes_or_no;
                if(yes_or_no == "n")
                {
                    break;
                }
            }
        }
        cout << "detail car color" << endl;
        std::cout<<std::setw(11) << left <<"black: "<<count_black<<std::endl;
        std::cout<<std::setw(11) << left <<"red: "<< count_red<<std::endl;
        std::cout<<std::setw(11) << left <<"white: "<<count_white<<std::endl;
    
    
            return 0;
    }
    
    
    

    【讨论】:

    • 像这里使用的int e = 0, f =0, g = 0; //e = black; f = red; g = white; 这样的评论是承认您的变量命名不足。更喜欢使用描述性标识符,这样代码的读者就不必去寻找解释性的 cmets。好的代码会自行生成。
    • 当然,您可以随意更改变量名称,但这不是 OP 要求的重点。我刚刚添加了所需的功能。学会理解问题中所要求的内容和不需要的内容之间的区别。
    猜你喜欢
    • 2016-08-06
    • 2017-08-14
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多