web-dev-qa-db-fra.com

C ++ - expression primaire attendue avant ''

Mise à jour: merci à tous pour vos réponses rapides - problème résolu!

Je suis nouveau en C++ et en programmation, et j'ai rencontré une erreur que je ne peux pas comprendre. Lorsque j'essaie d'exécuter le programme, j'obtiens le message d'erreur suivant:

stringPerm.cpp: In function ‘int main()’:
stringPerm.cpp:12: error: expected primary-expression before ‘Word’

J'ai également essayé de définir les variables sur une ligne distincte avant de les affecter aux fonctions, mais je finis par obtenir le même message d'erreur.

Quelqu'un peut-il donner des conseils à ce sujet? Merci d'avance!

Voir le code ci-dessous:

#include <iostream>
#include <string>
using namespace std;

string userInput();
int wordLengthFunction(string Word);
int permutation(int wordLength);

int main()
{
    string Word = userInput();
    int wordLength = wordLengthFunction(string Word);

    cout << Word << " has " << permutation(wordLength) << " permutations." << endl;

    return 0;
}

string userInput()
{
    string Word;

    cout << "Please enter a Word: ";
    cin >> Word;

    return Word;
}

int wordLengthFunction(string Word)
{
    int wordLength;

    wordLength = Word.length();

    return wordLength;
}

int permutation(int wordLength)
{    
    if (wordLength == 1)
    {
        return wordLength;
    }
    else
    {
        return wordLength * permutation(wordLength - 1);
    }    
}
16
LTK

Vous n'avez pas besoin de "chaîne" dans votre appel à wordLengthFunction().

int wordLength = wordLengthFunction(string Word);

devrait être

int wordLength = wordLengthFunction(Word);

21
Omaha

Changement

int wordLength = wordLengthFunction(string Word);

à

int wordLength = wordLengthFunction(Word);
7
fbafelipe

Vous ne devez pas répéter la partie string lors de l'envoi des paramètres.

int wordLength = wordLengthFunction(Word); //you do not put string Word here.
5
crashmstr