/*
* 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。这是故意的。我非常确定(因为没有出现错误)我有我需要加载的库。
答案 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;
这将读取string
和int
类型的输入(按此顺序)并将其保存到first_name
和age
。它不会提示您输入。 (您必须使用cout
来实现它。)它只是等待您提供输入。这可能看起来像程序已经完成。但事实并非如此。
cout<<"Hello,"<<first_name<<"(age"<<age<<")\n";
此行最终将打印您的预期输出。