C ++代码cout打印而不是等待它在字符串中使用

时间:2017-02-21 19:43:43

标签: c++

/*
 * hello_world.cpp
 *
 *  Created on: Feb 21, 2017
 *      Author: George Lutas
 */

#include <iostream> 
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

inline void keep_window_open() { char ch; cin>>ch; }

int main()      //C++ programs start by executing the function main
{
    cout << "George 17\n";
    string first_name = "George";
    int age = 17;
    cin >> first_name >> age;
    cout << "Hello," << first_name << "(age" << age << ")" << endl;
}

这是我的代码。我在这里想念的是什么代码的目标是输出&#34; Hello,George(17岁)&#34;。所以,我怎样才能让它读到而不是#34;乔治17&#34;?另外,我知道我没有安装std_lib_facilities.h。这是故意的。我非常确定(因为没有出现错误)我有我需要加载的库。

1 个答案:

答案 0 :(得分:3)

让我们分析您的main功能:

cout<<"George 17\n";

此行打印"George 17"并切换到新行。

string first_name="George";
int age=17;

这定义了一个初始化为first_name的字符串变量"George"和一个初始化为age的整数变量17

cin>>first_name>>age;

这将读取stringint类型的输入(按此顺序)并将其保存到first_nameage。它不会提示您输入。 (您必须使用cout来实现它。)它只是等待您提供输入。这可能看起来像程序已经完成。但事实并非如此。

cout<<"Hello,"<<first_name<<"(age"<<age<<")\n";

此行最终将打印您的预期输出。