Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ -- expected primary-expression before ' '

Tags:

I am new to C++ and programming, and have run into an error that I cannot figure out. When I try to run the program, I get the following error message:

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

I've also tried defining the variables on a separate line before assigning them to the functions, but I end up getting the same error message.

Can anyone offer some advice about this? Thanks in advance!

See code below:

#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);     }     } 
like image 250
LTK Avatar asked Jul 16 '12 15:07

LTK


People also ask

What is mean by expected primary expression before token?

This typically means that your code is missing a closing '}'. For every opening '{' there should be a closing '}' expected primary-expression before ')' token. Sometimes this happens when the variable passed into a function isn't the type the function expected.

What is primary expression in C?

Primary expressions are the building blocks of more complex expressions. They may be constants, identifiers, a Generic selection, or an expression in parentheses.

How do you fix expected unqualified ID before token?

– Adjust the Curly Braces To Fix the Expected Unqualified Id Error. You should match the opening and closing curly braces in your code to ensure the right quantity of brackets. The code should not have an extra or a missing curly bracket.

What is expected primary expression before token Arduino?

A “define” replaces the text before the compiler starts compiling. A “const int” is a fixed number and is a little more preferred for pin assignments.


2 Answers

You don't need "string" in your call to wordLengthFunction().

int wordLength = wordLengthFunction(string word);

should be

int wordLength = wordLengthFunction(word);

like image 190
Omaha Avatar answered Sep 28 '22 13:09

Omaha


Change

int wordLength = wordLengthFunction(string word); 

to

int wordLength = wordLengthFunction(word); 
like image 44
fbafelipe Avatar answered Sep 28 '22 13:09

fbafelipe