如何存储某些内容/名称,然后再次显示

How do I store certain content/names, and then bring it up again?

本文关键字:名称 然后 显示 何存储 存储      更新时间:2024-09-23

我刚刚开始使用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);