Also remember that ASCII '9' is different from the decimal constant 9...
Also remember that ASCII '9' is different from the decimal constant 9...
Is there a way of doing it with fgets?
Yes
'N yeah you can put it in a loop if you want...Code:char buf[8]; // we only really need 1 + NUL (but better safe than sorry!) fgets(buf, sizeof(buf), stdin); if(buf[0] == 'a') ; // a! else if(buf[0] == '1') ; // 1 else ; // invalid input
you could even just use getchar function to read a char from the user. That's the actual choice from the user.Code:#include <stdio.h> int main() { int ch; printf(" ( a ). Aplha \n"); printf(" ( b ). Beta \n"); printf(" ( 1 ). One \n"); printf(" ( 2 ). Two \n"); printf("Enter choice\n?"); ch = getchar(); while( ch != 'q' ) { switch( ch ) { case 'a': printf("You entered A\n"); break; case 'b': printf("You entered B\n"); break; case '1': printf("You entered 1\n"); break; case '2': printf("You entered 2\n"); break; } while((ch=getchar()) != '\n' && ch != EOF); printf("Enter choice\n?"); ch = getchar(); } while((ch=getchar()) != '\n' && ch != EOF); getchar(); return 0; }
ssharish2005
Hey guys iv done if(input == 97) which repesents 'a' in ascii that works if i do it once but however it does not work for ascii 98 which is 'b' and so on. I can only use this if else statements i cant use switches. And i have to use fgets
Code:do { /* Print out menu */ printf("\nMain Menu:\n"); printf(" (1) Hot Drinks Summary\n"); printf(" (2) Cold Drinks Summary\n"); printf(" (3) Detailed Menu Report\n"); printf(" (4) Add Menu Category\n"); printf(" (5) Delete Menu Category\n"); printf(" (6) Add Menu Item\n"); printf(" (7) Delete Menu Item\n"); printf(" (8) Save & Exit\n"); printf(" (9) Abort\n"); printf("\nSelect your option (1-9): "); /* Store user's input and check for correct length */ fgets(input, MAX_OPTION_INPUT + EXTRA_SPACES, stdin); /* Convert the user's input into an integer */ inputNumber = atoi(input); /* Process the user's request */ if(input[0] == 97) printf("character a \n"); else if(input[1] == 98) printf("dude"); else if(inputNumber == 1) displaySummary(&menu, HOT); else if(inputNumber == 2) displaySummary(&menu, COLD); else if(inputNumber == 3) categoryReport(&menu); else if(inputNumber == 4) addCategory(&menu); else if(inputNumber == 5) deleteCategory(&menu); else if(inputNumber == 6) addItem(&menu); else if(inputNumber == 7) deleteItem(&menu); else if(inputNumber == 8) { saveData(&menu, menuFile, submenuFile); printf("Changes saved successfully! Now exiting...\n"); systemFree(&menu); return EXIT_SUCCESS; } else if(inputNumber == 9) { /* If user abort's program, free all allocated memory before exiting */ printf("Thank you for using this program. Now exiting...\n"); systemFree(&menu); return EXIT_SUCCESS; } } while(inputNumber != ABORT); return EXIT_SUCCESS; }
Why are you testing the second char when testing for 'b' ?Code:else if(input[1] == 98)
what's wrong with making it more portable ( and readable ) like this ?
KurtCode:else if(input[0] == 'b')
Said the night wind to the little lamb,"Do you see what I see?"Code:if(input[0] == 97) printf("character a \n"); else if(input[1] == 98)
Quzah.
Hope is the first step on the road to disappointment.
Wouldn't it be wonderful if we could replace 97 or 98 by something more meaningful like ... 'a' and 'b' ?
Can you do that?!
Quzah.
Hope is the first step on the road to disappointment.
sorry guys just did that for testing purposes because i couldnt get it working ill try. I may of been pointing to the wrong element in the array