Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <stdio.h>
#define N 9
void mainMenu();
int main() { char input[40]; char input2[40]; char board[N][N]; mainPlayGame(board); printf("bye, please press enter to exit!\n"); getchar(); return 0; }
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
int index,kndex; char input[40]; char ch_cords[40]; char input2[40]; int tr; int cords[8][2]; int tndex;
int i,k,j,ch,l;
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
} } {
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");
75 76 77 78 79 80
Share
printf("0. exit\n"); printf("please enter your choice (input control is needed): \n");
3. 01-04-2009#2
laserlight
C++ Witch Join Date Oct 2003 Location Singapore Posts 16,206
how to do tat no matter what wrong input i will give it,it will print the menu again only once?? "Flush" the input buffer after you read the option. This is covered by the FAQ, and in fact it has been shown to you in at least one previous thread.
Share
C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
4. 01-04-2009#3
transgalactic2
Share
5. 01-04-2009#4
transgalactic2
i need it to print it again only once no matter what illegal input here is what i get http://img49.imageshack.us/my.php?image=27465407kf1.gif maybe is because of the scanf ??
Share
6. 01-04-2009#5
transgalactic2
is there a way ??
Share
7. 01-04-2009#6
laserlight
C++ Witch Join Date Oct 2003 Location Singapore Posts 16,206
cant use flush What do you mean? If you are not allowed to "flush" the input buffer as described in the FAQ, then your problem cannot be solved, because to solve it is against the rules imposed on you. If you think that I am recommending the use of fflush(), then you have not read the FAQ.
Share
C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
8. 01-04-2009#7
Adak
This is one way to do what you want, but note that it uses the non-standard header file, conio.h, to provide unbuffered input. Other options include printing up the whole menu except the one line that shows them the choice. In case of error, a while loop can just reprint the last line and scanf(), again. So your bad entry would look like this: Your Choice [1-4]: 5 <enter> Your Choice [1-4]: 2 <enter> etc. So only that one line would be reprinted, for each bad entry, not the whole menu. You could also just print out 40 newlines, which would blank the screen out, and just reprint the whole menu (seems odd, but it works). Anyway, here's how you make people who strictly love the C standard, faint in their tracks: Code:
To plain text
9 1 0 1 1 1 2 1 } 3
choice = menu(); printf("\n Your Choice Was %d ", choice); Program Complete - Press Enter When Ready \n");
printf("\n\n\t
int menu(void)
1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2
printf("\n\t\t %c%c%c Welcome to the Main Menu %c%c%c\n\n\n", a1,a2,a3, a3,a2,a1); printf("\t Please Choose One of the Following Choices: \n\n"); printf("\t 1) Play a Game \n"); printf("\t 2) View The Highest Scores \n"); printf("\t 3) View All Scores \n"); printf("\t 4) Explore Game Options and Characters \n"); printf("\n do { number = getche() - 48; //scanf("%d", &number); getch(); if(number < 0) number = 0; if(number < 1 || number > 4) printf("%c%c%c", '\b', ' ', '\b'); //an error occurred Your Choice [1-4]: ");
7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0
There are other low-level options including using conio.h to locate your current screen position, and just back it up one row, then print a line of 79 char's, and then return to the start of that line again, and print your "Your Choice [1-4]: line, again. Another option is working directly with your video memory to poke in the right values probably a pain in Windows, though. Another option is to use the Windows API which provides a function for moving the cursor on the monitor. Using getchar() won't help here. You're dealing with buffered input, and as soon as the <enter> key is hit, the monitor will move to the next line.
Last edited by Adak; 01-04-2009 at 11:54 AM.
return number; }
Share
9. 01-05-2009#8
transgalactic2
Share
10. 01-05-2009#9
laserlight
C++ Witch Join Date Oct 2003 Location Singapore Posts 16,206
can i use gets?? Yes you can, no you may not. I think Adak misinterpreted your requirements since you seem perfectly fine with printing the menu whenever there is invalid input, but the point is that you only want to
print it once. As you are apparently unable to find the correct FAQ item, here it is: How do I flush the input buffer?
Share
C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
11. 01-05-2009#10
transgalactic2
i added getchar bellow the scanf for "abc" its still printing the menu several times instead of once
Share
12. 01-05-2009#11
laserlight
i added getchar bellow the scanf for "abc" its still printing the menu several times instead of once Why did you add the getchar below the scanf? What do you think the getchar does? The FAQ suggests more than that, and even what the FAQ suggests cannot be taken blindly since you must adapt it for your situation.
Share
C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
13. 01-05-2009#12
transgalactic2
Share
14. 01-05-2009#13
laserlight
C++ Witch Join Date Oct 2003 Location Singapore Posts 16,206
getschar clears the buffer No, getchar() gets the next character from the buffer, or returns EOF if there is none. How many characters are on the buffer after the user enters "abc"? How many are left after the scanf() fails to scan an integer?
Share
C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
15. 01-05-2009#14
transgalactic2
after scanf() fails to scan an integer there are 2 characters in the buffer
Share
16. 01-05-2009#15
transgalactic2
1 2 3 4
num=getchar(); }
get char will take one character out of the buffer each time is that correct?