hi, the below program is for a lab that i am doing in college, it suppose ask user to enter hour, minutes, is it am or pm, then adds 1 minute to it. hour and am/pm also advance as pass 59mins or 12 hours. everything else seems to be working but i couldnt get the prompt for am or pm (highlight in red), if i use scanf("%2s",&m), the value in minute will become 0 somehow. so can anyone help me with it? thx Code: #include <stdio.h> #include <conio.h> void add_minute(int *hour, int *minute, char *ampm); main() { clrscr(); int hour,min,x; char ampm; printf("Please the hour:\n"); scanf("%i",&hour); printf("Minute:\n"); scanf("%i",&min); [COLOR=DarkRed]printf("am or pm:"); scanf("%c",&m);[/COLOR] add_minute(&hour,&min,&m); printf("\nThe new time is:\n"); printf("%ihour(s) %iminute(s) %cm",hour,min,ampm); getch(); } void add_minute(int *hour, int *min, char *ampm) { *min=*min+1; if(*min>59) { *min=0; *hour=*hour+1; } if(*hour>12) { *hour=1; if(*ampm=='a') *ampm='p'; if(*ampm=='p') *ampm='a'; } }
I can't see why it would reset the minute variable, but have you tried using cin >> ampm; (#inlcude <iostream.h>) instead of scanf? thats how I generally get values from standard I/O. Matt
It's because when you call scanf("%c", %ampm), it is reading in the new line/caridge return when you hit enter from when you entered in the minutes. So a simple way would just to add a getchar() before you read in the ampm.