if statement with ordering numbers

Hey everyone,
So I'm writing this program that randomly generates 3 numbers between 0 and 100 and then has to display them in order, low to high. I have this so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

// Function prototype
int randomGenerator (int min, int max);

int main()
{
	// variable declarations
	int minimum = 0;
	int maximum = 100;
	int num1;
	int num2;
	int num3;

	// call to function randomGenerator
	num1 = randomGenerator(minimum, maximum);
	num2 = randomGenerator(minimum, maximum);
	num3 = randomGenerator(minimum, maximum);

	if ( num1 < num2 && num1 < num3 )
	{       cout << num1 << endl;
	}
	else if ( num2 < num1 && num2 < num3 )
	{	    cout << num2 << endl;
	}
	else if ( num3 < num1 && num3 < num2 )
	{   	cout << num3 << endl;
	}
	
	



	cout << endl << endl << "Please press enter once or twice to continue...";
	cin.ignore().get();
	return EXIT_SUCCESS;

}

// Function definition
int randomGenerator (int min, int max)
{
	return (min + rand() % (max - min + 1));
}


When I run the program, it just displays one number and not 3. Anybody know what I can do to fix this? I've just started using selection structures so I'm not fluent enough in it yet. Thanks for any help.
Your program is doing what you are asking it to do, output only one number in each if statement. Try this.

http://www.cplusplus.com/reference/cstdlib/rand/?kw=rand
http://www.cplusplus.com/doc/tutorial/control/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <ctime>

using namespace std;

int randomGenerator();

int main()
{
	srand(unsigned(time(NULL)));
	
	int num1, num2, num3;
	
	num1 = randomGenerator();
	num2 = randomGenerator();
	num3 = randomGenerator();
	
	if (num1 < num2 && num2 < num3)
		cout << num1 << ' ' << num2 << ' ' << num3 << endl;
	else if (num1 < num3 && num3 < num2)
		cout << num1 << ' ' << num3 << ' ' << num2 << endl;
        //And so on
	return 0;
}

int randomGenerator(){
	return rand() % 101;
}
Last edited on
I need the randomly generated numbers to be displayed in order of their value, low to high, though. The assignment i'm doing is an exercise in selection control structures, so I have to use if-else statements to display them in order.
Try doing something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int tempValue = 0;
bool swap = true;

while (swap)
{
    if (num1 > num2)
    {
        tempValue = num2; //Store value of num2, since it will be replaced by num1
        num2 = num1; //Swap values of num2 and num1
        num1= tempValue; //Set num1 to the previous value of num2
        swap = true;
    }
    else if (num2 > num3)
    {
        tempValue = num3;
        num3 = num2;
        num2 = tempValue;
        swap = true;
    }
    else
        swap = false; //No swap occurred
}


This should put all of your numbers in order.
Last edited on
Topic archived. No new replies allowed.