首页 > 解决方案 > 如何将单词放入var

问题描述

我想设置一个向“客户”询问个人信息的程序,并且此代码需要将姓名和姓氏存储在一个包含 var 的结构中。但是在输出中它显示了一个数字。

#include <iostream>

using namespace std;

//DECLARATIONS

struct dates {
int emb_date;
int post_date;
};

struct info {
int nom;
int prenom;
};

void saisie_info() {
info identite;
cout << "Veuillez indiquer votre prenom : " << endl;
cin >> identite.prenom;
cout << "Veuiller maintenant insiquer votre nom : " << endl;
cin >> identite.nom;
cout << identite.prenom << " " << identite.nom << endl;
}

int main()
{
saisie_info();
}

如您所见: prenom 意思是姓氏,而 nom 意思是名字(我是法国人)这不起作用....为什么?我准确地说我没有其他选择来使用'''struct'''来存储“nom”和“prenom”。谢谢你的帮助

标签: c++structcincoutword

解决方案


您必须更改nomprenom中的数据类型struct info。类型int代表数字。字符串(或单词)std::string在 C++ 中表示为:

struct info {
    std::string nom;
    std::string prenom;
};

如果我对您的代码进行更改并输入我的姓名,我会在输出中看到它。


推荐阅读