-2

I would like to set a program that ask personnal information to a "customer", and this code need to store a name and a surname in a structure contening al the var. But at the output it shows me a number.

#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();
}

As you can see : prenom mean Surname, and nom mean Name (I'm french) And that don't work.... Why ? I precise I have no other choice to use a '''struct''' to store "nom" and "prenom". Thanks for your help

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Céleste
  • 7
  • 3
  • 1
    what is the meaning of "don't work" ? What does the code do? What should it do instead? What is input, output, expected output? – 463035818_is_not_an_ai Nov 11 '20 at 21:40
  • The code is "ok". I mean for someone named 46303 with sursame 5818 there is no problem. Why is `prenom` and `nom` integers? – 463035818_is_not_an_ai Nov 11 '20 at 21:42
  • Are you familiar with `string`s? https://www.tutorialspoint.com/cplusplus/cpp_strings.htm – computercarguy Nov 11 '20 at 21:42
  • "_his code need to store a name and a surname in a structure contening al the var. But at the output it shows me a number._" If you need to store a name, or a surname, why are you reading those into an `int`, which represents `integer`? – Algirdas Preidžius Nov 11 '20 at 21:42
  • what is strange is that if you get numbers as output that means you also must have entered numbers as input... – 463035818_is_not_an_ai Nov 11 '20 at 21:43
  • @idclev463035818 My code is sensed to receive a word and to store it into a variable – Céleste Nov 11 '20 at 21:44
  • your code reads integers and prints integers. Use `std::string` if you want to read and display strings – 463035818_is_not_an_ai Nov 11 '20 at 21:45
  • @Céleste Once again, if you need to read a word, why did you declare your variables with an `int` type, instead of `std::string`? – Algirdas Preidžius Nov 11 '20 at 21:45
  • To all of you, I just don't know how to do, I only know that "nom" and "prenom" are words, and that I have to store them into a variable, what I don't know wich type of variable I have to use – Céleste Nov 11 '20 at 21:46
  • @idclev463035818 Thank You, I'll try that – Céleste Nov 11 '20 at 21:46
  • 2
    Did you try [reading a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to find out what kind of variable types exists in C++ and how they are used? – Sam Varshavchik Nov 11 '20 at 21:47
  • Oh thanks !! It works with strings, It seems so obvious I'm a bit stupid sorry. Have a nice day. – Céleste Nov 11 '20 at 21:48
  • please do not add "SOLVED" to your question. The way to indicate that your question has been answered is to accept one of the answers – 463035818_is_not_an_ai Nov 11 '20 at 21:49
  • @AlgirdasPreidžius Because I started C++ yesterday :) I don't really know actually what do what in a code. – Céleste Nov 11 '20 at 21:50
  • C++ isnt the best language to learn via guessing. Sam gave you a link to a large collection of books, you should get one – 463035818_is_not_an_ai Nov 11 '20 at 21:50
  • @idclev463035818 I take all your consel, and I will improve myself thanks to them. And yes I saw what Sam gave. But how do I "accept" an answer – Céleste Nov 11 '20 at 21:54
  • not sure anymore but this should have been part of the [tour] – 463035818_is_not_an_ai Nov 11 '20 at 21:55
  • @idclev463035818 Ew I missed that. I'll read it – Céleste Nov 11 '20 at 21:56
  • @Céleste "_Because I started C++ yesterday_" You can't learn C++ by guessing, as already explained by others. Mostly because it is easy to write code, which has undefined behavior, and seems to work by sheer coincidence, which then breaks after unrelated changes are made. By the very nature of undefined behavior - it is impossible to test (without knowing it) if some behavior is undefined. – Algirdas Preidžius Nov 11 '20 at 22:02
  • @AlgirdasPreidžius I don't only do by guessing, hopefuly. But I can't know what I don't know. That's why I ask here, so I can have a precise answer to my precise problem – Céleste Nov 11 '20 at 22:19

1 Answers1

1

You have to change the data type of nom and prenom in the struct info. The type int represents numbers. Strings (or words) are represented by std::string in C++:

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

If I make that change to your code and I enter my name, I see it in the output.

Frank
  • 1,565
  • 1
  • 10
  • 9