-
input control question
In the first run when i enter invalid input (to choose an option in a menu)
its working fine
but as soon as i define a board and place mines
no matter what wrong input i will put
its going to the second option(placing mines option) instead of just printing the menu again
why??
Code:
#include <stdio.h>
#define N 9
int checkEndOfGame(char board[N][N],int size);
void sprint(char board[N][N],int size);
int same_digit(char ch_cords[40],int k);
int is_valid(const char input[40],int leng);
void mainPlayGame(char board[N][N]);
int playGame(char board[N][N],int size);
void printBoard(char board[N][N],int size);
int removeMine(char board[N][N],int size,int x,int y);
void mainMenu();
int unCover(char board[N][N],int size,int x,int y);
int main()
{
char board[N][N];
mainPlayGame(board);
printf("bye, please press enter to exit!\n");
getchar();
return 0;
}
void mainPlayGame(char board[N][N])
{
int x_cord,y_cord,wins=0,loses=0;
char input[40];
char input2[40];
int flag2=0;
int opt,leng,size=-1;
int index,kndex;
char ch_cords[40];
int tr;
int cords[8][2];
int tndex,result;
int i,k,j,ch,l,x_rem,y_rem;
for(index=0;index<N;index++)
{
for(kndex=0;kndex<N;kndex++)
{
board[index][kndex]=' ';
}
}
do
{///
mainMenu();
scanf("%d",&opt);
while((l= getchar()) != '\n');
if (opt==1)
{
for(index=0;index<N;index++)
{
for(kndex=0;kndex<N;kndex++)
{
board[index][kndex]=' ';
}
}
printf("enter board size [1..9] (no input control is needed):\n");
scanf("%d",&size);
}
if (opt==2)
{
flag2=0;
if (size==-1)
{
printf("you must choose board size first!\n");
}
else
{
printf("enter x,y of the new mines (x1,y1)(x2,y2)...(xn,yn)\n");
for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
k=0;
for(index=0;index<=i;index++)
{
if(input2[index]!=' ')
{
input[k]=input2[index];
k++;
}
}
leng=k;//same digit input check
if (is_valid(input,leng))
{
for(index=0;index<leng;index++)
{
if (((input[index])>='0')&&((input[index])<='9')&&((input[index+1])>='0')&&((input[index+1])<='9'))
{
flag2=1;
}
}
k=0;
if (flag2!=1){
for(index=0;index<leng;index++)
{
if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')'))
{
ch_cords[k]=input[index];
k++;
}
}
k--;
tndex=0;
for(index=0;index<=leng;index++)
{
if((input[index]==')'))
{
tndex++;
}
}
j=0;
for(index=0;index<tndex;index++)
{
for(kndex=0;kndex<2;kndex++)
{
tr=ch_cords[j]-'0';
cords[index][kndex]=tr;
j++;
}
}
result=same_digit(ch_cords,tndex); //i call the function here
if (result==0)
{
for(index=0;index<tndex;index++)
{
x_cord=cords[index][0];
y_cord=cords[index][1];
if((x_cord>size)||(y_cord>size)||(y_cord<0)||(x_cord<0))
{
//out of bound
printf("(%d,%d) is out of range\n",x_cord,y_cord);
}
else
{ //excepted
if (board[y_cord-1][x_cord-1]==' ')
{
board[y_cord-1][x_cord-1]='*';
printf("(%d,%d) mine inserted\n",x_cord,y_cord);
}
else
{
printf("(%d,%d) already full\n",x_cord,y_cord);
}
}
}
}
}
else
{
printf("wrong input\n");
}
}
else
{
printf("wrong input\n");
}
}
}//end if opt 2///
if (opt==3)
{
if (size==-1)
{
printf("you must choose board size first!\n");
}
else
{
printf("enter x,y of the mine to remove (no input control is needed):\n");
for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
ch_cords[0]=input2[0];
ch_cords[2]=input2[2];
x_rem=ch_cords[0]-'0';
y_rem=ch_cords[2]-'0';
if (board[y_rem-1][x_rem-1]!='*')
{
printf("wrong input or no mine\n");
}
else
{
printf("mine removed\n");
board[y_rem-1][x_rem-1]=' ';
}
}
}
if (opt==4)
{
if (size==-1)
{
printf("you must choose board size first!\n");
}
else
{
printBoard(board,size);
}
}
if (opt==5)
{
tr=playGame(board,size);
if (tr==1){
wins++;
}
if (tr==0){
loses++;
}
}
if (opt==0)
{
printf("you played %d games(s):\n",wins+loses);
printf("you won %d\n",wins);
printf("you lost %d\n",loses);
printf("bye!");
}
}while(opt!=0);
}
int same_digit(char ch_cords[40],int k)
{
int index;
char ch;
ch=ch_cords[0];
for (index=0;index<=k;index++)
{
if (ch_cords[index]!=ch)
{
return 0;
}
}
if(k>2)
{
return 1;
}
else
{
return 0;
}
}//end same digit
void mainMenu()
{
printf("--------------------------\n");
printf("welcome to the game\n");
printf("1. choose board size\n");
printf("2. place mines\n");
printf("3. remove mines\n");
printf("4. show mines\n");
printf("5. start the game\n");
printf("0. exit\n");
printf("please enter your choice (input control is needed): \n");
}
void printBoard(char board[N][N],int size)
{
int index,kndex;
for(index=0;index<size;index++)
{
printf("+");
for(kndex=0;kndex<size;kndex++)
{
printf("-+");
}
printf("\n");
printf("|");
for(kndex=0;kndex<size;kndex++)
{
printf("%c|",board[index][kndex]);
}
printf("\n");
}
printf("+");
for(kndex=0;kndex<size;kndex++)
{
printf("-+");
}
printf("\n");
}
int removeMine(char board[N][N],int size,int x,int y)
{
if (board[y-1][x-1]=='*')
{
board[y-1][x-1]=' ';
printf("mine removed");
return 1;
}
else
{
printf("wrong input or no mine");
return 0;
}
}
int unCover(char board[N][N],int size,int x,int y){
int count=0;
if (board[y-1][x-1]=='*'){
printf("you lost\n");
board[y-1][x-1]='X';
return 0;
}
else
{
if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
count++;
}
if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
count++;
}
if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
count++;
}
if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
count++;
}
if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
count++;
}
if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
count++;
}
if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
count++;
}
if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
count++;
}
board[y-1][x-1]='0' +count;
return 1;
}
}
int is_valid(const char input[40],int leng)
{
int flag=3;
int i=1;
int index;
for(index=0;index<leng;index++)
{
if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')'))
{
if (((input[1])<'0')||((input[1])>'9'))
{
return 0;
}
}
}
if (input[0] != '(')
{
flag=0;
}
if (((input[1])<'0')||((input[1])>'9'))
{
flag=0;
}
i++;
while((input[i]<='9')&&(input[i]>='0'))
{
i++; //increasing the address by1
} //it wiil get char '2'
if (input[i] == ',')
{ //'2' differs ','
if (((input[i+1])<'0')||((input[i+1])>'9'))
{
flag=0;
}
i++;
while((input[i]<='9')&&(input[i]>='0'))
{
input++; //increasing the address by1
}
if (input[i]==')')
{
i++;
}
else
flag=0;
}
else
{
flag=0;
}
if (flag ==0)
{
return 0;
}
while (input[i]!= '\0')
{
if (input[i] != '(')
{
flag=0;
}
if (((input[i+1])<'0')||((input[i+1])>'9'))
{
flag=0;
}
i++;
while((input[i]<='9')&&(input[i]>='0'))
{
i++; //increasing the address by1
} //it wiil get char '2'
if (input[i] == ',')
{ //'2' differs ','
if (((input[i+1])<'0')||((input[i+1])>'9'))
{
flag=0;
}
i++;
while((input[i]<='9')&&(input[i]>='0'))
{
i++; //increasing the address by1
}
if (input[i]==')')
{
i++;
}
else
flag=0;
}
else
{
flag=0;
}
if (flag ==0)
{
return 0;
}
}
return 1;
}
int checkEndOfGame(char board[N][N],int size){
int index,kndex;
for (index=0;index<size;index++){
for (kndex=0;kndex<size;kndex++){
if (board[index][kndex]==' '){
return 0;
}
}
}
return 1;
}
void sprint(char board[N][N],int size)
{
int index,kndex;
for(index=0;index<size;index++)
{
printf("+");
for(kndex=0;kndex<size;kndex++)
{
printf("-+");
}
printf("\n");
printf("|");
for(kndex=0;kndex<size;kndex++)
{
if ((board[index][kndex]=='*')||(board[index][kndex]==' '))
{
printf("?|");
}
else
{
printf("%c|",board[index][kndex]);
}
}
printf("\n");
}
printf("+");
for(kndex=0;kndex<size;kndex++)
{
printf("-+");
}
printf("\n");
}
int playGame(char board[N][N],int size){
int x_rem,y_rem,i,ch,kent,wins=0,loses=0,end,tr;
char input2[40];
if (size==-1)
{
printf("you must choose board size first!\n");
}
else
{
do
{
printf("enter a square x,y to uncover : (no input control is needed):\n");
for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
x_rem=input2[0]-'0';
y_rem=input2[2]-'0';
kent=unCover(board,size,x_rem,y_rem);
if (kent==0){
end=1;
loses++;
printBoard(board,size);
}
else{
tr=checkEndOfGame(board,size);
if (tr==0)
{
sprint(board,size);
}
else
{
printBoard(board,size);
end=1;
printf("all mines were uncovered - you won\n");
wins++;
}
}
}while(end!=1);
}
if (wins==1){
return 1;
}
{
return 0;
}
}
-
Could you be a little more specific as to how you produced the bug? I was just running through the program in the console (as well as the debugger) and I could not see any problems...
-
there is no menu
when you run the program??
-
Well when I run the program, I get the menu, so I choose the board size, say 3, then I get the menu again, so I place mines, (2,2) (2,3), then I get the menu again, so I choose show mines, and it does, and so on. Seems to work ok.
Code:
--------------------------
welcome to the game
1. choose board size
2. place mines
3. remove mines
4. show mines
5. start the game
0. exit
please enter your choice (input control is needed):
1
enter board size [1..9] (no input control is needed):
3
--------------------------
welcome to the game
1. choose board size
2. place mines
3. remove mines
4. show mines
5. start the game
0. exit
please enter your choice (input control is needed):
2
enter x,y of the new mines (x1,y1)(x2,y2)...(xn,yn)
(2,2) (2,3)
(2,2) mine inserted
(2,3) mine inserted
--------------------------
welcome to the game
1. choose board size
2. place mines
3. remove mines
4. show mines
5. start the game
0. exit
please enter your choice (input control is needed):
4
+-+-+-+
| | | |
+-+-+-+
| |*| |
+-+-+-+
| |*| |
+-+-+-+
--------------------------
welcome to the game
1. choose board size
2. place mines
3. remove mines
4. show mines
5. start the game
0. exit
please enter your choice (input control is needed):
-
if you have a menu
input "dfgdfgdfgdf" enter
it will type the menu again
but if you
input "1" enter (choosing board size)
input "3" enter (board size is 3)
input "2" enter (putting mines in the board)
input "(2,2)" enter (put mine in place 2,2 )
then you get the main menu again
but this time
you can type what ever you want no matter how wrong input it is
and it will go to option 2 (placing mines option)
-
Your bug does not seem to work for me...
Code:
--------------------------
welcome to the game
1. choose board size
2. place mines
3. remove mines
4. show mines
5. start the game
0. exit
please enter your choice (input control is needed):
ddasdasd
--------------------------
welcome to the game
1. choose board size
2. place mines
3. remove mines
4. show mines
5. start the game
0. exit
please enter your choice (input control is needed):
1
enter board size [1..9] (no input control is needed):
3
--------------------------
welcome to the game
1. choose board size
2. place mines
3. remove mines
4. show mines
5. start the game
0. exit
please enter your choice (input control is needed):
2
enter x,y of the new mines (x1,y1)(x2,y2)...(xn,yn)
(2,2)
(2,2) mine inserted
--------------------------
welcome to the game
1. choose board size
2. place mines
3. remove mines
4. show mines
5. start the game
0. exit
please enter your choice (input control is needed):
4
+-+-+-+
| | | |
+-+-+-+
| |*| |
+-+-+-+
| | | |
+-+-+-+
--------------------------
welcome to the game
1. choose board size
2. place mines
3. remove mines
4. show mines
5. start the game
0. exit
please enter your choice (input control is needed):
-
after you place a mine
type a wrong input
no matter what wrong input you enter
it will not print the menu again but will go to option 2
this is a print screen of the problem
http://img230.imageshack.us/img230/6615/46430691ds0.gif
-
You have two scanf()s. After the first one, you empty the input buffer. Do the same thing after the second scanf() and see if that fixes it. You should also be checking, on both scanf()s, that you actually got valid data from the user.
-
As Dino has pointed out, you have a big problem in that you are not checking your scanf for valid input. The problem you have created is that you get integer 2 into 'opt' the first time around, then you enter invalid input (scanf was looking for integers with %d, not characters) so the call to scanf is not successful. Since you don't check the return value of scanf, you have no way of doing anything about the failure. So what happens is that the previous value, 2, is still in the variable 'opt' That is why you keep getting that portion of the program. If you set the size for 3, then chose option 4, 'show mines' and then typed in invalid input, sdjfhjsdhfj, you would get the show mines screen over and over, because it was the last valid integer into the variable 'opt'.
Have a look at this, for some ideas on how you might get better control over user input. Assume the user is going to give you garbage input, and control it. Don't allow for any input that you don't want. Make the user give you the input you expect.
-
i did that
i put this line after the second scanf too
while((l= getchar()) != '\n');
i still get the same resolt
http://img258.imageshack.us/img258/2218/88578515ab6.gif
Code:
#include <stdio.h>
#define N 9
int checkEndOfGame(char board[N][N],int size);
void sprint(char board[N][N],int size);
int same_digit(char ch_cords[40],int k);
int is_valid(const char input[40],int leng);
void mainPlayGame(char board[N][N]);
int playGame(char board[N][N],int size);
void printBoard(char board[N][N],int size);
int removeMine(char board[N][N],int size,int x,int y);
void mainMenu();
int unCover(char board[N][N],int size,int x,int y);
int main()
{
char board[N][N];
mainPlayGame(board);
printf("bye, please press enter to exit!\n");
getchar();
return 0;
}
void mainPlayGame(char board[N][N])
{
int x_cord,y_cord,wins=0,loses=0;
char input[40];
char input2[40];
int flag2=0;
int opt,leng,size=-1;
int index,kndex;
char ch_cords[40];
int tr;
int cords[8][2];
int tndex,result;
int i,k,j,ch,l,x_rem,y_rem;
for(index=0;index<N;index++)
{
for(kndex=0;kndex<N;kndex++)
{
board[index][kndex]=' ';
}
}
do
{///
mainMenu();
scanf("%d",&opt);
while((l= getchar()) != '\n');
if (opt==1)
{
for(index=0;index<N;index++)
{
for(kndex=0;kndex<N;kndex++)
{
board[index][kndex]=' ';
}
}
printf("enter board size [1..9] (no input control is needed):\n");
scanf("%d",&size);
while((l= getchar()) != '\n');
}
if (opt==2)
{
flag2=0;
if (size==-1)
{
printf("you must choose board size first!\n");
}
else
{
printf("enter x,y of the new mines (x1,y1)(x2,y2)...(xn,yn)\n");
for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
k=0;
for(index=0;index<=i;index++)
{
if(input2[index]!=' ')
{
input[k]=input2[index];
k++;
}
}
leng=k;//same digit input check
if (is_valid(input,leng))
{
for(index=0;index<leng;index++)
{
if (((input[index])>='0')&&((input[index])<='9')&&((input[index+1])>='0')&&((input[index+1])<='9'))
{
flag2=1;
}
}
k=0;
if (flag2!=1){
for(index=0;index<leng;index++)
{
if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')'))
{
ch_cords[k]=input[index];
k++;
}
}
k--;
tndex=0;
for(index=0;index<=leng;index++)
{
if((input[index]==')'))
{
tndex++;
}
}
j=0;
for(index=0;index<tndex;index++)
{
for(kndex=0;kndex<2;kndex++)
{
tr=ch_cords[j]-'0';
cords[index][kndex]=tr;
j++;
}
}
result=same_digit(ch_cords,tndex); //i call the function here
if (result==0)
{
for(index=0;index<tndex;index++)
{
x_cord=cords[index][0];
y_cord=cords[index][1];
if((x_cord>size)||(y_cord>size)||(y_cord<0)||(x_cord<0))
{
//out of bound
printf("(%d,%d) is out of range\n",x_cord,y_cord);
}
else
{ //excepted
if (board[y_cord-1][x_cord-1]==' ')
{
board[y_cord-1][x_cord-1]='*';
printf("(%d,%d) mine inserted\n",x_cord,y_cord);
}
else
{
printf("(%d,%d) already full\n",x_cord,y_cord);
}
}
}
}
}
else
{
printf("wrong input\n");
}
}
else
{
printf("wrong input\n");
}
}
}//end if opt 2///
if (opt==3)
{
if (size==-1)
{
printf("you must choose board size first!\n");
}
else
{
printf("enter x,y of the mine to remove (no input control is needed):\n");
for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
ch_cords[0]=input2[0];
ch_cords[2]=input2[2];
x_rem=ch_cords[0]-'0';
y_rem=ch_cords[2]-'0';
if (board[y_rem-1][x_rem-1]!='*')
{
printf("wrong input or no mine\n");
}
else
{
printf("mine removed\n");
board[y_rem-1][x_rem-1]=' ';
}
}
}
if (opt==4)
{
if (size==-1)
{
printf("you must choose board size first!\n");
}
else
{
printBoard(board,size);
}
}
if (opt==5)
{
tr=playGame(board,size);
if (tr==1){
wins++;
}
if (tr==0){
loses++;
}
}
if (opt==0)
{
printf("you played %d games(s):\n",wins+loses);
printf("you won %d\n",wins);
printf("you lost %d\n",loses);
printf("bye!");
}
}while(opt!=0);
}
int same_digit(char ch_cords[40],int k)
{
int index;
char ch;
ch=ch_cords[0];
for (index=0;index<=k;index++)
{
if (ch_cords[index]!=ch)
{
return 0;
}
}
if(k>2)
{
return 1;
}
else
{
return 0;
}
}//end same digit
void mainMenu()
{
printf("--------------------------\n");
printf("welcome to the game\n");
printf("1. choose board size\n");
printf("2. place mines\n");
printf("3. remove mines\n");
printf("4. show mines\n");
printf("5. start the game\n");
printf("0. exit\n");
printf("please enter your choice (input control is needed): \n");
}
void printBoard(char board[N][N],int size)
{
int index,kndex;
for(index=0;index<size;index++)
{
printf("+");
for(kndex=0;kndex<size;kndex++)
{
printf("-+");
}
printf("\n");
printf("|");
for(kndex=0;kndex<size;kndex++)
{
printf("%c|",board[index][kndex]);
}
printf("\n");
}
printf("+");
for(kndex=0;kndex<size;kndex++)
{
printf("-+");
}
printf("\n");
}
int removeMine(char board[N][N],int size,int x,int y)
{
if (board[y-1][x-1]=='*')
{
board[y-1][x-1]=' ';
printf("mine removed");
return 1;
}
else
{
printf("wrong input or no mine");
return 0;
}
}
int unCover(char board[N][N],int size,int x,int y){
int count=0;
if (board[y-1][x-1]=='*'){
printf("you lost\n");
board[y-1][x-1]='X';
return 0;
}
else
{
if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
count++;
}
if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
count++;
}
if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
count++;
}
if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
count++;
}
if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
count++;
}
if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
count++;
}
if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
count++;
}
if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
count++;
}
board[y-1][x-1]='0' +count;
return 1;
}
}
int is_valid(const char input[40],int leng)
{
int flag=3;
int i=1;
int index;
for(index=0;index<leng;index++)
{
if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')'))
{
if (((input[1])<'0')||((input[1])>'9'))
{
return 0;
}
}
}
if (input[0] != '(')
{
flag=0;
}
if (((input[1])<'0')||((input[1])>'9'))
{
flag=0;
}
i++;
while((input[i]<='9')&&(input[i]>='0'))
{
i++; //increasing the address by1
} //it wiil get char '2'
if (input[i] == ',')
{ //'2' differs ','
if (((input[i+1])<'0')||((input[i+1])>'9'))
{
flag=0;
}
i++;
while((input[i]<='9')&&(input[i]>='0'))
{
input++; //increasing the address by1
}
if (input[i]==')')
{
i++;
}
else
flag=0;
}
else
{
flag=0;
}
if (flag ==0)
{
return 0;
}
while (input[i]!= '\0')
{
if (input[i] != '(')
{
flag=0;
}
if (((input[i+1])<'0')||((input[i+1])>'9'))
{
flag=0;
}
i++;
while((input[i]<='9')&&(input[i]>='0'))
{
i++; //increasing the address by1
} //it wiil get char '2'
if (input[i] == ',')
{ //'2' differs ','
if (((input[i+1])<'0')||((input[i+1])>'9'))
{
flag=0;
}
i++;
while((input[i]<='9')&&(input[i]>='0'))
{
i++; //increasing the address by1
}
if (input[i]==')')
{
i++;
}
else
flag=0;
}
else
{
flag=0;
}
if (flag ==0)
{
return 0;
}
}
return 1;
}
int checkEndOfGame(char board[N][N],int size){
int index,kndex;
for (index=0;index<size;index++){
for (kndex=0;kndex<size;kndex++){
if (board[index][kndex]==' '){
return 0;
}
}
}
return 1;
}
void sprint(char board[N][N],int size)
{
int index,kndex;
for(index=0;index<size;index++)
{
printf("+");
for(kndex=0;kndex<size;kndex++)
{
printf("-+");
}
printf("\n");
printf("|");
for(kndex=0;kndex<size;kndex++)
{
if ((board[index][kndex]=='*')||(board[index][kndex]==' '))
{
printf("?|");
}
else
{
printf("%c|",board[index][kndex]);
}
}
printf("\n");
}
printf("+");
for(kndex=0;kndex<size;kndex++)
{
printf("-+");
}
printf("\n");
}
int playGame(char board[N][N],int size){
int x_rem,y_rem,i,ch,kent,wins=0,loses=0,end,tr;
char input2[40];
if (size==-1)
{
printf("you must choose board size first!\n");
}
else
{
do
{
printf("enter a square x,y to uncover : (no input control is needed):\n");
for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i)
{
input2[i] = ch;
}
input2[i] = '\0';
x_rem=input2[0]-'0';
y_rem=input2[2]-'0';
kent=unCover(board,size,x_rem,y_rem);
if (kent==0){
end=1;
loses++;
printBoard(board,size);
}
else{
tr=checkEndOfGame(board,size);
if (tr==0)
{
sprint(board,size);
}
else
{
printBoard(board,size);
end=1;
printf("all mines were uncovered - you won\n");
wins++;
}
}
}while(end!=1);
}
if (wins==1){
return 1;
}
{
return 0;
}
}
-
Your problem is with the failed call to scanf.
You need to deal with that failure. If scanf fails, the OLD value in the variable 'opt' remains the same. It never gets overwritten. What makes scanf fail? In your case, when it is looking for an int, and you give it characters, like 'oshaashdj', then it fails. Since you don't check the return value, it fails 'silently,' though you can see the effect as a bug in your program.
-
how do i deal with it??
i cant use strtol()
-
Why can't you use strtol? School project?
Look at your documentation for scanf then. If you have to use it, know that it returns a value which corresponds to the number of input items successfully matched and assigned. So in this case, you would be looking for a return value of 1. If it did not equal 1, then you would assume the user put in garbage, and you need to ask them nicely for a proper number.
Code:
int input;
....
input = scanf("%d", &size);
if(input != 1) { /* error */
/* Do something */
}
-
yes its a assignment project i cant use it.
if the input is wrong i just want it
to show the menu again
Code:
scanf("%d",&opt);
while((l= getchar()) != '\n');
if ((opt<0)||(opt>9))
{
//do nothing so it will show the menu again
}
else
{
//go to the program
}
like this?
-
But that still does not deal with the fact that scanf failed, and the OLD value is still in 'opt' .
Code:
if ((opt<0)||(opt>9))
The above will never be true because the previous value will be in 'opt', which is the last correct integer the user entered.
Check the return value for scanf like I showed you. If it does not return 1 (for the one integer you are asking for) then tell the user he or she did not enter the input correctly and then send them back to the menu to try again.