Really need some help! (I just started C for 3 months)

Time Input Output in Words
09:10:00 ten past nine
10:45:00 quarter to eleven

#include <stdio.h>
void main()
{
int hh,mm,ss;

printf("Enter hour(s):");
scanf("%d",&hh);
printf("Enter minute(s):");
scanf("%d",&mm);
printf("Enter seconds(s):");
scanf("%d",&ss);

printf("%2d:%2d:%2d\n",hh,mm,ss);
if (mm>='30'&& mm<='45')
{
hh=hh+1;
printf("quater to %d.",hh+1);
}


}

My teacher give some hints for us, it consist of array/for/while/if loops.

Last edited on
What is your question?

We don't do homework assignments.
Any reason why you use printf and scanf over cout and cin?
we use printf instead of cout and cin.
printf and scanf are C functions.

And instead of just posting a homework assignment, why don't you tell us what specifically you're having issues with. What don't you understand about what you're doing?
I am not sure is it correct to use if loops for this ques. I also don't have any idea how to put in array for this question.
I am not sure is it correct to use if loops for this ques. I also don't have any idea how to put in array for this question.


You could use a while (test expression) { /* do stuff */ } loop that continues getting console input and outputting the results until the user inputs otherwise.

As for arrays, I don't think it's necessary for this question. By the way, 3 posts later, and you still haven't asked a single question.
I did some correction on my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
void main()
{
	int hh,mm,ss;

	printf("Enter hour(s):");
	scanf("%d",&hh);
	printf("Enter minute(s):");
	scanf("%d",&mm);	
	printf("Enter seconds(s):");
	scanf("%d",&ss);

	printf("%2d:%2d:%2d\n",hh,mm,ss);
	while (mm<60) 
	{	
		if(mm=45)
		{
		printf("Quarter to %d.",hh+1);
		}
	}
	
    printf("Invalid Input.\n\n");

}


I am trying to ask the user to input 10:45:00 and my output should be "Quarter to eleven".But right now I only can display the output as "Quarter to 11" instead of "Quarter to eleven" and the if loop keeps looping and can't stop.
Last edited on
You don't need a while statement there, a simple if statement works, unless you were told that you need to convert mm to an actual minute value within the range of 0-59. You're also going to be displaying Invalid Input everytime.

To convert 11 to eleven, you simply need if statements to print out the word of each number 1, one, 2, two, 3, three, etc.
How can I makes the output appears to be> 09:02:00 when I input hh=9,mm=2 and ss=0?

The other problem I am facing now is when I input hh=23 and mm=45, The output will be "Quater to ." because hh has become 24. How can I make hh=0, when hh=24?

Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
void main()
{
	int hh,mm,ss;
	char number[50][24]={"Midnight","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven"};

	printf("Enter hour(s):");
	scanf("%2d",&hh);
	printf("Enter minute(s):");
	scanf("%2d",&mm);	
	printf("Enter seconds(s):");
	scanf("%2d",&ss);
                   
	printf("\nOutput");
	printf("\n%2d:%2d:%2d\n",hh,mm,ss);

	
	if(mm=45 && hh<=23 && hh>=0)
	{ 
		hh=hh+1;
		printf("Quater to %s.\n\n",number[hh]);
		
	}
		
    else printf("Invalid Input.\n\n");

}
Topic archived. No new replies allowed.