如何使此"while"工作?
How can I make this "while" work?
我正在阅读Bjarne Stroustrup的《编程原理和使用C Plus Plus的实践》,我读到第66页。
我正在自己尝试一些事情,我在Visual Studio上写了这个:
#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 Mccartney"时,返回:
Lennon: plays guitar!
Mccartney: plays bass!
但是我得到的是:
:plays guitar!
Lennon: plays bass!
:plays guitar!
Mccartney: plays bass!
我们都知道麦卡特尼弹奏贝斯。为什么我得到这个?请问我怎样才能修好它?
谢谢。
你没有得到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";
}
}
相关文章:
- 当输入在带有空格的单行中给出时,cin 如何在 while 循环中工作?
- 为什么我的 while 循环对于特定输入中断,而对于其他输入工作正常?
- 如何使用while循环找到GCD?模数运算符是如何工作的
- While循环和if/else语句工作不正常
- 这个代码片段中的while循环是如何工作的
- 我在while循环中遇到问题,我只是无法弄清楚为什么它无法按预期工作
- C++ if(cin>>input) 在 while 循环中无法正常工作
- 在这种情况下,while 循环如何工作
- 如何使 getline 命令在 while 循环中工作
- 试图让while循环工作,当输入符号而不是字符时会中断
- C++ while(getline()) 无法正常工作
- C ++:在我的代码中执行 while 循环无法正常工作
- do while循环工作不正常
- 我在不使用string.h的情况下写了一个C++回文.在没有 Do-While 循环的情况下,它可以正常工作.但是当我尝
- c++中的文件读取:for和while嵌套循环没有按预期工作:串行
- 这个C++while循环是如何工作的
- C++While循环工作
- CIN in while 循环无法正常工作 (C++)
- 有人可以用例子解释递归如何与 while 循环一起工作吗?我无法理解流程
- 在 do{}while 语句中使用的开关大小写会导致 GoAgain 无法正常工作