C ++如何按字输入并计算我输入了多少字

C++ how to input by word and count how much word i input

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

#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

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

尝试这样的事情:

#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

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