IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

Tout nouveau, j'arrive avec une question ^^


Sujet :

C++

  1. #21
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    Allo, ben ouais je crois progresser dans la correction de mon codes ^_^... voici ce que ça donne jusqu'à maintenant :
    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
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
     
    //main.cpp
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include "pendu.h"
     
    //code trouvé à http://cpp.enisoc.com/articles/clrscr/
    void clear()
    {
      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
      COORD coord = {0, 0};
      DWORD count;
     
      CONSOLE_SCREEN_BUFFER_INFO csbi;
      GetConsoleScreenBufferInfo(hStdOut, &csbi);
     
      FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
     
      SetConsoleCursorPosition(hStdOut, coord);
    }
     
    unsigned int count(const std::string &chaine, const char &caractere) {
    	unsigned int combien = 0;
    	for (unsigned int i = 0; i < chaine.length(); i++) {
    		if (caractere == chaine[i]) combien++;
    	}
     
    	return combien;
    }
     
    unsigned short int main() {
    	std::string rejouer;
     
    	do {
    		bool temoin = true;
    		Mot alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
     
    		std::string mot;
    		do {
    			std::cout << "Inscrivez un mot pour faire une partie de pendu : ";
    			std::cin >> mot;
    			for (unsigned int i = 0; i < mot.length(); i++) {
    				if ((temoin && !alphabet.Contient(toupper(mot[i]))) || (mot.length() < 2) || (count(mot, mot[0]) == mot.length())) temoin = false;
    			}
    		} while (!temoin);
     
        Pendu pendu(mot);
        std::string essai;
        while(pendu.PartieTermine() == ETAT_PENDU::ENCOURS) {
        	clear();
    			pendu.Affichage(AFFICHAGE::PENDU);
    			pendu.Affichage(AFFICHAGE::MOTTIRETS);
    			pendu.Affichage(AFFICHAGE::ALPHABET);
    			std::cout << "Faire un essai : ";
    			std::cin >> essai;
    			pendu.UnEssai(essai[0]);
        }
        clear();
        pendu.Affichage(AFFICHAGE::PENDU);
     
        if (pendu.PartieTermine() == ETAT_PENDU::GAGNEE) {
        	std::cout << "Bravo! Le mot était bel et bien ";
        } else {
        	std::cout << "Désolé! Le mot était ";
        }
        std::cout << pendu.Get_motADeviner().Get_mot() << "." << std::endl;
     
        do {
    			std::cout << "Souhaîtez vous rejouer une partie? (O/N) ";
    			std::cin >> rejouer;
    			rejouer = std::string(1, rejouer[0]);
    			transform(rejouer.begin(), rejouer.end(), rejouer.begin(), toupper);
        } while ((rejouer != "O") && (rejouer != "N"));
        clear();
    	} while (rejouer == "O");
     
    	return 0;
    };
    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
     
    //mot.h
    #include <string>
    #include <algorithm>
     
    class Mot {
    	private:
    		std::string m_mot;
    		std::string UCase(std::string chaine) {
    			std::transform(chaine.begin(), chaine.end(), chaine.begin(), toupper);
    			return chaine;
    		}
     
    	public:
    		const std::string Get_mot() { return m_mot; }
    		const unsigned int Get_len() { return m_mot.length(); }
     
    		void Set_mot(std::string mot) {
    			m_mot = UCase(mot);
    		}
     
    		Mot() {}
    		Mot(const std::string &mot) { Set_mot(mot); }
     
    		char Lettre(const unsigned int &position) {
    			if (position < m_mot.length()) return m_mot[position];
    			else return ' ';
    		}
     
    		void ChangerLettre(const unsigned int &position, const char &nouvLettre) { 
    			if (position < m_mot.length()) m_mot.replace(position, 1, UCase(std::string(1, nouvLettre)));
    		}
     
    		bool Contient(const char &caractere) { return (m_mot.find(caractere) != m_mot.npos); }
     
    		unsigned int Cherche(char &occurence) {
    			if (Contient(occurence)) {
    				return m_mot.find(occurence);
    			} else {
    				return 0;
    			}
    		}
     
    		bool Comparer(Mot &mot) { return (m_mot == mot.Get_mot()); }
    };
    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
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
     
    //pendu.h
    #include <string>
    #include <algorithm>
    #include <iostream>
    #include "mot.h"
     
    namespace AFFICHAGE
    {
    	const enum AFFICHER { PENDU, MOTTIRETS, ALPHABET };
    }
     
    namespace ETAT_PENDU
    {
    	const enum STATUS_PARTIE { ENCOURS, GAGNEE, PERDUE };
    }
     
    class Pendu {
    	private:
    		Mot m_motADeviner;
    		Mot m_motTirets;
    		Mot m_alphabet;
    		unsigned short int m_nbEssaisMax;
    		unsigned short int m_noEssai;
     
    	public:
    		Mot Get_motADeviner() { return m_motADeviner; }
    		Mot Get_motTirets() { return m_motTirets; }
    		Mot Get_alphabet() { return m_alphabet; }
    		unsigned int Get_nbEssaisMax() { return m_nbEssaisMax; }
    		unsigned int Get_noEssai() { return m_noEssai; }
     
    		void Set_mot(const std::string &mot) { m_motADeviner.Set_mot(mot); }
     
    		Pendu () {}	
    		Pendu(const std::string &mot)
    		:	m_nbEssaisMax(5), m_noEssai(0), m_motADeviner(mot), 
    			m_motTirets(std::string(mot.length(), '_')), 
    			m_alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    		{
    			UnEssai(m_motADeviner.Lettre(0));
    		}
     
    		void UnEssai(char lettre) {
    			lettre = toupper(lettre);
    			if ((lettre != ' ') && (m_alphabet.Contient(lettre))) {
    				if (m_motADeviner.Contient(lettre)) {
    					for (unsigned int i = 0; i < m_motADeviner.Get_len(); i++) {
    						if (m_motADeviner.Lettre(i) == lettre) {
    							m_motTirets.ChangerLettre(i, lettre);
    						}
    					}
    				} else {
    					m_noEssai++;
    				}
    				m_alphabet.ChangerLettre(m_alphabet.Cherche(lettre), char(' '));					
    			}
    		}
     
    		ETAT_PENDU::STATUS_PARTIE PartieTermine() {
    			if (m_motTirets.Comparer(m_motADeviner)) {
    				return ETAT_PENDU::GAGNEE;
    			} else if(m_noEssai == m_nbEssaisMax) {
    				return ETAT_PENDU::PERDUE;
    			} else {
    				return ETAT_PENDU::ENCOURS;
    			}
    		}
     
    		void Affichage(const AFFICHAGE::AFFICHER &quoi) {
    			switch(quoi)
    			{
    				case AFFICHAGE::PENDU:
    					switch(m_noEssai)
    					{
    						case 0:
    							std::cout << "_______Pendu_______" << std::endl;
    							std::cout << "|                 |" << std::endl;
    							std::cout << "|                 |" << std::endl;
    							std::cout << "|   |_____        |" << std::endl;
    							std::cout << "|   |/   |        |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |             |" << std::endl;
    							std::cout << "|   |             |" << std::endl;
    							std::cout << "|   |             |" << std::endl;
    							std::cout << "|   |             |" << std::endl;
    							std::cout << "|   |________     |" << std::endl;
    							std::cout << "|   |       |     |" << std::endl;
    							std::cout << "|   |       |     |" << std::endl;
    							std::cout << "___________________" << std::endl;
    							break;
    						case 1:
    							std::cout << "_______Pendu_______" << std::endl;
    							std::cout << "|                 |" << std::endl;
    							std::cout << "|                 |" << std::endl;
    							std::cout << "|   |_____        |" << std::endl;
    							std::cout << "|   |/   |        |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |   ( )       |" << std::endl;
    							std::cout << "|   |             |" << std::endl;
    							std::cout << "|   |             |" << std::endl;
    							std::cout << "|   |             |" << std::endl;
    							std::cout << "|   |________     |" << std::endl;
    							std::cout << "|   |       |     |" << std::endl;
    							std::cout << "|   |       |     |" << std::endl;
    							std::cout << "___________________" << std::endl;
    							break;
    						case 2:
    							std::cout << "_______Pendu_______" << std::endl;
    							std::cout << "|                 |" << std::endl;
    							std::cout << "|                 |" << std::endl;
    							std::cout << "|   |_____        |" << std::endl;
    							std::cout << "|   |/   |        |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |   ( )       |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |________     |" << std::endl;
    							std::cout << "|   |       |     |" << std::endl;
    							std::cout << "|   |       |     |" << std::endl;
    							std::cout << "___________________" << std::endl;
    							break;
    						case 3:
    							std::cout << "_______Pendu_______" << std::endl;
    							std::cout << "|                 |" << std::endl;
    							std::cout << "|                 |" << std::endl;
    							std::cout << "|   |_____        |" << std::endl;
    							std::cout << "|   |/   |        |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |   ( )       |" << std::endl;
    							std::cout << "|   |   _|_       |" << std::endl;
    							std::cout << "|   |  / | \\      |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |________     |" << std::endl;
    							std::cout << "|   |       |     |" << std::endl;
    							std::cout << "|   |       |     |" << std::endl;
    							std::cout << "___________________" << std::endl;
    							break;
    						case 4:
    							std::cout << "_______Pendu_______" << std::endl;
    							std::cout << "|                 |" << std::endl;
    							std::cout << "|                 |" << std::endl;
    							std::cout << "|   |_____        |" << std::endl;
    							std::cout << "|   |/   |        |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |   ( )       |" << std::endl;
    							std::cout << "|   |   _|_       |" << std::endl;
    							std::cout << "|   |  / | \\      |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |___/_\\__     |" << std::endl;
    							std::cout << "|   |       |     |" << std::endl;
    							std::cout << "|   |       |     |" << std::endl;
    							std::cout << "___________________" << std::endl;
    							break;
    						case 5:
    							std::cout << "_______Pendu_______" << std::endl;
    							std::cout << "|                 |" << std::endl;
    							std::cout << "|                 |" << std::endl;
    							std::cout << "|   |_____        |" << std::endl;
    							std::cout << "|   |/   |        |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |   (x)       |" << std::endl;
    							std::cout << "|   |  /_|_\\      |" << std::endl;
    							std::cout << "|   |    |        |" << std::endl;
    							std::cout << "|   |___ | __     |" << std::endl;
    							std::cout << "|   |  \\/ \\ |     |" << std::endl;
    							std::cout << "|   |  \|    |     |" << std::endl;
    							std::cout << "___________________" << std::endl;
    					}
    					break;
    				case AFFICHAGE::MOTTIRETS:
    					std::cout << "[Lettres trouvées]" << std::endl;
    					std::cout << " -";
    					for (unsigned short int i = 0; i < m_motTirets.Get_len(); i++) {
    						std::cout << "--";
    					}
    					std::cout << " " << std::endl;
     
    					std::cout << "| ";
    					for (unsigned short int i = 0; i < m_motTirets.Get_len(); i++) {
    						std::cout << m_motTirets.Lettre(i) <<" ";
    					}
    					std::cout << "|" << std::endl;
     
    					std::cout << " -";
    					for (unsigned short int i = 0; i < m_motTirets.Get_len(); i++) {
    						std::cout << "--";
    					}
    					std::cout << " " << std::endl;
    					break;
    				case AFFICHAGE::ALPHABET:
    					std::cout << "[Lettres disponibles]" << std::endl;
    					std::cout << " -------------------------- " << std::endl;
    					std::cout << "|" << m_alphabet.Get_mot() << "|" << std::endl;
    					std::cout << " -------------------------- " << std::endl;
     
    			}
    	}
    };
    Bref, j'ai remarquer un bug, si lorsque le programme demande à l'utilisateur d'inscrire un mot et qu'il écrit des chiffres mêlés à des lettres. La boucle lui redemande d'inscrire un mot (juske là c'est OK). Mais suite à cela si il entre un mot valide (c-a-d constitué de lettres). La boucle continu. Ah... j'oubliais... on parle de cette boucle :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    		do {
    			std::cout << "Inscrivez un mot pour faire une partie de pendu : ";
    			std::cin >> mot;
    			for (unsigned int i = 0; i < mot.length(); i++) {
    				if ((temoin && !alphabet.Contient(toupper(mot[i]))) || (mot.length() < 2) || (count(mot, mot[0]) == mot.length())) temoin = false;
    			}
    		} while (!temoin);
    Pour faire encors plus court je crois que le bug se produit dans ces lignes :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    			for (unsigned int i = 0; i < mot.length(); i++) {
    				if ((temoin && !alphabet.Contient(toupper(mot[i]))) || (mot.length() < 2) || (count(mot, mot[0]) == mot.length())) temoin = false;
    			}
    Donc, je patauge, p-e demain je vais y voir plus clair sur ce nenuit

  2. #22
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    Puah! Une erreure tout bête, un oubli ^_^
    dans ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
     
          do {
             std::cout << "Inscrivez un mot pour faire une partie de pendu : ";
             std::cin >> mot;
             for (unsigned int i = 0; i < mot.length(); i++) {
                if ((temoin && !alphabet.Contient(toupper(mot[i]))) || (mot.length() < 2) || (count(mot, mot[0]) == mot.length())) temoin = false;
             }
          } while (!temoin);
    Je devait ajouter : temoin = true; apres le do
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
     
          do {
             temoin = true;
             std::cout << "Inscrivez un mot pour faire une partie de pendu : ";
             std::cin >> mot;
             for (unsigned int i = 0; i < mot.length(); i++) {
                if ((temoin && !alphabet.Contient(toupper(mot[i]))) || (mot.length() < 2) || (count(mot, mot[0]) == mot.length())) temoin = false;
             }
          } while (!temoin);

  3. #23
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    Rebonjour!!! Voilà j'ai eu quelques problemes avec mon pc et je suis de retour ... sauf que là je voulais esseyer gcc (downloader à l'aide de delorie)... bref il est installer et tout et là j'arrive pour compiler mon pendu et je recois cette erreur
    E:\DVELOP~1\C__~1\pendu>gxx main.cpp
    pendu.h:9: error: qualifiers can only be specified for objects and functions
    pendu.h:14: error: qualifiers can only be specified for objects and functions
    Bah, je me demandais ce que cela signifiait o_O
    Voici les lignes de codes de 1 à 14
    //main.cpp
    #include <iostream>
    #include <string>
    #include "pendu.h"

    unsigned int count(const std::string &chaine, const char &caractere) {
    unsigned int combien = 0;
    for (unsigned int i = 0; i < chaine.length(); i++) {
    if (caractere == chaine[i]) combien++;
    }

    return combien;
    }
    Par le fait même, j'ai remarquer qu'il n'y avais pas de librarie sock ou winsock2 cela veux-t-il dire qu'il me faut aller les downloader par moi-même?

  4. #24
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 379
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 379
    Points : 41 573
    Points
    41 573
    Par défaut
    les erreurs sont dans pendu.h, pas dans pendu.c...

  5. #25
    Nouveau membre du Club
    Inscrit en
    Octobre 2005
    Messages
    22
    Détails du profil
    Informations forums :
    Inscription : Octobre 2005
    Messages : 22
    Points : 26
    Points
    26
    Par défaut
    Bonjour,

    Je n'ai pas spécialement regardé ton code, mais pour le C++, tu devrais plutôt utiliser g++ qui est inclu dans l'installation de gcc (normalement).

    Cordialement,
    Bisounours.

  6. #26
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    Citation Envoyé par Médinoc
    les erreurs sont dans pendu.h, pas dans pendu.c...
    lol ctait pas fort ^^!

    je viens de toruver mon erreur, elle était dans le fait que j'appelais un name space identique dans deux fichiers...

Discussions similaires

  1. [VxiR2] Lister toutes les tables jointes avec une table
    Par Geo55 dans le forum Designer
    Réponses: 3
    Dernier message: 16/06/2016, 20h44
  2. Nouveau avec une question sur Xcode
    Par shub22 dans le forum XCode
    Réponses: 0
    Dernier message: 24/11/2014, 12h15
  3. Réponses: 2
    Dernier message: 21/02/2014, 13h33
  4. Hardware : Freecom sécurise son nouveau disque dur avec une carte RFID
    Par Emmanuel Chambon dans le forum Actualités
    Réponses: 8
    Dernier message: 16/07/2009, 13h31
  5. [MySQL] interdire les votes toutes les 5 minutes avec une variable de session
    Par mademoizel dans le forum PHP & Base de données
    Réponses: 4
    Dernier message: 18/03/2008, 10h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo