开关语句中的函数调用
Function Call in switch statement.
嘿伙计们,我正在尝试弄清楚如何使用 switch 语句在我的代码中调用函数。我试图寻找许多不同的参考资料,但无论如何似乎都不起作用,如果有人能让我走上正确的道路,那将是一个很大的帮助。代码如下:
#include <iostream>
#include <string>
using namespace std;
int playGame(string word);
int main()
{
int choice;
bool menu = true;
do{
cout <<"Please select one of the following options: n";
cout << "1: Playn"
"2: Helpn"
"3: Confign"
"4: Quitn";
cout << "Enter your selection (1, 2 and 3): ";
cin >> choice;
//*****************************************************************************
// Switch menu to display the menu.
//*****************************************************************************
switch (choice)
{
case 1:
cout << "You have chosen playn";
int playGame(string word);
break;
case 2:
cout << "You have chosen helpn";
cout << "Here is a description of the game Hangman and how it is played:nThe word to guess is represented by a row of dashes, giving the number of letters, numbers and category. If the guessing player suggests a letter or number which occurs in the word, the other player writes it in all its correct positions";
break;
case 3:
cout << "You have chosen Quit, Goodbye.";
break;
default:
cout<< "Your selection must be between 1 and 3!n";
}
}while(choice!=3);
getchar();
getchar();
cout << "You missed " << playGame("programming");
cout << " times to guess the word programming." << endl;
}
int playGame(string word) //returns # of misses
{
//keep track of misses
//guess is incorrect
//repeated guess of same character
//guess is correct
int misses = 0;
int exposed = 0;
string display = word;
for(int i=0; i< display.length(); i++)
display[i] ='*';
while(exposed < word.length()) {
cout << "Miss:" << misses << ":";
cout << "Enter a letter in word ";
cout << display << " : ";
char response;
cin >> response;
bool goodGuess = false;
bool duplicate = false;
for(int i=0 ; i<word.length() ; i++)
if (response == word[i])
if (display[i] == word[i]) {
cout << response << " is already in the word.n";
duplicate = true;
break;
} else {
display[i] = word[i];
exposed++;
goodGuess = true;
}
if (duplicate)
continue;
if (!goodGuess){
misses ++;
cout << response << " is not in the word.n";
}
}
cout << "Yes, word was " << word << "." << endl;
return misses;
}
您没有在 switch 语句中调用playGame
函数,
switch (choice)
{
case 1:
cout << "You have chosen playn";
//int playGame(string word); // this does not call playGame,
// it re-declare playGame function again
playGame("word"); // this will call playGame with word parameter
//^^^^^^^^^^^^^^^
break;
int playGame(string word);
在您的开关语句中可能是问题所在...尝试:
int misses = playGame(word);
您正在尝试从 playGame 方法返回未命中数,因此您必须将返回数据放在变量中。
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 在可变函数调用中执行一次语句
- C++实用程序,用于将长开关语句转换为封装开关案例阶梯的简洁函数调用
- 一个关于在函数中运行未调用语句的非常奇怪的错误
- 递归可变参数函数调用对简单 if.else 语句的性能
- 在 CPP 中将函数调用定时为 if 语句条件
- 开关语句中的函数调用
- 避免循环中的条件语句和函数调用
- 哪一个更快?函数调用或条件if语句
- gcc-gprof/gcov/other-如何获取函数调用/出口+控制流语句的序列
- 如果函数调用是返回语句,编译器可以自动移动函数参数吗?