【问题标题】:How to put a word into a var如何将单词放入var
【发布时间】:2020-11-11 21:39:18
【问题描述】:

我想设置一个向“客户”询问个人信息的程序,并且此代码需要将姓名和姓氏存储在包含 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”。 感谢您的帮助

【问题讨论】:

  • “不工作”是什么意思?代码有什么作用?它应该怎么做?什么是输入、输出、预期输出?
  • 代码“正常”。我的意思是对于名为 46303 和 sursame 5818 的人来说没有问题。为什么prenomnom 是整数?
  • "他的代码需要将姓名和姓氏存储在一个包含所有变量的结构中。但在输出中它显示了一个数字。" 如果您需要存储一个名字或姓氏,你为什么要把它们读成int,它代表integer
  • 您是否尝试reading a good C++ textbook 来了解 C++ 中存在哪些变量类型以及如何使用它们?

标签: c++ struct cin cout word


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多