Development c++ need help

Discussion in 'Software' started by Lineage, 9 Feb 2007.

  1. Lineage

    Lineage What's a Dremel?

    Joined:
    19 Sep 2003
    Location:
    Canada
    Posts:
    143
    Likes Received:
    0
    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",&ampm), 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",&ampm);[/COLOR]
    	add_minute(&hour,&min,&ampm);
    	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';
    	}
    
    }
    	
     
  2. Shuriken

    Shuriken same christmas AV for a whole year

    Joined:
    1 Jan 2003
    Location:
    Woolacombe, Devon
    Posts:
    1,312
    Likes Received:
    22
    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
     
  3. glaeken

    glaeken Freeeeeeeze! I'm a cawp!

    Joined:
    1 Jan 2005
    Location:
    Orlando, FL
    Posts:
    2,041
    Likes Received:
    50
    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.
     
  4. Lineage

    Lineage What's a Dremel?

    Joined:
    19 Sep 2003
    Location:
    Canada
    Posts:
    143
    Likes Received:
    0
    ok, got it

    thx all
     

Share This Page