do while doesnt work like expected

Hello,
i have 2 input-types int. They have to be bigger than the ones before. Start values is 0 and -1;
Allowed Values:
{ 31,32,41,42,43,51,52,53,54,61,62,63,64,65,11,22,33,44,55,66,21 }
So after 32, valid values are: 41,42,43...
31 and 32 are not allowed.
But with the code i get this to only work one time, then i can type in a bigger number and the output says its a smaller one.
heres the code which works one time::

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
  #include <iostream>
#include <string>
using namespace std;


bool compare(int max, int min, int input1, int input2) //true = input bigger then value before
{
	if ((input1 == 2) && (input2 == 1))
	{
		return true;
	}
	else if ((max == 2) && (min == 1))
	{
		return false;
	}
	else if ((max == min) && (input1 == input2)) // both doubles
	{
		if (max > input1)// input is lower
		{
			return false;
		}
		else //input is higher
		{
			return true;
		}
	}
	else if (max == min) //old input is a double
	{
		return false;
	}
	else if (input1 == input2) //new input is a double
	{
		return true;
	}
	//no double
	else if (max > input1)	//Input is lower than old value
	{
		return false;
	}
	else if (max < input1) //Input is higher than old value
	{
		return true;
	}
	else // old value1 = new value1 -> compare value2 
	{
		if (input2 > min) //new input is bigger
		{
			return true;
		}
		else if (input2 == min) // both are equal
		{
			return false;	
		}
		else //Input is lower
		{
			return false;
		}
	}
}


int a = 0;
int b = -1;
int tempa;
int tempb ;

int main()
{
	
	int tempValue1, tempValue2, Value1, Value2;
	tempValue1 = 0;
	tempValue2 = -1;

	do {
		cin >> Value1;
		cin >> Value2;
		if (compare(tempValue1, tempValue2, Value1, Value2))
		{
			cout << "Input is bigger!" << endl;
			tempValue1 = Value1;
			tempValue2 = Value2;
		}
		else
		{
			cout << "Input is smaller!" << endl;
		}
	} while ((tempValue2 > tempValue1) && (compare(tempValue1, tempValue2, Value1, Value2) == false));

	
	system("pause");
	return 0;
}


now the code with the error, cause i goes up(do-while breaks)even though i dont want it to do so:
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
#include <iostream>
#include <string>
using namespace std;


bool compare(int max, int min, int input1, int input2) //true = input bigger then value before
{
	if ((input1 == 2) && (input2 == 1))
	{
		return true;
	}
	else if ((max == 2) && (min == 1))
	{
		return false;
	}
	else if ((max == min) && (input1 == input2)) // both doubles
	{
		if (max > input1)// input is lower
		{
			return false;
		}
		else //input is higher
		{
			return true;
		}
	}
	else if (max == min) //old input is a double
	{
		return false;
	}
	else if (input1 == input2) //new input is a double
	{
		return true;
	}
	//kein Pasch
	else if (max > input1)	//Input is lower than old value
	{
		return false;
	}
	else if (max < input1) //Input is higher than old value
	{
		return true;
	}
	else // old value1 = new value1 -> compare value2 
	{
		if (input2 > min) //new input is bigger
		{
			return true;
		}
		else if (input2 == min) // both are equal
		{
			return false;	
		}
		else //Input is lower
		{
			return false;
		}
	}
}


int a = 0;
int b = -1;
int tempa;
int tempb ;

int main()
{
	
	int tempValue1, tempValue2, Value1, Value2;
	tempValue1 = 0;
	tempValue2 = -1;

	for (int i = 0; i < 10; i++)
	{
		do {
			cout << "Player " << i + 1 << ", pls enter your values here: " << endl;
			cin >> Value1;
			cin >> Value2;
			if (compare(tempValue1, tempValue2, Value1, Value2))
			{
				cout << "Input is bigger!" << endl;
				tempValue1 = Value1;
				tempValue2 = Value2;
			}
			else
			{
				cout << "Input is smaller!" << endl;
			}
		} while ((tempValue2 > tempValue1) && (compare(tempValue1, tempValue2, Value1, Value2) == false));
	}
	
	system("pause");
	return 0;
}

Last edited on
is compare giving the wrong answer for some input?
if so, what inputs did you give it to trigger the problem? Which conditions did it hit in the compare function, where did it go wrong? You need to answer these things (to yourself, not me) by using the debugger to see what happened.
Its probably the compare function, not the loop.
Last edited on
WTF?!?!? Your code and explanation are insane.
lmao.
So after 32, valid values are: 41,42,43...
31 and 32 are not allowed.
So is 33, 34, ... etc. allowed? Why did you start your example at 41?

Why do you define all these meaningless "temp" variables? None of them are "temp", they stay alive and are used throughout your main function. Plus, you have unused global variables,
1
2
3
4
int a = 0;
int b = -1;
int tempa;
int tempb ;

Point being: Giving your variables meaningful names is important, especially if we can't understand the requirements.

If I'm understanding correct, the initial pair of numbers is -1 and 0.
The user must enter 2 numbers bigger than the existing numbers.
So if the user enters 0 and 4, it will print "input is smaller" and not accept the input.
Otherwise, it will replace the initial pair of numbers with the newer pair of numbers entered.

I don't see the end goal here, but okay that seems simple enough.
Last edited on
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
#include <iostream>
#include <algorithm>

struct Pair { // or just use std::pair in <utility>
    int first;
    int second;
};

int min(const Pair& pair)
{
    return std::min(pair.first, pair.second);
}

int max(const Pair& pair)
{
    return std::max(pair.first, pair.second);
}

int main()
{
    Pair prev_input {0, -1};
    
    Pair input;
    while (std::cin >> input.first >> input.second)
    {
        if (min(input) > max(prev_input))
        {
            std::cout << "input is higher\n";
            prev_input = input;
        }
        else
        {
            std::cout << "input is lower or equal\n";
        }
    }
}
Last edited on
Topic archived. No new replies allowed.