2017-09-20 6 views
0

Ceci est mon fichier d'en-têteCout sortie ligne de charabia de message au lieu de la déclaration de Cout de l'utilisateur réel

#ifndef KINGDOM_H_ 
#define KINGDOM_H_ 

#include <string> 
using namespace std; 
namespace sict{ 
    class Kingdom { 
    public: 
     char m_name[32]; 
     int m_population; 

    }; 
    void display(Kingdom& pKingdom); 
} 
#endif 

et ce sont mes fichiers cpp

#include <iostream> 
#include <string> 
#include "kingdom.h" 

using namespace std; 

namespace sict { 

    void display(Kingdom& pKingdom) { 
     cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl; 
    } 
} 

Et voici mon dernier fichier cpp

#include <iostream> 
#include "Kingdom.h" 

using namespace std; 
using namespace sict; 

void read(sict::Kingdom&); 

int main() { 
    int count = 0; // the number of kingdoms in the array 
    Kingdom* pKingdom = nullptr; 


    cout << "==========\n" 
     << "Input data\n" 
     << "==========\n" 
     << "Enter the number of Kingdoms: "; 
    cin >> count; 
    cin.ignore(); 

    if (count < 1) return 1; 


    pKingdom = new Kingdom[count]; 
    for (int i = 0; i < count; ++i) { 
     cout << "Kingdom #" << i + 1 << ": " << endl; 
     cin >> i; 
     cout << "Enter the name of the Kingdom: " << pKingdom[i].m_name; 
     cin >> pKingdom[i].m_name; 
     cout << "Enter the number people living in " << pKingdom[i].m_population << ": "; 
     cin >> pKingdom[i].m_population; 

    } 
    cout << "==========" << endl << endl; 

    // testing that "display(...)" works 
    cout << "------------------------------" << endl 
     << "The 1st kingdom entered is" << endl 
     << "------------------------------" << endl; 
    sict::display(pKingdom[0]); 
    cout << "------------------------------" << endl << endl; 


    delete[]pKingdom; 
    pKingdom = nullptr; 
      return 0; 
} 

// read accepts data for a Kingdom from standard input 

void read(sict::Kingdom& kingdom) { 

    cout << "Enter the name of the Kingdom: "; 
    cin.get(kingdom.m_name, 32, '\n'); 
    cin.ignore(2000, '\n'); 
    cout << "Enter the number of people living in " << kingdom.m_name << ": "; 
    cin >> kingdom.m_population; 
    cin.ignore(2000, '\n'); 
} 

Lorsque le code arrive à la partie Entrez le nom du Royaume, il invite l'utilisateur à répondre, mais avant qu'il ne l'invite , Il produit juste charabia comme celui-ci

https://i.imgur.com/MSSHgvz.png

De plus, quand il fait entrer le nombre de personnes vivant dans, il serait également sortie « -842150451 » avant que je puisse même saisir un numéro valide.

Des suppositions pour résoudre le problème?

+0

Factor sur tous les pointeurs. Ensuite, utilisez 'std :: string' à la place des tampons char. Ne faites pas 'using namespace std;' dans les en-têtes. Enfin, n'imprimez pas les variables avant qu'elles ne soient initialisées. – moooeeeep

+0

que voulez-vous dire par factoriser tous les pointeurs? – lucas

+0

Ne les utilisez pas dans votre code. – moooeeeep

Répondre

2

Votre programme imprime des données erronées car les variables (char[] et int) ne sont pas initialisées. Le comportement réel est indéfini. Pour résoudre ce problème, vous devez probablement ajouter un constructeur à votre classe et initialiser les variables.

Pour en savoir plus:

Aussi, lorsque vous utilisez std::cin pour permettre aux utilisateurs d'entrer un nom de royaume dans le tableau de taille fixe char, ils peuvent facilement produire une débordement de tampon. Ce n'est généralement pas souhaitable. Merci d'utiliser std::string à la place. L'utilisation de using namespace std; est déconseillée. Surtout dans les fichiers d'en-tête.

Pour en savoir plus:

Sauf si vous avez de très bonnes raisons que vous devriez normalement pas utiliser des pointeurs pour attribuer des objets ou des tableaux dynamiquement. Si vous devez allouer un tableau au moment de l'exécution, utilisez plutôt std::vector.

Pour en savoir plus:

Vous devriez probablement ajouter pour les < surcharges < et >> pour votre classe. Vous n'auriez pas besoin de déclarer ces membres publics alors.

Pour en savoir plus:

0

Vous pouvez le faire comme ça. Je l'ai fait rapidement, donc ce n'est pas parfait.

main.cpp

#include "Kingdom.h" 

int main() { 
    int count = 0; // the number of kingdoms in the array 
    sict::Kingdom* pKingdom = nullptr; 


    std::cout << "==========\n" 
     << "Input data\n" 
     << "==========\n" 
     << "Enter the number of Kingdoms: "; 
    std::cin >> count; 
    std::cin.ignore(); 

    if (count < 1) return 1; 

    //without read() 
    //pKingdom = new sict::Kingdom[count]; 
    //for (int i = 0; i < count; ++i) { 
    // std::cout << "Kingdom #" << i + 1 << ": \n"; 
    // //cin >> i; 
    // std::cout << "Enter the name of the Kingdom: "; 
    // pKingdom[i].setName(); 
    // std::cout << "Enter the number people living in " << pKingdom[i].m_name << ": "; 
    // std::cin >> pKingdom[i].m_population; 

    //} 
    //std::cout << "==========\n\n"; 

    pKingdom = new sict::Kingdom[count](); 

    for (int i = 1; i <= count; ++i) 
    { 
     pKingdom[i-1].read(); 
    } 


    // testing that "display(...)" works 
    std::cout << "------------------------------\n" 
     << "The 1st kingdom entered is:\n" 
     << "------------------------------\n"; 
    pKingdom[0].display(); 
    std::cout << "------------------------------\n\n"; 


    delete[]pKingdom; 
    pKingdom = nullptr; 
    return 0; 
} 

Kingdom.h

#ifndef KINGDOM_H_ 
#define KINGDOM_H_ 

#include <iostream> 
#include <string> 

namespace sict { 

    class Kingdom { 
    private: 
     std::string m_name; 
     int m_population; 
    public: 
     Kingdom(); 

     void display(); 
     void read(); 

     const std::string& getName(); 
     void setName(std::string name); 
     const int& getPopulation(); 
     void setPopulation(int population); 
    }; 
} 
#endif 

Kingdom.cpp

#include "Kingdom.h" 

namespace sict { 

    Kingdom::Kingdom() 
     :m_name(""), 
     m_population(0) 
    {} 

    void Kingdom::display() { 
     std::cout << "Name: " << m_name << ", population: " << m_population << "\n"; 
    } 

    void Kingdom::read() { 
     std::cout << "\n\nEnter the name of the Kingdom: "; 
     std::getline(std::cin, m_name); 

     std::cout << "Enter the number of people living in " << m_name << ": "; 
     std::cin >> m_population; 
     std::cin.ignore(); 
    } 

    const std::string& Kingdom::getName() 
    { 
     return m_name; 
    } 

    void Kingdom::setName(std::string name) 
    { 
     m_name = name; 
    } 

    const int& Kingdom::getPopulation() 
    { 
     return m_population; 
    } 

    void Kingdom::setPopulation(int population) 
    { 
     m_population = population; 
    } 

}