Salut, je suis débutant en C++ et j’ai écrit un code simple de recherche d’informations sur un client parmi d’autres en utilisant des structures comme ci dessous:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 
#include <iostream>
#include<string>
#include<vector>
 
using namespace std;
 
struct Adresse{string rue; int numero;};
struct Client{string nom; Adresse adresseClient;};
 
 Client tableauDeClient[3];
 
int main( ) {
 
    string nomClient;
 
    tableauDeClient[0].nom = "Francis";
    tableauDeClient[0].adresseClient.rue = "Sauerbruch";
    tableauDeClient[0].adresseClient.numero = 12;
 
    tableauDeClient[1].nom = "Eric";
    tableauDeClient[1].adresseClient.rue = "Richard-Dehmel";
    tableauDeClient[1].adresseClient.numero = 9;
 
    tableauDeClient[2].nom = "Alexandre";
    tableauDeClient[2].adresseClient.rue = "Albert";
    tableauDeClient[2].adresseClient.numero = 6;
 
    cout << "Entrer votre nom :";
    cin >> nomClient;
 
    bool testDetection(false);
 
    for (int i=0; i<3; ++i){
 
        if ( nomClient == tableauDeClient[i].nom){
 
        cout <<"Nom du Client : "<< tableauDeClient[i].nom << endl;   
 
        cout <<"Adresse : "<< tableauDeClient[i].adresseClient.rue << " " << tableauDeClient[i].adresseClient.numero << endl;    
 
        testDetection = true;
        }
 
    }
 
    if (testDetection == false){
 
        cout << "Ce client n existe pas "<< endl;
 
    }
 
    return 0;
}

Le code fonctionne, mais j’aimerais remplaçer le tableau statique utilisé par un tableau dynamique afin de donner la possibilité plus tard à l’utilisateur d’ajouter des clients. Pour cela, j’ai modifié le code comme ci dessous, mais n’arrive pas à utiliser des push_back() avec ces structures pour l’ajout des trois premiers clients.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
#include <iostream>
#include<string>
#include<vector>
 
using namespace std;
 
struct Adresse{string rue; int numero;};
struct Client{string nom; Adresse adresseClient;};
 
 vector<Client> tableauDeClient;  // declare un tableau dynamique vide de clients
 
int main( ) {
 
    string nomClient;
 
// comment utiliser des push_back() ici pour ajouter ces trois clients?
    tableauDeClient[0].nom = "Francis";
    tableauDeClient[0].adresseClient.rue = "Sauerbruch";
    tableauDeClient[0].adresseClient.numero = 12;
 
    tableauDeClient[1].nom = "Eric";
    tableauDeClient[1].adresseClient.rue = "Richard-Dehmel";
    tableauDeClient[1].adresseClient.numero = 9;
 
    tableauDeClient[2].nom = "Alexandre";
    tableauDeClient[2].adresseClient.rue = "Albert";
    tableauDeClient[2].adresseClient.numero = 6;
 
    cout << "Entrer votre nom :";
    cin >> nomClient;
 
    bool testDetection(false);
 
    for (int i=0; i<tableauDeClient.size(); ++i){
 
        if ( nomClient == tableauDeClient[i].nom){
 
        cout <<"Nom du Client : "<< tableauDeClient[i].nom << endl;   
 
        cout <<"Adresse : "<< tableauDeClient[i].adresseClient.rue << " " << tableauDeClient[i].adresseClient.numero << endl;    
 
        testDetection = true;
        }
 
    }
 
    if (testDetection == false){
 
        cout << "Ce client n existe pas "<< endl;
 
    }
 
    return 0;
}
Merci d‘avance