数独输入程序跳过提示

时间:2012-11-01 16:38:00

标签: c++ sudoku

我为我的计算机科学课程编写了一个程序,用于验证和解决.txt文件中的数独游戏,但我想更进一步,编写一个程序,使其易于输入和数独游戏。我相信你可以根据这段代码弄清楚文件的格式。我唯一的问题是最后一个cin被跳过了,这个选项对我很重要。任何见解都将受到赞赏!!

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct s {
s();
~s() {/*zzzz*/}
void show_grid();
void set (int &r, int &c, int &v) {g[r][c] = v;}
private:
int g[9][9];
};

//************************************************************************

void s::show_grid() {

//print game out to check it
cout << "  |  -------------------------------  |" << endl;
for (int k=0; k<81; k++) {
    if (k%3 == 0)
        cout << "  |";
    cout << "  " << g[k/9][k%9];
    if (k%9 == 8) {
        cout << "  |" << endl;
        if ((k/9)%3 == 2)
            cout << "  |  -------------------------------  |" << endl;
    }
}
cout << endl;
}

//************************************************************************

s::s() {

//initialize all elements to zero
for (int i=0; i<9; i++) {
    for (int j=0; j<9; j++) {
        g[i][j] = 0;
    }
}
}

//************************************************************************

void create_name (string &name) {

//append .txt extension LIKE IT OR NOT
string ext = name;
ext.erase(ext.begin(), ext.end() - 4);

if (ext.compare(".txt")!=0)
    name.append(".txt");
}

//************************************************************************

int main () {

s g;
string name;
string yon("");
int count = 0;
int row, col, val, rcv;
ofstream os;

cout << "Enter game file name: ";
cin >> name;

create_name(name);

//open and do typical checks
os.open(name.c_str());
if (os.fail()) {
    cerr << "Could not create " << name << ". Waaaah waaaaaaaaaah...\n\n";
    return 0;
}

//useful output (hopefully)
cout << "Enter grid coordinates and value as a 3-digit number,\n"
    << "from left to right, row by row.\n" 
    << "(e.g. 2 in first box would be 112)\n";

//take input as one int, to be user friendly
while (cin >> rcv && count < 81) {
    row = (rcv / 100) - 1;
    col = ((rcv / 10) % 10) - 1;
    val = rcv % 10;
    os << row << " " << col << " " << val << endl;
    g.set (row, col, val);
    count++;
}

os.close();

//From here down is broken, but it still compiles, runs, and works
cout << "Show grid input(y/n)?\n";
cin >> yon;

if (yon.compare("y")==0)
    g.show_grid();
else if (yon.compare("n")==0)
    cout << "Peace!\n";

return 0;
}

3 个答案:

答案 0 :(得分:1)

问题在于:

while (cin >> rcv && count < 81)

考虑count==81时会发生什么:首先,rcv将从cin输入,然后才会将条件count < 81评估为假。循环将停止,rcv的值将被忽略。所以你有效地阅读了一个输入太多了。

您应该更改评估顺序,以便首先检查count

while (count < 81 && cin >> rcv)

修改

根据您上面的评论,您实际上希望阅读少于81个值。在这种情况下,我建议用户输入一个特殊值(例如0)来终止循环。您只需添加if (rcv==0) break;即可。如果您只是输入一个无效值,那么cin流将处于失败状态,并且进一步输入将不会成功。

答案 1 :(得分:0)

cin >> yon

实际上仍然读入一个变量,它只读取while循环发现为false的变量。当while循环条件返回false时,忽略rcv,因此该数字保留在输入流中,等待下一个cin语句。当yon被调用时,rcv的数字被读入yon,给你一些奇怪的错误。

最好使用interjay的方法:

while (count < 81 && cin >> rcv)

答案 2 :(得分:0)

尝试类似:

//useful output (hopefully)
cout << "Enter grid coordinates and value as a 3-digit number,\n"
    << "from left to right, row by row.\n" 
    << "(e.g. 2 in first box would be 112)\n"
    << "or Z to end the loop\n";  // 1

//take input as one int, to be user friendly
while (count < 81 && cin >> rcv ) { // 2
    row = (rcv / 100) - 1;
    col = ((rcv / 10) % 10) - 1;
    val = rcv % 10;
    os << row << " " << col << " " << val << endl;
    g.set (row, col, val);
    count++;
}

if(!std::cin) {  // 3
  std::cin.clear();
  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

1)让用户知道他可以输入无效的字符。它不一定是Z,实际上任何非数字字符都可以。

2)按照&&的顺序修复一个错误。

3)如果std::cin处于错误状态,请清除错误并忽略Z