2016-08-20 7 views
3

私は数独のためのプログラムを書こうとしています。数独は私の入力ファイルのためにうまくいく。しかし、私はコンパイラでファイルを入力するいくつかの変更を加えたいです。それは 'open'への呼び出しのための一致するメンバ関数のようなエラーをキャッチします。私の問題はI/Oファイルだと思うので、これは私のプログラムの一部です。どんな助けもありがとう!ありがとうございます!Sudokuでファイルを開くには?

#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; 
} 
+0

あなたは#include ですか? – Assimilater

+0

@Assimilater:はい、あります。 – pansoh

+0

http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrongそして、エラーは何ですか? – kfsone

答えて

2

あなたfilename変数はchar型を持ちます。これは、と1つの "文字"を格納できる単一の整数値です。

charのファイル名がfstreamコンストラクタにないと言うと、コンパイラは正しくなります。

おそらくchar[SOME_BUFFER_SIZE]、理想的にはstd::stringを意味します。あなたがstd::stringを使用して、C++ 03コンパイラに移動した場合、あなたはfstreamにそれを渡したときに歴史的な理由のために、c_str()を追加する必要がありますことを

は注意してください。

+0

私のエラーは "myfile.open(filename)"だと思います。私はそれを修正する方法がわかりません。私はC++ 11を使用しています。 – pansoh

+1

@pansohこの問題を解決する方法は、この回答にあるように 'char []'または 'std :: string'に変換します。あなたが使ったタイプは、* 1文字*でなければなりません。言い換えれば、あなたのファイル名は "a"または "b"ですが、 "hello.txt"ではなく複数の文字である可能性があります。 – Assimilater

+0

@Assimilater:もう1つの質問があります:なぜ私は "4x4" "9x9"入力に出力が出ますか?私が「9x9」を選択し、「4x4」入力を与えると無限に表示されます。 – pansoh