// Exercise A: This program print out the Sudoku Puzzle // and its solution in a Console window #include //--------------------------------------------------------------------------- int main( ) // The program does not take any input arguments { char filename[15] = "sudoku001.txt"; // To specify a different file // change filename here char str[81]; // stores the header string char ch; // stores current character read from file ifstream infile; // declaration for the input file variable infile.open(filename); // open the file infile.getline(str,81); // read the header line cout << "The puzzle is: \n\n"; while (infile.get(ch)) { // 3 possible actions: if (ch == '\n') // 1. output CR/LF cout << endl; // 2. 1-9 are given digits, just output else if ((ch >= '1') && (ch <= '9')) cout << ch; // 3. Anything else is unknown else cout << '-'; } infile.close(); // close the file - finished reading cout << "\n" << "The solution is: \n\n"; infile.open(filename); // opent he file again, this time for solution infile.getline(str,81); // skip the header line while (infile.get(ch)) { // Now 4 possibilities: if (ch == '\n') // 1. output CR/LF as is cout << endl; else if ((ch >= '1') && (ch <= '9')) // 2. 1-9 as given cout << ch; else if ((ch >= 'A') && (ch <= 'I')) // 3. A-I are solutions // translate 'A' as '1' etc cout << char(int(ch) - int('A') + int('1')); else cout << '-'; // 4. Anything else, output '-' } infile.close(); getchar(); return 0; }