3

I am trying to write a program for Sudoku. The Sudoku runs well for my input file. But I want to make some changes that input the file in the compiler. It catches the error like no matching member function for call to 'open'. This is just part of my program because I think my problem is the I/O file. Any help is appreciated! Thanks you!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

int main()
{
char filename;
ifstream myfile;
//int row,column;
int choice;
cout << "Enter the desired sudoku 4 for (4x4) or 9 for (9x9) : \n";
cin >> choice;

if(choice == 9) {

    for(int row = 0; row < 9; row++) // iterating the loop to assign initial dummy values
    {
        for(int column = 0; column < 9; column++)
        {
            sudoku[row][column] = 0; // assigining zeros
        }
    }
    cout << "Enter the filename:" << endl;
    cin >> filename;
    myfile.open(filename); // opening the file mentioned
    cout << "The values in the file are :" << endl;
    if (myfile.is_open())
    {
        while (!myfile.eof())
        {
            for(int row = 0; row < 9; row++) // iterating the loope to get the values form the file
            {
                for(int column = 0; column < 9; column++)
                {
                    myfile >> sudoku[row][column]; // assigning the values to the grid
                    cout << sudoku[row][column] << endl; // printing the grid
                }
            }
        }
    }
    myfile.close(); // closing the file
    solvesudoku(0,0);//We start solving the sudoku.
}

else if(choice == 4) {

    for(int row = 0; row < 4; row++) // iterating the loop to assign initial dummy values
    {
        for(int column = 0; column < 4; column++)
        {
            sudoku1[row][column] = 0; // assigining zeros
        }
    }
    cout << "Enter the filename:" << endl;
    cin >> filename;
    myfile.open(filename); // opening the file mentioned
    cout << "The values in the file are :" << endl;
    if (myfile.is_open())
    {
        while (!myfile.eof())
        {
            for(int row = 0; row < 4; row++) // iterating the loope to get the values form the file
            {
                for(int column = 0; column < 4; column++)
                {
                    myfile >> sudoku1[row][column]; // assigning the values to the grid
                    cout << sudoku1[row][column] << endl; // printing the grid
                }
            }
        }
    }
    myfile.close(); // closing the file
    solsudoku(0,0);//We start solving the sudoku.
}
else {
    cout << "Invalid Choice..!!!";
 }
return 0;
}
Assimilater
  • 944
  • 14
  • 33
pansoh
  • 47
  • 2
  • 7

1 Answers1

2

Your filename variable has type char. That is a single integral value that can store one "character".

The compiler is correct when it says that no fstream constructor takes a filename of type char.

You probably meant char[SOME_BUFFER_SIZE], or ideally std::string.

Note that if you use std::string and you move to a C++03 compiler, you'll have to append c_str() when you pass it to the fstream, for historical reasons.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I guess my error is on "myfile.open(filename)"; I am not sure how to fix it. I am using c++11. – pansoh Aug 20 '16 at 17:31
  • 1
    @pansoh The way to "fix it" it is convert it to `char[]` or `std::string` as this answer says. The type you have used can only be *one character*. in other words your file name can be "a" or "b" but not "hello.txt" because that's more than one character – Assimilater Aug 21 '16 at 14:34
  • @Assimilater: I see but I have one more question: why I choose "4x4" and give a "9x9" input will have an output comes out? If I choose "9x9" and give a "4x4" input will show infinite. – pansoh Aug 21 '16 at 17:07
  • @pansoh: No idea, since you didn't show us what `sudoku` is. You're probably overwriting that buffer too. Your error checking is minimal in most places and wrong in others. – Lightness Races in Orbit Aug 21 '16 at 17:19
  • @LightnessRacesinOrbit: I have updated the whole program. Please have a look and give me some advices. Thank you! – pansoh Aug 21 '16 at 17:39
  • @pansoh: Please ask a specific question, and post [MCVE]s, not "whole programs"! – Lightness Races in Orbit Aug 21 '16 at 19:38
  • @LightnessRacesinOrbit: My question is when I choose "9x9" and give a "4x4" input will have an output comes out? The example is like the images show: http://i.stack.imgur.com/lJMtu.png, http://i.stack.imgur.com/HReQI.png, http://i.stack.imgur.com/iNS7z.png. – pansoh Aug 21 '16 at 22:06
  • 2
    @pansoh it isn't fair to the answerers to change your question in this format. A single question isn't meant to be a one on one coaching session on stack overflow. It is meant to answer a specific question. – Assimilater Aug 23 '16 at 14:20
  • 1
    @pansoh If you post a separate question I'll have an answer for your question – Assimilater Aug 23 '16 at 14:25