Quick if statement error

I am having some issues that i am sure is just a obvious problem if am missing. Simple if statements but the problem is that it only does the right thing for choice == 1. If the choice is equal to 1 or 2 it does nothing and just lets the user input a number over and over again.
EDIT: I did define choice as an int and i do have output that is suppose to show up after the if statements. I even tried putting just a simple hello output in the if choice == 2 but nothing happened.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
cin >> choice;
	if (choice == 0)
		exit (1);
	if(choice == 2)
	{
		loadgame.open("game.dat");
		if (loadgame.fail())
		{
			cout << "Input file opening failed.\n";
			exit(1);
		}

		while (!loadgame.eof())
		{
			for(y = 0; y < 10; y++) {
				x = 0;
				loadgame >> game_board[x][y];
				if (y == 9)
				{
					x++;
					y = 0;
				}
				if (x == 10)
					y = 10;
			}
		}
		loadgame.close();
	}
	if (choice == 1)
	{
		for(y = 0; y < 10; y++) {
			x = 0;
			game_board[x][y] = -1;
			if (y == 9)
			{
				x++;
				y = 0;
			}
			if (x == 10)
			{
				y = 10;
			}	
		}
	}
Last edited on
What type is choice?

Choice is type int.
Without seeing more of the code it is difficult to tell. What you have posted should work as long as the user inputs a number and not a letter. The only thing I can think of is that the input cin is getting an error condition from some previous input that has not been cleared.

Try this to see if that may be the case:
1
2
cin.clear();
cin >> choice;
Last edited on
I tried that with no success. The program is to create a sudoku game (https://wiki.ittc.ku.edu/ittc/EECS168:Lab10)
Here is what i have so far, i know there are logical errors and stuff like that, i just need to get past these if statements so i can see what it looks like so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void display_board (int board_par[][9]);
int main ()
{
	ifstream loadgame;
	int game_board[9][9];
	int choice, x = 0, y = 0, number, row, column, game_choice;
	char choice2;
	cout << "Welcome to Sudoku!" << endl;
	cout << "[1] Start a new game\n";
	cout << "[2] Load a saved game\n";
	cout << "[0] Exit the program\n";
	cin.clear();
	cin >> choice;
	if (choice == 0)
		exit (1);
	if(choice == 2)
	{
		loadgame.open("game.dat");
		if (loadgame.fail())
		{
			cout << "Input file opening failed.\n";
			exit(1);
		}

		while (!loadgame.eof())
		{
			for(y = 0; y < 10; y++) {
				x = 0;
				loadgame >> game_board[x][y];
				if (y == 9)
				{
					x++;
					y = 0;
				}
				if (x == 10)
					y = 10;
			}
		}
		loadgame.close();
	}
	if (choice == 1)
	{
		for(y = 0; y < 10; y++) {
			x = 0;
			game_board[x][y] = -1;
			if (y == 9)
			{
				x++;
				y = 0;
			}
			if (x == 10)
			{
				y = 10;
			}	
		}
	}
	cout << choice;
	
	do {
		display_board(game_board);
		cout << "[1] Enter a number on the board\n";
		cout << "[2] Save current game\n";
		cout << "[0] Quit current game\n";
		cin >> game_choice;
		
		if (game_choice == 2)
		{
			ofstream savegame;
			savegame.open ("game.dat");
			for(y = 0; y < 10; y++) {
				x = 0;
				savegame << game_board[x][y];
				if (y == 9)
				{
					x++;
					y = 0;
				}
				if (x == 10)
					y = 10;
			savegame.close();
			}
			cout << "Game progress successfully saved.\n";
		}
		if (game_choice =='1')
		{
			cout << "Enter the row number: ";
			cin >> row;
			cout << "Enter the column number: ";
			cin >> column;
			cout << "Enter the number you would like placed at row " << row << " column " << column << ": ";
			cin >> number;
			game_board[row][column] = number;
		}
		
	}while (game_choice != 0);
	
	return 0;
}
	
void display_board (int board_par[][9])
{
	cout << " 	1 2 3   4 5 6   7 8 9\n";
	int x = 0;
	for (int y = 0; y < 10; y++)
	{
		if (y == '9')
		{
			x++;
			y = 0;
		}
		if (y == 0)
			cout << x << "     ";
		if ((x == 4) || (x == 7))
			cout << "      --- --- --- | --- --- --- | --- --- ---";
		cout << "[";
		if (board_par[x][y] == -1)
			cout << "X";
		else 
			cout << board_par[x][y];
		cout << "] ";
		if ((y == 3) || (y == 6))
			cout << " | ";
		if (x == 10)
			y = 10;
	}
}
Your for loop to load/set the board in choice 1/2 is buggy. You reset x to zero at the start of every loop causing it to loop forever. Why don't you just use two for loops?
I noticed i do that a lot... That fixed the if (choice == 2) loop but the if (choice == 1) loop is still doing nothing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void display_board (int board_par[][9]);
int main ()
{
	ifstream loadgame;
	int game_board[9][9];
	int choice, x = 0, y = 0, number, row, column, game_choice;
	char choice2;
	cout << "Welcome to Sudoku!" << endl;
	cout << "[1] Start a new game\n";
	cout << "[2] Load a saved game\n";
	cout << "[0] Exit the program\n";
	cin.clear();
	cin >> choice;
	if (choice == 0)
		exit (1);
	if(choice == 2)
	{
		loadgame.open("game.dat");
		if (loadgame.fail())
		{
			cout << "Input file opening failed.\n";
			exit(1);
		}

		while (!loadgame.eof())
		{
			for(y = 0; y < 10; y++) {
				loadgame >> game_board[x][y];
				if (y == 9)
				{
					x++;
					y = 0;
				}
				if (x == 10)
					y = 10;
			}
		}
		loadgame.close();
	}
	if (choice == 1)
	{
		x = 0;
		for(y = 0; y < 10; y++) {
			game_board[x][y] = -1;
			if (y == 9)
			{
				x++;
				y = 0;
			}
			if (x == 10)
			{
				y = 10;
			}	
		}
	}
	
	do {
		display_board(game_board);
		cout << "\n[1] Enter a number on the board\n";
		cout << "[2] Save current game\n";
		cout << "[0] Quit current game\n";
		cin >> game_choice;
		
		if (game_choice == 2)
		{
			int x= 0;
			ofstream savegame;
			savegame.open ("game.dat");
			for(y = 0; y < 10; y++) {
				savegame << game_board[x][y];
				if (y == 8)
				{
					x++;
					y = 0;
				}
				if (x == 10)
					y = 10;
			savegame.close();
			}
			cout << "\n\nGame progress successfully saved.\n";
		}
		if (game_choice ==1)
		{
			cout << "Enter the row number: ";
			cin >> row;
			cout << "Enter the column number: ";
			cin >> column;
			cout << "Enter the number you would like placed at row " << row << " column " << column << ": ";
			cin >> number;
			game_board[row-1][column-1] = number;
		}
		
	}while (game_choice != 0);
	
	return 0;
}
	
void display_board (int board_par[][9])
{
	cout << "       1   2   3      4   5   6      7   8   9\n";
	int x = 0;
	for (int y = 0; y < 10; y++)
	{
		if (y == 9)
		{
			x++;
			y = 0;
			cout << endl;
		}
		if ((x == 6) && (y == 0))
			cout << "      --- --- ---  | --- --- ---  | --- --- ---\n";
		if ((x == 3) && (y == 0))
			cout << "      --- --- ---  | --- --- ---  | --- --- ---\n";
		if (x == 9){
			y = 10;
			break;
			}
		if (y == 0)
			cout << x + 1 << "     ";
		cout << "[";
		if (board_par[x][y] == -1)
			cout << "X";
		else 
			cout << board_par[x][y];
		cout << "] ";
		if ((y == 2) || (y == 5))
			cout << " | ";
		
	}
}
What do you mean by "doing nothing"? All it does is fill the array with -1s; It shouldn't print anything out.

Also, I *really* suggest you replace your strange for loop internals with another for loop, it would be a lot nicer to look at...
It asks for the choice input and it just continues to let the user input over and over. It is suppose to fill the array and then output the do-while loop (display the board and the options for the game).
Topic archived. No new replies allowed.