首页 > 编程语言 >《c++徒步》IO篇

《c++徒步》IO篇

时间:2023-05-06 14:13:19浏览次数:41  
标签:city name c++ 徒步 IO using include string cout

iostream

cin cout

参考链接:https://www.runoob.com/cplusplus/cpp-basic-input-output.html

标准输出

#include <iostream>

using namespace std;

int main( )
{
   char str[] = "Hello C++";

   cout << "Value of str is : " << str << endl;
}

输入

#include <iostream>

using namespace std;

int main( )
{
   char name[50];

   cout << "请输入您的名称: ";
   cin >> name;
   cout << "您的名称是: " << name << endl;

}

string

getline

参考链接:http://c.biancheng.net/view/1345.html

当 cin 读取数据时,它会传递并忽略任何前导白色空格字符(空格、制表符或换行符)。一旦它接触到第一个非空格字符即开始阅读,当它读取到下一个空白字符时,它将停止读取。
如下:

// This program illustrates a problem that can occur if
// cin is used to read character data into a string object.
#include <iostream>
#include <string> // Header file needed to use string objects
using namespace std;
int main()
{
	string name;
	string city;
	cout << "Please enter your name: ";
	cin >> name;
	cout << "Enter the city you live in: ";
	cin >> city;
	cout << "Hello, " << name << endl;
	cout << "You live in " << city << endl;
	return 0;
}

程序输出结果:

Please enter your name: John Doe
Enter the city you live in: Hello, John
You live in Doe

没有机会输入city

为了解决这个问题,可以使用一个叫做 getline 的 C++ 函数。此函数可读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中。

getline 函数如下所示:

// This program illustrates using the getline function
//to read character data into a string object.
#include <iostream>
#include <string> // Header file needed to use string objects
using namespace std;
int main()
{
	string name;
	string city;
	cout << "Please enter your name: ";
	getline(cin, name);
	cout << "Enter the city you live in: ";
	getline(cin, city);
	cout << "Hello, " << name << endl;
	cout << "You live in " << city << endl;
	return 0;
}

程序输出结果:

Please enter your name: John Doe
Enter the city you live in: Chicago
Hello, John Doe
You live in Chicago

标签:city,name,c++,徒步,IO,using,include,string,cout
From: https://www.cnblogs.com/fusio/p/17377086.html

相关文章

  • C++一些bug的记录
    目录表达式必须具有类类型但它具有xxx类型表达式必须具有类类型但它具有xxx类型错误一般发生在使用.进行访问时,原因可能在于:你以为你定义了一个类对象,其实你是声明了一个函数,在编译器看来;对类对象指针采用.的方式访问其成员变量;也包括基本类型变量,错误地使用.inta......
  • Struts2----中使用ValueStack、ActionContext、ServletContext、request、session等
     声明:本文参考Struts2版本为2.3.1.2,内容仅供参考,限于笔者水平有限,难免有所疏漏,望您能友善指出。本文发表于ITEYE,谢绝转载。1.ValueStack  ValueStack在中文版的《Struts2深入浅出》一书中译作“值栈”。其本身数据结构是一个栈,使用者可以把一些对象(又称作bean)存入值栈中,然后......
  • AndroidStudio插件GsonFormat快速实现JavaBean
    安装方法一:1.AndroidstudioFile->Settings..->Plugins–>Browserepositores..搜索GsonFormat2.安装插件,重启androidstudio方法二:1.下载GsonFormat.jar;2.AndroidstudioFile->Settings..->Plugins–>installpluginfromdisk..导入下载GsonFormat.jar3.重启android......
  • Could not create ActionMapper: WebWork will *not* work!
    CouldnotcreateActionMapper:WebWorkwill*not*work!解决方法:将webwork.properties的webwork.objectFactory=springwebwork.objectFactory.spring.autoWire=name 两行去掉就可以了......
  • application/x-www-form-urlencoded & multipart/form-data & text/plain
    FORM元素的enctype属性指定了表单数据向服务器提交时所采用的编码类型  我们知道在通过POST方式向服务器发送AJAX请求时最好要通过设置请求头来指定为application/x-www-form-urlencoded编码类型。知道通过表单上传文件时必须指定编码类型为"multipart/form-data"。而text/plain......
  • python中的报错:dictionary changed size during iteration
    该报错是一个python中常见的错误,通常在使用for循环迭代字典时删除其中的元素时出现。这个问题的根本原因在于迭代字典的同时修改了它,导致字典的大小发生了变化。如下面的例子,就会报错:d={"a":1,"b":2,"c':3}fork,vind.items():ifv==2:deld[k]上述......
  • 【已解决】Microsoft Visual C++ Redistributable is not installed
    【Error】导入torch,提示报错:MicrosoftVisualC++Redistributableisnotinstalled,thismayleadtotheDLLloadfailure.【Cause】Anaconda没有默认安装在C盘;系统没有安装VC++Redistributable程序。【Resolve】VC++Redistributable.exe双击安装,重启电脑即可。......
  • 回调函数(callback function)
    是什么回调函数是一种特殊的函数,它不是在程序中直接调用的,而是由程序在特定事件发生时进行调用的。回调函数通常作为参数传递给其他函数,而这些函数在执行时会将回调函数作为其内部的一部分来调用。为什么解耦.回调函数的好处在于它们可以让程序更加模块化和可扩展。怎......
  • spring-transaction源码分析(2)EnableTransactionManagement注解
    概述(Javadoc)该注解开启spring的注解驱动事务管理功能,通常标注在@Configuration类上面用于开启命令式事务管理或响应式事务管理。@Configuration@EnableTransactionManagementpublicclassAppConfig{@BeanpublicFooRepositoryfooRepository(){//c......
  • Istio数据面新模式:Ambient Mesh技术解析
    摘要:AmbientMesh以一种更符合大规模落地要求的形态出现,克服了大多数Sidecar模式的固有缺陷,让用户无需再感知网格相关组件,真正将网格下沉为基础设施。本文分享自华为云社区《华为云云原生团队:Istio数据面新模式AmbientMesh技术解析》,作者:云容器大未来。如果说在以Kubernetes......