Reputation: 17
I wrote my first program in C++ which reads in three integers and determines which is the smallest number from the group. However I need some guidance on how to prompt the user and read the three values from the console with a cout
print message.
In my pseudo code I have it to be...""The smallest value among a, b, and c is x
(where a
, b
, c
, and x
are replaced by the actual values.)
I am just stuck on how to implement that with my code.
My requirements are:
&&
and ||
Any guidance is really appreciated
#include <iostream>
using namespace std;
int main()
{
int a, b, c, d;
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;
if (a < b)
d = a;
else
d = b;
cout << "the smallest of the numbers is: " << ((d < c) ? d : c) << endl; //
return 0;
}
Upvotes: 0
Views: 13725
Reputation: 11153
If you want to print the message The smallest value among a, b, c
then this is what you need
int a, b, c, d;
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;
if(a < b && b < c) {
d = a;
}
else {
if(b < a && b < c) {
d = b;
}
else {
d = c;
}
}
cout << "the smallest value among " << a << ", " << b << " and " << c << " is: " << d << endl;
Upvotes: 0
Reputation: 7625
Taking input is correct, after taking input:
if(a<b){
if(a<c){
cout << "the smallest of the numbers is: " <<a<<endl;
}
else{
cout << "the smallest of the numbers is: " <<c<<endl;
}
}
else {
if(b<c){
cout << "the smallest of the numbers is: " <<b<<endl;
}
else{
cout << "the smallest of the numbers is: " <<c<<endl;
}
}
You can do it in one line:
d = a<b? (a<c?a:c) : (b<c?b:c);
cout << "the smallest of the numbers is: " <<d<<endl;
Upvotes: 3