我怎样才能做到这一点"而#34;工作?

时间:2016-09-13 16:49:20

标签: c++ while-loop

我正在阅读"编程原则与实践使用C Plus Plus",Bjarne Stroustrup和我在第66页。

我自己尝试了一些东西,我在Visual Studio上写了这个:

A

我想要它做的是:当我写下#Len; Mccartney"在提示符下,返回:

#include "../../std_lib_facilities.h";

int main()
{
    string first_name = " ";
    string last_name;
    int numbert = 0;
    while (cin >> last_name) {
        ++numbert;
        if (last_name > first_name)
            cout << first_name << ": plays guitar!\n" << last_name << " : plays bass!\n";
        }
}

但我得到的是:

Lennon: plays guitar!
Mccartney: plays bass!

我们都知道麦卡特尼演奏低音。我为什么要这个?我该如何解决呢?

感谢。

1 个答案:

答案 0 :(得分:1)

您没有获得first_name的输入,您应该使用以下代码

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

    int main()
    {
        string first_name = " ";
        string last_name;
        int numbert = 0;
        while (cin >> first_name >>last_name) {
            ++numbert;
            if (last_name > first_name)
                cout << first_name << ": plays guitar!\n" << last_name << " : plays bass!\n";
        }
    }
相关问题