C programming question?

2016-01-14 2:24 pm
Whats wrong in this program? its is not behaving properly.

#include <stdio.h>
#include <stdlib.h>

main()
{
char n,ok;
ok='n';
while(ok=='n')
{


puts("Menu\n");
puts("1. Create a directory\n");
puts("2. delete a directory\n");
puts("3. Show a directory\n");
puts("4. Exit\n");
puts(" Your choice: \n");

n=getchar();
switch (n)
{
case '1' : puts ("you entered 1");
break;
case '2' : puts("you entered 2");
break;
case '3' : puts("you entered 3");
break;
case '4' : puts("you entered 4");
break;
default : puts("Enter a valid number\n");
ok='n';
}
}

}
更新1:

The switch function is not working !

回答 (5)

2016-01-14 7:39 pm
Good tips from husoski about input validation; and yes, the newline in the input stream is the thing that is causing you a problem. I suggest you use fgets:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE_LEN 80
#define EXIT '4'
char *menuChoices = "1234";

int main(int argc, char *argv[]) {
. . char in[MAX_LINE_LEN], ok = 'n', menuSel = ' ';

. . do {
. . . . do {
. . . . . . puts("\n --- Menu --- ");
. . . . . . puts("1. Create a directory");
. . . . . . puts("2. Delete a directory");
. . . . . . puts("3. Show a directory");
. . . . . . puts("4. Exit\n");
. . . . . . printf(" Your choice: ");
. . . . . . fgets(in, MAX_LINE_LEN, stdin);
. . . . . . if (strchr(menuChoices, in[0]) != NULL) {
. . . . . . . . ok = 'y';
. . . . . . }
. . . . } while (ok == 'n');

. . . . switch (menuSel = in[0]) {
. . . . case '1' :
. . . . . . puts ("you entered 1");
. . . . . . break;
. . . . case '2' :
. . . . . . puts("you entered 2");
. . . . . . break;
. . . . case '3' :
. . . . . . puts("you entered 3");
. . . . . . break;
. . . . case '4' :
. . . . . . puts("you entered 4");
. . . . . . puts("goodbye");
. . . . . . break;
. . . . default :
. . . . . . puts("Enter a valid number");
. . . . . . break;
. . . . }
. . } while (menuSel != EXIT);
. . return 0;
}

#if 0

Sample run:

. --- Menu ---
1. Create a directory
2. Delete a directory
3. Show a directory
4. Exit

. Your choice: 1
you entered 1

. --- Menu ---
1. Create a directory
2. Delete a directory
3. Show a directory
4. Exit

. Your choice: 5
Enter a valid number

. --- Menu ---
1. Create a directory
2. Delete a directory
3. Show a directory
4. Exit

. Your choice: 4
you entered 4
goodbye

#endif
2016-01-14 3:54 pm
All I see (other than that you didn't specify a return type for main()...not good style) is that you are not filtering out whitespace characters.

Add a compound case to your switch:
case ' ':
case '\n':
case '\t': break; /* skip over spaces, tabs and newlines */

That will get the big three (spacebar, Tab and Enter keys.) To ignore all characters that C considers "whitespace", add <ctype.h> to your includes and add an if statement to the start of your default: clause...

default:
if (isspace(n)) break; /* ignore whitespace */

Then you don't need the other case labels.

The reason this is a problem is that console or terminal input with stdin is line-oriented. That allows the user to edit what is typed before submitting a whole line by pressing Enter or Return. It also means that there will be a newline character ('\n') in the stream at the end of each line. You could just test for that, but it's also nice to allow leading or trailing spaces in the input. The isspace() macro exists partly for that reason.

PS: About the return type, you should specify the return type and explicitly return a value (0 if you have nothing to say):

int main()
{
.... [body of main() function]
.... return 0;
}
2016-01-14 2:48 pm
Maybe it is the getchar() that is not working.
Try looping :

n = 0;
while(n==0) n = getch();

In this case you wait until a key is pressed.
2016-01-14 2:43 pm
So the switch does not print "Enter a valid number" or any message, it just keeps printing the menu. That is very odd.
It looks like there is no way to change ok, that means the program is an infinite loop. You have not said that.

So if nothing is printed out and there is not an infinite loop some sort of error is in getchar.
2016-01-14 2:32 pm
What behavior do you expect and what is it outputting?


收錄日期: 2021-04-21 16:18:06
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20160114062423AAbWvoC

檢視 Wayback Machine 備份