0

I am writing in C++ and the errors I am having are:

E0120 return value type does not match the function type

C4700 uninitialized local variable 'name' used

C2562 interact 'void function returning a value

And a warning error of C4447.

My code is down below:

#include <limits>
#include <cstdlib>
#include <fstream>
#include <iostream>
    
    
using namespace std;
    
void readFile(char sudokuBoard[][9]);
    
void writeFile(char sudokuBoard[][9]);
    
void display(char sudokuBoard[][9]);
    
void interact();
    
void getOption(char sudokuBoard[][9]);
    
void editSquare(char sudokuBoard[][9]);
    
void showValues();
    
void openingMessage();
    
void openingQuestion();
    
    
//opening message to the game and pause
    
void openingMessage()
{

    std::cout << "Welcome to Sudoku" 

        << endl << "Press any key to continue.";

    cin.ignore();

}

//Questions to determine whether the user will play the game 

void openingQuestion()

{

    char response; // users input to the question below

    cout << "Would you like to play Sudoku ? " << endl << "1 for yes, 2 for no";
    cin >> response;

    if (response == 1) //if there answer is yes 
    {
        //return main();
    }

    else if (response == 2) //if answer is no
    {
        cout << "are you sure you do not want to play ?" << endl << "1 for yes and 2 for no";

        //if user answers no
        char eResponse; //response to the next question
        if (eResponse == 2) // 2 equals no
        {
            return main();
        }
        // if user answers yes
        if (eResponse == 1) // 1 equals yes 
        {
            //return exit();
        }
    }
}

//Makes other functions function

int main()

{

    //Declaring array
    char sudokuBoard[9][9];

    //calling the other functions
    readFile(sudokuBoard);
    interact();
    display(sudokuBoard);

    return 0;
}

//Asks the user for a fulename to read a gamebaord 

//in from that file name and then places it in an array

void readFile(char sudokuBoard[][9])

{

    //Declare filename

    char sourceFile[256];

    //Declaring file input
    ifstream fin;

    //Getting the filename from the user 
    cout << "Where is your board located? ";
    cin >> sourceFile;

    //Open file error checking 
    fin.open(sourceFile);
    if (fin.fail())
    {
        cout << "Input file opening failed. " << endl;
        exit(1);
    }

    //Read file into array
    for (int col = 0; col < 9; col++)
    {
        for (int row = 0; row < 9; row++)
        {
            fin >> sudokuBoard[row][col];
            if (sudokuBoard[row][col] == '0')
            {
                sudokuBoard[row][col] = ' ';
            }
        }
    }
    //close the file
    fin.close();
}

//displays the result to the screen

void display(char sudokuBoard[][9])

    {

        //Declare variables
        char option;

        //Display Column Title

        cout << " A| B| C| D| E| F| G| H| I" << endl;

        //Row 1 
        cout << "1 "
            << sudokuBoard[0][0]
            << " " << sudokuBoard[1][0]
            << " " << sudokuBoard[2][0]
            << "|"
            << sudokuBoard[3][0]
            << " " << sudokuBoard[4][0]
            << " " << sudokuBoard[5][0]
            << "|"
            << sudokuBoard[6][0]
            << " " << sudokuBoard[7][0]
            << " " << sudokuBoard[8][0]
            << endl;

        //Row 2
        cout << "2 "
            << sudokuBoard[0][0]
            << " " << sudokuBoard[1][1]
            << " " << sudokuBoard[2][1]
            << "|"
            << sudokuBoard[3][1]
            << " " << sudokuBoard[4][1]
            << " " << sudokuBoard[5][1]
            << "|"
            << sudokuBoard[6][1]
            << " " << sudokuBoard[7][1]
            << " " << sudokuBoard[8][1]
            << endl;

        //Row 3
        cout << "3 "
            << sudokuBoard[0][2]
            << " " << sudokuBoard[1][2]
            << " " << sudokuBoard[2][2]
            << "|"
            << sudokuBoard[3][2]
            << " " << sudokuBoard[4][2]
            << " " << sudokuBoard[5][2]
            << "|"
            << sudokuBoard[6][2]
            << " " << sudokuBoard[7][2]
            << " " << sudokuBoard[8][2]
            << endl;

        //Separator 
        cout << "   -----_-----_-----" << endl;

        //Row 4
        cout << "4 "
            << sudokuBoard[0][3]
            << " " << sudokuBoard[1][3]
            << " " << sudokuBoard[2][3]
            << "|"
            << sudokuBoard[3][3]
            << " " << sudokuBoard[4][3]
            << " " << sudokuBoard[5][3]
            << "|"
            << sudokuBoard[6][3]
            << " " << sudokuBoard[7][3]
            << " " << sudokuBoard[8][3]
            << endl;

        //Row 5
        cout << "5 "
            << sudokuBoard[0][4]
            << " " << sudokuBoard[1][4]
            << " " << sudokuBoard[2][4]
            << "|"
            << sudokuBoard[3][4]
            << " " << sudokuBoard[4][4]
            << " " << sudokuBoard[5][4]
            << "|"
            << sudokuBoard[6][4]
            << " " << sudokuBoard[7][4]
            << " " << sudokuBoard[8][4]
            << endl;
    
        //Row 6
        cout << "6 "
            << sudokuBoard[0][5]
            << " " << sudokuBoard[1][5]
            << " " << sudokuBoard[2][5]
            << "|"
            << sudokuBoard[3][5]
            << " " << sudokuBoard[4][5]
            << " " << sudokuBoard[5][5]
            << "|"
            << sudokuBoard[6][5]
            << " " << sudokuBoard[7][5]
            << " " << sudokuBoard[8][5]
            << endl;
        
        //Separator
        cout << "   -----_-----_-----" << endl;

        //Row 7
        cout << "7 "
            << sudokuBoard[0][6]
            << " " << sudokuBoard[1][6]
            << " " << sudokuBoard[2][6]
            << "|"
            << sudokuBoard[3][6]
            << " " << sudokuBoard[4][6]
            << " " << sudokuBoard[5][6]
            << "|"
            << sudokuBoard[6][6]
            << " " << sudokuBoard[7][6]
            << " " << sudokuBoard[8][6]
            << endl;

        //Row 8
        cout << "8 "
            << sudokuBoard[0][7]
            << " " << sudokuBoard[1][7]
            << " " << sudokuBoard[2][7]
            << "|"
            << sudokuBoard[3][7]
            << " " << sudokuBoard[4][7]
            << " " << sudokuBoard[5][7]
            << "|"
            << sudokuBoard[6][7]
            << " " << sudokuBoard[7][7]
            << " " << sudokuBoard[8][7]
            << endl;

        cout << "9 "
            << sudokuBoard[0][8]
            << " " << sudokuBoard[1][8]
            << " " << sudokuBoard[2][8]
            << "|"
            << sudokuBoard[3][8]
            << " " << sudokuBoard[4][8]
            << " " << sudokuBoard[5][8]
            << "|"
            << sudokuBoard[6][8]
            << " " << sudokuBoard[7][8]
            << " " << sudokuBoard[8][8]
            << endl
            << endl;

        getOption(sudokuBoard);
}

//Allows the user to interact and manipulate the game board

void interact()

{

    cout << "Options:" << endl

        << "  ? Show these instructions" << endl

        << "  D Display the board" << endl

        << "  X Edit one square" << endl

        << "  H Help show the possible values for one of the squares" 

        << endl

        << "  Q Save and Quit" << endl

        << endl;

    return getOption;
}

//gets the user's input

void getOption(char sudokuBoard[][9])

{

    char option;
    cout << "> ";
    cin >> option;

    if (option == 'x' || option == 'X')
        editSquare(sudokuBoard);

    else if (option == '?')
        interact();

    else if (option == 'd' || option == 'D')
        display(sudokuBoard);

    else if (option == 'h' || option == 'H')
        showValues();

    else if (option == 'q' || option == 'Q')
        writeFile(sudokuBoard);

    else
        cout << "ERROR: Invalid command";

    return;   
}

//edits one square with coordinates entered by the user

void editSquare(char sudokuBoard[][9])

{

    //Declare variables
    char letter;
    int number;
    int value = 0;

    //Get letter and number coordinates
    cout << "What are the coordinates of the square: ";
    cin >> letter >> number;

    //Converts letter to uppercase
    letter = toupper(letter);

    //if square is full, display "read only" message
    if (sudokuBoard[letter - 65][number - 1] != ' ')
    {
        cout << "ERROR: square \'" << letter
            << number << "\' is a read-only" << endl;
        getOption(sudokuBoard);
    }

    else
    {
        //get value to place in the coordinates
        cout << "what is the value at \'" << letter
            << number << "\': ";
        cin >> value;

        //makes sure value is within the right range
        if (value < 1 || value > 9)
        {
            cout << "Try Again: Value |'" << value << "| in square |'"
                << letter << number << "|' is invalud" << endl;
            cout << endl;
                getOption(sudokuBoard);

        }
        // Check for duplicate in column

        for (int row = 0; row < 9; ++row)
            if (sudokuBoard[row][number - 1] == value)
            {
                cout << "ERROR: square \'" << letter
                    << number << "\' you typed a duplicate number" << endl
                    << "Please try again" << endl;
            }


        // Check for duplicate in row

        for (int col = 0; col < 9; ++col)
            if (sudokuBoard[letter - 65][col] == value)
            {
                cout << "ERROR: square \'" << letter
                    << number << "\' you typed a duplicate number" << endl
                    << "Please try again" << endl;
            }

        cout << endl;
        sudokuBoard[letter - 65][number - 1] = value;
        getOption(sudokuBoard);
    }

    return;
}

//writes the content of the board to a file to be picked up later

void writeFile(char sudokuBoard[][9])

{

    //File output
    ofstream fout;
    char destinationFile[256];

    //user input
    cout << "What file would you like to write your board to: ";
    cin >> destinationFile;

    //Open destination file & error checking 
    fout.open(destinationFile);
    if (fout.fail())
    {
        cout << "Output file opening failed" << endl;
        exit(1);
    }

    else
        cout << "Board written successfully";

    //Write board to file
    for (int col = 0; col < 9; col++)
    {
        for (int row = 0; row < 9; row++)
        {
            if (sudokuBoard[row][col] == ' ')
            {
                sudokuBoard[row][col] = '0';
            }

            fout << sudokuBoard[row][col];

            //Makes sure it's a 9x9 grid
            if (row % 9 == 0)
            {
                fout << endl;
            }

        }
    }

    //close file
    fout.close();
}

//Show all the possible values for a given coordinates

void showValues()

{
    
    //variables
    char letter;
    int number;

    //letter/number coordinates
    cout << "What are the coordinates of the square: ";
    cin >> letter >> number;

    //Coinverts letter to uppercase
    letter = toupper(letter);

    return;
}

I am using Visual Studio 2019 for this.

Community
  • 1
  • 1
  • 1
    Please share the exact lines of code the errors are on. That helps us understand the problem more easily. – cigien Apr 29 '20 at 21:54
  • 2
    `void` functions *cannot* return values. Look for any function of that sort with a `return` having a value. `void interact()` tries to return something, so maybe the signature is wrong? – tadman Apr 29 '20 at 21:57
  • 1
    ***C4700 uninitialized local variable 'name' used*** Should be pretty obvious. – drescherjm Apr 29 '20 at 22:07
  • 1
    `return main();` is a bug. In c++ you are not permitted to call main. [https://stackoverflow.com/questions/2128321/can-main-function-call-itself-in-c](https://stackoverflow.com/questions/2128321/can-main-function-call-itself-in-c) – drescherjm Apr 29 '20 at 22:08
  • - cigien - the errors are for E0120 are on lines 50 and 277. C3861: 50 and C4447: 61. - Drescherjm - I did solve that problem on my code are well and thanks - tadman - – Dylan-Dang Apr 29 '20 at 22:40
  • @Dylan we can't see line numbers. Please indicate in the code with big comments like `// <---- error "error text" here` – JohnFilleau Apr 29 '20 at 23:25
  • // for the E0120 errors its the void interact() at the very end *return getOption* also the C2562 error has to do with the *getOption* and the void openingQuestion() at the end *return main()* and for error E0165 it is near the end of the void openingQuestion() *return exit()*. For the C3861 error it is also the *return main()* . The final one is the warning C4447 with the *int main* opening. I am sorry for doing a bad job commenting and stuff. This is my first time using this website – Dylan-Dang Apr 30 '20 at 00:10
  • in interact() you have `return getOption;` and `getOption` is a function however you are not calling it as a function and you are not passing the parameters. Although it does not matter because interact() can't return anything anyways since it is a `void` function. – drescherjm Apr 30 '20 at 00:16

1 Answers1

0

A couple notes to point out

first of all, you can not return the main function in your line

if (eResponse == 2) // 2 equals no
        {
            return main();
        }

next you have a lot of void function with return statements because the function is void you can not return anything

instead of

void getOption(char sudokuBoard[][9])

{

    char option;
    cout << "> ";
    cin >> option;

    if (option == 'x' || option == 'X')
        editSquare(sudokuBoard);

    else if (option == '?')
        interact();

    else if (option == 'd' || option == 'D')
        display(sudokuBoard);

    else if (option == 'h' || option == 'H')
        showValues();

    else if (option == 'q' || option == 'Q')
        writeFile(sudokuBoard);

    else
        cout << "ERROR: Invalid command";

    return;   
}

use

char getOption(char sudokuBoard[][9])

{

    char option;
    cout << "> ";
    cin >> option;

    if (option == 'x' || option == 'X')
        editSquare(sudokuBoard);

    else if (option == '?')
        interact();

    else if (option == 'd' || option == 'D')
        display(sudokuBoard);

    else if (option == 'h' || option == 'H')
        showValues();

    else if (option == 'q' || option == 'Q')
        writeFile(sudokuBoard);

    else
        cout << "ERROR: Invalid command";

    return option;   
}

go through your functions and ask yourself, do i need to return a value? if yes then change from void to what ever type you are using and if no then remove the return statement.

for your warning issue

char sudokuBoard[9][9];

is not being initialised.

you can do something like

char sudokuBoard[9][9] = {};
william_
  • 1,131
  • 6
  • 20