如何存储某些内容/名称,然后再次显示
How do I store certain content/names, and then bring it up again?
我刚刚开始使用c++,我正在练习一些基本的代码。我要求用户输入他们的名字,这样我就可以在重复他们的名字时迎接他们。我该如何写那一行?到目前为止,我拥有的是:
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter your name: " << endl;
如果你不想保存你的名字,你需要创建一个变量,然后从输入中为这个变量设置值:
int main()
{
cout << "Please enter your name: " << endl;
string name; // you should use string type to save text
cin >> name; // getting name from user's input
cout << "Hello, " << name << endl; // Now you can use your name
}
程序已经完成,但您也可以进一步并生成:
人员结构数据
你想要一个如何制作的例子吗?
#include <iostream>
#include <string.h>
struct People { // Making a struct
char name[20]; // to save the name
float height; // to save the height of the person
} person; //Obj name (the name of the struct user)
using namespace std;
int main()
{
// You have to name your structure to use it in the main
cout << "Hi enter your name: " << endl;
cin >> person.name; // You are getting name from user's input
cout << "Hello, " << person.name ; // The name is saved and reused
cout << ", How tall are you? n"; // You can get also decimals numbers
cin >> person.height; // Gettin' the height
cout <<"nnSo you are "<<person.name<<" and "<<person.height<<" tall!"; //resuming
}
结构是什么
根据维基百科,
C编程语言中的结构是一种复合数据类型(或record(声明,定义的物理分组列表内存块中一个名称下的变量,允许要通过单个指针或结构访问的变量声明的名称,返回相同的地址。结构数据类型可以包含其他数据类型,因此用于混合数据类型记录,例如作为硬盘驱动器目录条目或其他混合类型的记录。
举个例子:
struct [structure tag] { struct Books {
member definition; char title[50];
member definition; char author[50];
... ...
member definition; char subject[100];
} [one or more structure variables]; } one_book;
如果这个答案让你满意,然后标记为已解决。
如果您想将整个名称转换为一个可以使用的字符串,
#include <string>
string name;
getline(cin, name);
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 预处理器:插入结构名称中的前一个行号
- 基类中的函数名称解析
- 模板元程序查找相似的连续类型名称
- 没有名称的C++模板参数
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- C++中名称篡改的目的是什么
- 如何仅使用对象名称打印特定于对象的成员
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- boost::asio如何生成多个协同程序,然后加入它们
- 为什么要在结构中放置枚举,然后使用 typedef 名称?
- 为什么我不能定义一元运算符,然后在 MSVC 的模板类中声明具有相同名称的友元二进制运算符?
- 读取中的链接列表,然后按名称进行排序
- 是否可以在正则表达式中命名子图案,然后通过C 中的子图案名称提取匹配
- 在两个不同的头中声明相同的名称空间,然后将它们包含在一个cpp中
- 对输入'age'名称进行排序,然后显示结果
- 如何在头文件中声明名称空间别名,然后在源文件中使用它
- 将名称转换为数字,然后生成输出