2 Replies - 601 Views - Last Post: 19 July 2009 - 04:13 PM Rate Topic: -----

#1 azer_g  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 30-June 09

c++ conditional operator

Posted 19 July 2009 - 11:38 AM

Hello guys, I am trying to use a ternary operator{(expression1)?(expression2):(expression 3)} in minute converter program so that when minute and hour are singular instead of "minutes" the program say "minute". It seems like it doesn't work! I appreciate any help

(minutes== 1 ) ? ( cout<< minutes <<" minute equals " << hours << "hour and " << minutes - 60*hours <<"minute.\n"):(cout<< minutes <<" minutes equals " << hours << "hours and " << minutes - 60*hours<<"minutes.\n");


below is my whole code:

#include <iostream>
using std::cout;
using std::cin;

int Convert_from_minutes(int minutes)
{
int hours;
hours= minutes/60;
return hours;
}


int main()
{
int minutes,hours;
cout << "This program converts minutes to hours and minutes.\n";
cout << "Please enter a number of minutes (an integer) to be converted: ";
cin >> minutes;
hours = Convert_from_minutes(minutes);
if (minutes%60==0)
cout << minutes <<" minutes equals " << hours <<" " <<"hours.\n";
else
cout << minutes <<" minutes equals " << hours << "hours and " << minutes - 60*hours << "minutes.\n";
return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: c++ conditional operator

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: c++ conditional operator

Posted 19 July 2009 - 12:06 PM

well I think all you need to do is add some parethesis. If this does not fix then please let us know what the problem is (what error message you get what is wrong with the output etc).

(minutes== 1 ) ? ( cout<< minutes << " minute equals " << hours << "hour and " << (minutes - 60 * hours)  << "minute.\n"):
				 ( cout<< minutes << " minutes equals " << hours << "hours and " << (minutes - 60 * hours) << "minutes.\n");


But really this is a place where you should us if-else rather than ?:

the conditional operator would be better used for something like:
cout << minutes << ((minutes== 1 )? " minute equals " : " minutes equals ") 
	 << hours << "hours and " << (minutes - 60 * hours)  << ((minutes== 1 )? " minute.\n" : " minutes.\n");


this would be much cleaner and clearer:
if (minutes== 1 )  {
	cout<< minutes << " minute equals " << hours << "hour and " << (minutes - 60 * hours)  << "minute.\n";
} else {
	cout<< minutes << " minutes equals " << hours << "hours and " << (minutes - 60 * hours) << "minutes.\n";
}


when it is put in-line into the expression. However since you have 2 uses of the same condition it is really just better to use the if-else.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_c.user*


Reputation:

Re: c++ conditional operator

Posted 19 July 2009 - 04:13 PM

 
#include <iostream>
 
using namespace std;

int main(void)
{
	int minutes = 1,
		hours = 3;
	
	{ /* will create ending array only for this block */
		char ending[2] = {
			minutes != 1 ?
				's' : '\0',
			'\0'
		};
	
		cout << minutes
			 << " minute"
			 << ending
			 << " equals "
			 << hours
			 << " hour"
			 << ending
			 << " and "
			 << (minutes - 60 * hours)
			 << " minute"
			 << ending
			 << "."
			 << endl;
	}
	
	return 0;
}


also with variable may
Was This Post Helpful? 0

Page 1 of 1