显示使用函数、switch 语句和多维数组的程序的多个不合逻辑的错误
Multiple illogical errors showing for program utilizing a function, switch statement, and multidimensional array
我的代码遇到了问题,真的没有意义。某些事情在某一点被称为错误,而在另一点则不然。它要求一个括号,但我看不出哪里甚至需要它。IDE 告诉我,在定义之前,函数声明后需要分号,但只有在原型之后才需要。代码看起来很可靠,但这些错误不断弹出。这是我的程序:
#include <iostream>
#include <iomanip>
using namespace std;
using std::istream;
int FizzBuzz(int, int);
int main()
{
int rowNum{}; // Declaration and initialization of variable number
int columnNum{}; // Declaration and initialization of variable number
int counter{}; // Declaration/initialization of counter
int array[4][4]{ { 60, 84, 50, 57 } ,{ 80, 75, 10, 54 } ,{ 81, 33, 55, 90 } ,{ 21, 35, 63, 45 } };
// Array initialization/Declaration
char indicator{ 'n' }; // Continue or not?
cout << "Welcome to my Fizzbuzz game, you are to guess the location of a "
<< "number which if is divisible by 5 and 3 you will win with "
<< "the output of Fizzbuzz. " << endl;
for (;;)
{
counter++;
switch (!(int FizzBuzz(int, int) % 15)) {
case 1: printf("You win you got FizzBuzz!!! n");
cout << "It took you " << counter << " tries." << endl;
break;
case 0:
switch (!(int FizzBuzz(int, int) % 3)) {
case 1: printf("Fizz, please try again. n");
break;
case 0:
switch (!(int FizzBuzz(int, int) % 5)) {
case 1: printf("Buzz, please try again. n");
break;
case 0: printf("please enter another input. n");
break;
}; break;
}; break;
}
cout << endl << "Do you want to enter another array (please enter y or n? ";
cin >> indicator; // Read indicator
if (('n' == indicator) || ('N' == indicator))
break; // Exit from loop
}
int FizzBuzz(int, int)
{
int result{};
cout << "Please enter an integer value between 0 and 3 "
<< "representing the row location of the number for the game, "
<< "then press the Enter key: " << endl;
switch (rowNum)
{
case 0: cout << endl
<< "Now please enter a value for the location within the column." << endl;
cin >> columnNum;
result = array[rowNum][columnNum]
break;
case 1: cout << endl
<< "Now please enter a value for the location within the column." << endl;
cin >> columnNum;
result = array[rowNum][columnNum]
break;
case 2: cout << endl
<< "Now please enter a value for the location within the column." << endl;
cin >> columnNum;
result = array[rowNum][columnNum]
break;
case 3: cout << endl
<< "Now please enter a value for the location within the column." << endl;
result = array[rowNum][columnNum]
break;
default: cout << endl << "You entered an incorrect value."
<< endl;
result = 0;
break;
}
return result;
同样,我真的不明白问题在哪里,因为我什至在写这篇文章时得到了帮助。
你的代码中有很多错误。我建议打开编译器警告并分析警告和错误消息。
在main
的多个地方,您有一些intLiteral
:
int FizzBuzz(int, int) % intLiteral
这不是您调用函数的方式。您可能应该使用类似的东西
FizzBuzz(rowNum, columnNum) % intLiteral
相反。另请注意,您正在打开布尔值(例如 !(FizzBuzz(42, 42) % 3)
(并将其与整数0
和1
进行比较(例如 case 0:
(。为什么不使用if
语句呢?
main
的定义缺少FizzBuzz
声明之前的结束}
。
FizzBuzz
的定义也缺少闭}
,并且也缺少其参数的名称。定义可能应该以
int FizzBuzz(int rowNum, int columnNum)
{
变量array
也可能应该是FizzBuzz
的一部分,而不是main
。
FizzBuzz
result = array[rowNum][columnNum]
行末尾缺少分号。
相关文章:
- 类动态数组错误
- 如何返回多维数组?错误C2440
- 数组错误(不允许类型不完整)
- C++ 将文件读取到数组错误:'operator>>'不匹配
- 不断增加的指针数组 错误:赋值中的类型不兼容
- 从文本文件读取 2D 数组错误
- 动态结构数组错误
- 旋转数组 - 错误
- 按字母顺序排序名称,数组错误
- 因为每个循环都读取数组错误
- 二维数组错误
- C 获取数组错误
- 编译时出现字符串数组错误
- c++2维指针类数组错误(使用openframework)
- C++数组错误
- C++上数组错误的大小
- C++数组错误:访问冲突读取位置0xC0000005
- c++2D数组错误
- C++:为“char [4]”数组错误获取太多初始值设定项
- 分段错误(核心转储)排序字符串数组错误