显示使用函数、switch 语句和多维数组的程序的多个不合逻辑的错误

Multiple illogical errors showing for program utilizing a function, switch statement, and multidimensional array

本文关键字:数组 错误 不合逻辑 程序 函数 语句 switch 显示      更新时间:2023-10-16

我的代码遇到了问题,真的没有意义。某些事情在某一点被称为错误,而在另一点则不然。它要求一个括号,但我看不出哪里甚至需要它。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) (并将其与整数01进行比较(例如 case 0: (。为什么不使用if语句呢?

main的定义缺少FizzBuzz声明之前的结束}

FizzBuzz的定义也缺少闭},并且也缺少其参数的名称。定义可能应该以

int FizzBuzz(int rowNum, int columnNum)
{

变量array也可能应该是FizzBuzz的一部分,而不是main

FizzBuzz result = array[rowNum][columnNum]行末尾缺少分号。