i recently started learning c++ and i have been given a question and no one seems to be able to get it in my class. I have to write a piece of programming. the program has to accept 3 positive numbersas inputs and print it out in accending order. if a negative number is inputted it should show a message to re enter a correct number . Any help will be appreciated.
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
int exit;
cout << "Please type the first number and press Enter." << endl;
cin >> num1;
cout << " " << endl;
cout << "Please type the second number and press Enter." << endl;
cin >> num2;
cout << " " << endl;
cout << "Please type the third number and press Enter." << endl;
cin >> num3;
cout << " " << endl;
if (num1<num2 && num2<num3)
cout<< num1 << num2 << num3;
if (num1<num3 && num3<num2)
cout<< num1 << num3 << num2;
if (num2<num1 && num1<num3)
cout<< num2 << num1 << num3;
if (num2<num3 && num3<num1)
cout<< num2 << num3 << num1;
if (num3<num2 && num2<num1)
cout<< num3 << num2 << num1;
if (num3<num1 && num1<num2)
cout<< num3 << num1 << num2;
#include <iostream>
usingnamespace std;
// Does not return until a positive number has been entered;
int get_positive_integer (int n)
{ int num;
do
{
cout << "Please enter number " << n << ": ";
cin >> num;
} while (num < 0);
return num;
}
int main()
{ int num1;
int num2;
int num3;
num1 = get_positive_integer (1);
num2 = get_positive_integer (2);
num3 = get_positive_integer (3);
if (num1<num2 && num2<num3)
cout<< num1 << num2 << num3;
if (num1<num3 && num3<num2)
cout<< num1 << num3 << num2;
if (num2<num1 && num1<num3)
cout<< num2 << num1 << num3;
if (num2<num3 && num3<num1)
cout<< num2 << num3 << num1;
if (num3<num2 && num2<num1)
cout<< num3 << num2 << num1;
if (num3<num1 && num1<num2)
cout<< num3 << num1 << num2;
system ("pause");
return 0; // A successful program should always return 0
}
BTW, you can't enter 'exit' into an int.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.