Complete noob question - if / else

Heya guys :)

Currently coding a converter for days into weeks and days.

It all works fine, and allows me to convert the amount of days I want into weeks and days, but it isn't perfect.

For an amount of days such as 8, it is 1 week and 1 day.
For an amount of days such as 9, it is 1 week and 2 days.
dayand days are different words.

So when a number of days is entered which ends up being however many weeks and 1 DAY, I want it to display that it is 1 DAY, not DAYS.

And when a number of days is entered which ends up being however many weeks and more than 1 day, I want it to display that it is however many DAYS, not day.

So how do I do this?

This is my current working 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
28
29
30
31
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main (void)
		{

		//Declare variables
		int days1,days2,weeks;
		
//Heading
		printf("This program allows you to input a number of days and will convert it to weeks and days\n\n");

		//Input number of days
		printf("Input a number of days: ");
		scanf("%d",&days1);

		//Calculate number of weeks
		weeks=days1/7;

		//Calculate number of days remaining
		days2=days1%7;

		//Output result
		printf("%d",days1);
        printf(" days equals: \n");
        printf("%d",weeks);
        printf(" weeks and ");
        printf("%d",days2);        
        printf(" days");
        getch();
}


I know this could probably be shortened a lot, but I'm not interested in that. I just want to be able to create an if, else statement which allows the program to display DAY or DAYS correctly, rather than DAYS all the time.

This probably does't make much sense.. I'm completely shattered, and could probably word it a lot better if I was more awake lol.

Cheers for any help,

Liam
Here you go:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main (void)
        {

        //Declare variables
        int days1,days2,weeks;
        
//Heading
        printf("This program allows you to input a number of days and will convert it to weeks and days\n\n");

        //Input number of days
        printf("Input a number of days: ");
        scanf("%d",&days1);

        //Calculate number of weeks
        weeks=days1/7;

        //Calculate number of days remaining
        days2=days1%7;

        //Output result
        printf("%d",days1);
        printf(" days equals: \n");
        printf("%d",weeks);
        if( weeks < 2 ){
            printf(" week and ");
        } else {
            printf(" weeks and ");
        }
        printf("%d",days2); 
        if( days2 < 2 )       
        {
            printf(" day");
        } else {
            printf(" days");
        } 
        getch();
}

I hope this helps ;)
You should read up on control structures. http://cplusplus.com/doc/tutorial/control/
That should be a really easy issue to solve

1
2
3
4
5
6
printf("weeks and ");
printf("%d", days2);
if(days2 == 1)
     printf(" day");
else
     printf(" days");


But like i said, really read the contents of the link i provided. It will save you from having to ask about this again ^_^

EDIT: I hate getting beat to the post by like 30 seconds. lol
Last edited on
Topic archived. No new replies allowed.