3 Replies - 353 Views - Last Post: 14 July 2011 - 04:06 AM Rate Topic: -----

#1 elkeiven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-July 11

c++ using ? problem

Posted 14 July 2011 - 02:31 AM

i cant convert this c++ codes using conditional operator (?)

#include <iostream.h>
#include <conio.h>


main()
{

int v1, v2, v3, first, second, third;

clrscr();

cout << "Enter First value : ";
cin >> v1;
cout << "Enter Second value : ";
cin >> v2;
cout << "Enter Third value : ";
cin >> v3;




if (v1 >= v2 && v1 >= v3) {
first = v1;
}

else if (v2 >= v1 && v2 >= v3) {
first = v2;
}

else if (v3 >= v1 && v3 >= v2) {
first = v3;
}




if (v1 > v2 && v1 < v3 || v1 < v2 && v1 > v3) {
second = v1;
}

if (v2 > v1 && v2 < v3 || v2 < v1 && v2 > v3) {
second = v2;
}


if (v3 > v2 && v3 < v1 || v3 < v2 && v3 > v1) {
second = v3;
}






if (v1 <= v2 && v1 <= v3) {
third = v1;
}

else if (v2 <= v1 && v2 <= v3) {
third = v2;
}

else if (v3 <= v1 && v3 <= v2) {
third = v3;
}





cout << "The highest number is  " << first;
cout << "\n";
cout << "The second number is  " << second;
cout << "\n";
cout << "The lowest number is  " << third;


getch();

}




somebody helps! thanks for advance! ^_^

This post has been edited by stayscrisp: 14 July 2011 - 03:26 AM
Reason for edit:: Please use code tags!!


Is This A Good Question/Topic? 0
  • +

Replies To: c++ using ? problem

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,199
  • Joined: 14-February 08

Re: c++ using ? problem

Posted 14 July 2011 - 03:31 AM

Read up about the conditional operator and you will be able to do this.
Was This Post Helpful? 0
  • +
  • -

#3 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: c++ using ? problem

Posted 14 July 2011 - 03:53 AM

Wow, that code is horrible! Let's go through it.
#include <iostream.h>
This is wrong and non-standard. It needs to be:
#include <iostream>

main()
This is also wrong. In C++, you MUST specify a return type; it does not default to int. Try:
int main()

clrscr();
This is completely unnecessary; the screen is already clear. If you're running it from command line, why do you want to clear it anyway? Your user might not appreciate losing their history.
getch();
This is non-standard C++. Using conio.h, a deprecated and non-standard header, simply to hold open your console window is a bad idea. Use:
cin.clear();
cin.ignore();
cin.get();


Looking at it I'm guessing you're using Turbo C++. If so, please get a newer compiler! Turbo C++ is 20 years old, and is missing lots of modern C++ features and standard libraries.
A good free compiler for Windows is Microsoft Visual C++. Code::Blocks is also good and free.
Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5883
  • View blog
  • Posts: 12,764
  • Joined: 16-October 07

Re: c++ using ? problem

Posted 14 July 2011 - 04:06 AM

For a start, beyond all the ancient compiler badness, you're logic isn't that helpful.

Let's just make a better if then else first...
if (v1>v2) {
	// we no longer have to consider v2
	if (v1>v3) {
		first = v1;
	} else {
		first = v3;
	}
} else {
	// we no longer have to consider v1
	if (v2>v3) {
		first = v2;
	} else {
		first = v3;
	}
}



Given this, let's make one level a ternary expression:
if (v1>v2) {
	first = (v1>v3) ? v1 : v3;
} else {
	first = (v2>v3) ? v2 : v3;
}



The rest is up to you. Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1