Simple Program small number

Jan 22, 2013 at 7:09am
Hello everyone, I am trying to write a program that reads in three integers and determines and prints the smallest number numerically in the group.

Help! What am I doing wrong!


#include <iostream>
using namespace std;
int main () {
int num1, num2, num3;
cout << "Enter 3 integers: ";
cin: num1, num2, num3;
if ( num1 < num2 || num3 )
cout << "The smallest value among // num1 // num2 // num3 is // num1";
if ( num2 < num1 || num3 )
cout << "The smallest value among // num1 // num2 // num3 is // num2";
if ( num3 < num1 || num2 )
cout << "The smallest value among // num1 // num2 // num3 is // num2";
return 0;
}
Jan 22, 2013 at 7:11am
Instead of

cin: num1, num2, num3;

should be

cin >> num1 >> num2 >> num3;

Instead of

if ( num1 < num2 || num3 )

should be

if ( num1 < num2 || num1 < num3 )
Jan 22, 2013 at 7:23am
Also you could economize your coding if it interests you by doing this:

1
2
3
4
5
6
7
8
cout << "The smallest value among // num1 // num2 // num3 is // ";

if ( num1 < num2 || num 1 < num3 )
   cout << "num1";
else if ( num2 < num1 || num2 < num3 )
   cout << "num2";
else if ( num3 < num1 || num3 < num1 )
   cout << "num2";
Jan 22, 2013 at 7:32am
Hey everyone, thanks for the help.. once more thing..

I am trying to get it to say "The smallest value among <a>, <b>, and <c> is <x>" (where <a>, <b>, <c>, and <x> are replaced by the actual values.) but it seems like I am messing up cout formatting. :(

Thanks


#include <iostream>
using namespace std;
int main () {
int num1, num2, num3;
cout << "Enter 3 integers: ";
cin >> num1 >> num2 >> num3;
if ( num1 < num2 || num1 < num3 )
cout << "The smallest value among // num1 // num2 // num3 is // num1";
if ( num2 < num1 || num2 < num3 )
cout << "The smallest value among // num1 // num2 // num3 is // num2";
if ( num3 < num1 || num3 < num2 )
cout << "The smallest value among // num1 // num2 // num3 is // num3";
return 0;
}
Jan 22, 2013 at 7:46am
1
2
cout << "The smallest value among " << num1 
       << ", " << num2 << ", and " << num3 << " is " << num1 << endl;
Jan 22, 2013 at 7:56am
This is outputting data but I need it to output in cout the smallest of the 3 numbers after going through the if statements.
Jan 22, 2013 at 7:59am
Try to do it yourself. As an advice define one more variable that will contain the minimum.

By the way your function is incorrect because it will not find the smallest if at least two of three numbers are equal to each other and less than the third number.
Last edited on Jan 22, 2013 at 8:02am
Topic archived. No new replies allowed.