can you help me fix this

so i have a class assignment , and i have to create a class assignment to make a c++ program which makes the user write 3 number then the program will arrange them from smallest to largest , i have to accord for all situation like if the user types in 2 same one wrong or if the user types in a letter , im only allowed to use if and else if along with variables ,'and' and ' or ' are not allowed so my problem is i don't understand whats wrong with my program

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>

using namespace std;

int main()
    {
	///variable declaration
	
	double numfirst;
    double numsecond;
	double numthird ;
	double smallest;
	double medium;
	double largest ;

	/// input
	cout<< "choose your first number : ";
	cin >> numfirst;
	
	cout<<"\nchoose your second number :";
	cin>> numsecond;
	
	cout<<"\nchoose your third number : ";
	cin>> numthird;
	
	cout<< "\nyour numbers will be ordered from smallest to largest unless they are the same";
	
	
	
	///output and processing
	
     if     (numfirst < numsecond)   
	{
       if (numsecond < numthird)
    {
	   smallest = numfirst;
	   medium = numsecond;
	   largest = numthird;
    }
    }
else if     (numfirst < numthird)   
	{
       if (numthird < numsecond)
    {
	   smallest = numfirst;   
	   medium = numthird;
	   largest = numsecond;
    }
    }
else if     (numsecond < numfirst)   
	{
       if (numfirst < numthird)
    {
       smallest = numsecond;	   
	   medium = numfirst;
	   largest = numthird;
    }
    }
else if     (numsecond < numthird)   
	{
       if (numthird < numfirst)
    {
       smallest = numsecond;	   
	   medium = numthird;
	   largest = numfirst;
    }
    }
else if     (numthird < numfirst)   
	{

       if (numfirst < numsecond)
    {
       smallest = numthird;  
	   medium = numfirst;
	   largest = numsecond;
    }
    }
else if     (numthird < numsecond)   
	{
       if (numsecond < numfirst)
    {
       smallest = numthird;	   
	   medium = numsecond;
	   largest = numfirst;
    }
    }
else if     (numfirst == numsecond)  
	{
       smallest = numfirst;
       if     (numsecond < numthird)
    {
	   medium = numsecond;
	   largest = numthird;
    }
    }
else if     (numfirst == numthird)  
	{
       smallest = numfirst;
       if     (numthird < numsecond)
    {
	   medium = numthird;
	   largest = numsecond;
    }
    }
else if     (numfirst == numsecond)  
	{
       if     (numsecond  > numthird)
    {
       smallest = numthird;
	   medium = numsecond;
	   largest = numfirst;
    }
    }
else if     (numfirst == numthird)  
	{
       if     (numthird > numsecond)
    {
       smallest = numsecond;
	   medium = numfirst;
	   largest = numthird;
    }
    }
else if     (numsecond == numfirst)  
	{
       smallest = numsecond;
       if     (numfirst < numthird)
    {
	   medium = numsecond;
	   largest = numthird;
    }
    }
else if     (numsecond == numthird)  
	{
       smallest = numsecond;
       if     (numthird < numfirst)
    {
	   medium = numthird;
	   largest = numfirst;
    }
    }
else if     (numsecond == numfirst)  
	{
       if     (numfirst > numthird)
    {
       smallest = numthird;
	   medium = numsecond;
	   largest = numfirst;
    }
    }
else if     (numsecond == numthird)  
	{
       if     (numsecond > numthird)
    {
       smallest = numthird;
	   medium = numfirst;
	   largest = numsecond;
    }
    }
else if     (numthird == numfirst)  
	{
       smallest = numthird;
       if     (numfirst < numsecond)
    {
	   medium = numfirst;
	   largest = numsecond;
    }
    }
else if     (numthird == numsecond)  
	{
       smallest = numthird;
       if     (numsecond < numfirst)
    {
	   medium = numsecond;
	   largest = numfirst;
    }
    }
else if     (numthird == numfirst)  
	{
       if     (numfirst > numsecond)
    {
       smallest = numsecond;
	   medium = numfirst;
	   largest = numthird;
    }
    }
else if     (numthird == numsecond)  
	{
       if     (numsecond > numfirst)
    {
       smallest = numfirst;
	   medium = numsecond;
	   largest = numthird;
    }
    }
else 
    {
       smallest = numfirst;
	   medium = numsecond;
	   largest = numthird;
    }
cout<< "\nthe order of your numbers :  "<< smallest << " , "<< medium<< " , " << largest;
 
cin.get();
    }
Your indentation is all over the place and makes what should be simple logic really hard for us to read. Also, darn, what kind of insane teacher doesn't allow basic logic operations?

You have at least 18 if-branches -- more than the possible number of outcomes! I think you are over-complicating this.

Are you allowed to use the + and - symbols in your program? If so, I have a nice solution.

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
#include <iostream>

double max(double a, double b)
{
    if (a >= b)
        return a;
    else
        return b;
}

double min(double a, double b)
{
    if (a <= b)
        return a;
    else
        return b;
}

int main()
{
    using namespace std;
    
    double a = 3.0;
    double b = 4.0;
    double c = 2.0;
    
    double min_num = min(a, min(b, c));
    double max_num = max(a, max(b, c));
    
    double med_num = (a + b + c) - (min_num + max_num);
    
    std::cout << "Ordered: " << min_num << " " << med_num << " " << max_num << std::endl;
}
Last edited on
yeah im allowed to use + and -
Oh, then see my edit.
If you can't use functions, I can edit them out to inline them.
Last edited on
oh yeah i cant use min_num sorry
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
51
#include <iostream>

int main()
{
    int first, second, third ;

    std::cout << "enter three numbers\n" ;

    //  accord for all situation like if the user types in a letter
    if( std::cin >> first >> second >> third ) // if three numbers were read
    {
        // only allowed to use if and else if along with variables,
        // 'and' and ' or ' are not allowed

        // if first is greater than second, swap the two
        if( first > second )
        {
            const int temp = first ;
            first = second ;
            second = temp ;
        }
        // after this, second is greater than or equal to first

        // if second is greater than third, swap the two
        if( second > third )
        {
            const int temp = second ;
            second = third ;
            third = temp ;
        }
        // after this, third is greater than or equal to second

        // if first is greater than second, swap the two
        if( first > second )
        {
            const int temp = first ;
            first = second ;
            second = temp ;
        }
        // after this, second is greater than or equal to first
        // (third was already greater than or equal to second)

        std::cout << "the numbers in ascending order: "
                  << first << ", " << second << ", " << third << '\n' ;
    }

    else // the user typed in a letter instead of a number
    {
        std::cout << "error: invalid input\n" ;
    }
}
Topic archived. No new replies allowed.