using && operator more than once in a statement

I wanted to give output to the user (from the Age Calculator) such that there is no value of 0.
That is:
The program will give the output of user's age in YEARS, MONTHS, WEEKS and DAYS.
Now I wanted that if, for instance, the user is exactly 16 years and 8 months old, the output will ignore weeks and days.
I used the following code to do so (is is still incomplete)
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
//Giving the Age
if (year_age == 0)
{
	cout << "You are "<< month_age << " months, "<< week_age << " weeks and " << date_age << " days old." 
	cout << endl << endl;
}			
else if ((month_age == 0) && (week_age == 0))
{
	cout << "You are "<< year_age << " years and " << date_age << " days old."<< endl << endl;
}
else if ((month_age == 0) && (date_age == 0))
{
	cout << "You are "<< year_age << " years and " << week_age << " weeks old. " << endl << endl;
}
			
else if ((date_age == 0) && (week_age == 0))
{
	cout << "You are "<< year_age << " years and " << month_age << " months old. "<< endl << endl;
	}
else if (date == cur_date && month == cur_month && week_age == 0)
{
	cout << "You are "<< year_age << " years old."<<endl <<endl;
}
else
{
	cout << "You are "<< year_age << " years, " << month_age << " months, "<< week_age << " weeks and ";
	cout << date_age << " days old." << endl << endl;
}


Now the problem I am facing:
If the users is exactly x years old (say 13), then the output should be
13 years
, but the output is
13 years and 0 days


The program is ignoring the last else if command and using the first one.
I encounter the same problem if I try to do so with the program ignoring only one of the 4 values(year, month, week and date).
If I add the conditions for these as well, he compiler ignores the others.
The others is like in the above code if I added
1
2
3
4
5
6
7
8
9
10
11
12
if (month_age == 0)
{
	cout << "You are " << year_age << " years, " << week_age << " weeks and " << date_age << " days old.";
}

//From the code I posted above

else if ((month_age == 0) && (week_age == 0))
{
	cout << "You are "<< year_age << " years and " << date_age << " days old."<< endl << endl;
}
//And further..... 

And if the Age of th User is 15 YEARS and 5 DAYS old.

It will give the output:

You are 15 years, 0 weeks and 5 days old.


So is there any way to get around this?
You should break your output:
1
2
3
4
5
cout << "You are";
if ( year_age != 0 )
    cout << ' ' << year_age << "years,";
// etc.
cout << "old." << endl << endl;
Otherwise you'd need a LOT of cases to match all possibilities.
If you have problems with superfluous commas, add an else to the last part ( date_age ):
1
2
3
4
if ( date_age != 0 )
    cout << ' ' << date_age << "days";
else
    cout << '\b'; // output a backspace to remove the last comma 
When using...
1
2
3
4
5
if(a)
	cout << "a is true";
else if(a && b)
	cout << "both a and b are true";
...

if a is indeed true the second condition won't even be checked (although it might also be true). The solution to your problem might require replacing else if with if or nesting those conditions.
@Zeillinger: So should I have used || operator?
@Bazzy: Thank a lot. Just one more thing. I want an and to be present before the least value. Like : if user is 18 years and 6 months old, the output in this method will be:
You are 18 years, 6 months old.
Last edited on
Topic archived. No new replies allowed.