LTK*_*LTK 13 c++ string variables function
更新:感谢大家的快速回复 - 问题解决了!
我是C++和编程新手,遇到了一个我无法弄清楚的错误.当我尝试运行该程序时,我收到以下错误消息:
stringPerm.cpp: In function ‘int main()’:
stringPerm.cpp:12: error: expected primary-expression before ‘word’
Run Code Online (Sandbox Code Playgroud)
我还尝试在将变量分配给函数之前在单独的行上定义变量,但我最终得到了相同的错误消息.
任何人都可以提供一些建议吗?提前致谢!
见下面的代码:
#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);
}
}
Run Code Online (Sandbox Code Playgroud)
Oma*_*aha 20
你的电话中不需要"字符串" wordLengthFunction()
.
int wordLength = wordLengthFunction(string word);
应该
int wordLength = wordLengthFunction(word);
更改
int wordLength = wordLengthFunction(string word);
Run Code Online (Sandbox Code Playgroud)
至
int wordLength = wordLengthFunction(word);
Run Code Online (Sandbox Code Playgroud)
string
发送参数时不应重复该部分.
int wordLength = wordLengthFunction(word); //you do not put string word here.
Run Code Online (Sandbox Code Playgroud)