Le principal problème avec votre code est l'indentation. Votre code n'est pas lisible et surtout difficile à comprendre. Embellissez-le. Ecrire agréable, lisible et un code structuré. Vous pouvez en savoir plus sur l'indentation au lien suivant.
https://en.wikipedia.org/wiki/Indentation_style
Ensuite, la déclaration de fonction. Vous ne déclarez pas votre fonction avant de la définir. La déclaration de la fonction doit être en haut de la fonction main
et la définition de la fonction doit être inférieure à la fonction main
. Vous pouvez trouver plus d'informations sur la déclaration de fonction à l'adresse suivante:
http://en.cppreference.com/w/cpp/language/function
Puisque vous n'utilisez pas char array
pour imprimer le string
, il est inutile de passer par le string
avec une boucle. Incluez la bibliothèque <string>
et commencez à travailler vers le type string
. En passant la variable string
à l'intérieur std::cout
est suffisant pour imprimer le string
.
Enfin, puisque vous essayez de manipuler une variable string
en dehors de la fonction main
, il est nécessaire que vous passiez un paramètre de référence à la place. (void myFunction(std::string& parameter);
. De cette façon, la variable d'origine qui existe à l'intérieur du principal ou à l'intérieur de toute autre fonction sera modifiée. Sans la référence, &
, la valeur que vous essayez de modifier ne sera pas modifiée.
Le lien suivant montre l'utilisation de la référence.
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
Veuillez lire mes commentaires ci-dessous de la raison pour laquelle certaines modifications ont été appliquées. J'ai fait des changements astucieux à la fonction change
. Vous êtes maintenant éligible pour travailler vers n'importe quel type string
avec n'importe quelle taille.
#include <iostream>
#include <string> //When you are working on strings, use the string library.
using namespace std;
//Function declaration is very important. Have the declarations above main.
void change(string&);
int main() {
string word("HELLO");
char choice;
cout << "The word is : " << endl;
cout << word << endl;
//No need for the for loop to print out the string as
// we are working on a string and not a char array.
// for (int i = 0; i < word.length(); i++) {
// cout << word[i];
// }
change(word);
cout << "The new word is" << endl << word << endl;
cout << "Would you like to enter another change ? Enter Y or N ? " << endl;
cin >> choice;
if (choice == 'y' || choice == 'Y') {
change(word);
cout << word << endl;
}
else {
cout << "Good Bye" << endl;
}
system("pause");
return 0;
}
//When your datatype is to be modified outside the function, use the reference
//parameter type '&'.
//Without the reference type, your modified version of the type will only be modified
//inside that function.
//The original one will not be altered.
void change(string& word) {
/*
* size_t is simply unsigned int, to work towards manipulation and accessing
* of string types, use unsigned int or std::size_t
*/
size_t x = 0;
cout << "Would you like to change a letter? Please enter the letter position. " << endl;
cin >> x;
//Check to see if the inputted value is within the string length range.
if(x > 0 && x <= word.length())
cout << "What do you want to change it to?" << endl;
else{
cout << "The entered position is outside the string size range\n";
return; //Quit from the function if the condition is not met.
}
/*
* Instead of using if/else if statements,
* Just make a normal loop. Much simpler.
*/
for(size_t i = 0; i < word.length(); i++){
if((x-1) == i)
cin >> word[i];
}
}
Renommer votre fonction de 'swap' ou supprimer l'évidence' l'aide de l'espace de noms std' vous avez – Tas
Votre code ressemble hideuse et trop en désordre. Votre fonction 'swap' ne change vraiment rien, seulement remplace un certain caractère. Renommez-le. Que faites-vous après le mot 'std :: cin >> mot [0];'? –
C'est une autre instruction if/else. Donc l'idée si quelqu'un appuie sur 1 change la première lettre. 2 change la seconde. –