Whats wrong with my code?

Whats wrong with my code?

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
#include <iostream>
using namespace std;
int main()
{
	int x,y;
	cin >> x, cin >> y;
	 
	do
	{
		cout << "the smaller value is "  x

	} 
	while (x < y);
		
		do
		{
			cout << "the larger value is: " x

		} 	
	while (x > y);

			do {
				cout << "the smaller value is " y

			} 		while (y > x);
		
				do
				{
					cout << "the larger value is  "  y
				} 				while (y > x);
					return 0;

		
	
	

}
I fixed the semi-colon issue but now the program keeps going and going , etc...

How do I make it stop?
https://en.wikipedia.org/wiki/Indentation_style
If your indentation is all over the place, it's impossible to tell just from looking at it where you're going wrong.

And post the latest code you have.
This is the code formatted and the cout syntax errors fixed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{
	int x {}, y {};
	cin >> x, cin >> y;

	do {
		cout << "the smaller value is " << x;
	} while (x < y);

	do {
		cout << "the larger value is: " << x;
	} while (x > y);

	do {
		cout << "the smaller value is " << y;
	} while (y > x);

	do {
		cout << "the larger value is  " << y;
	} while (y > x);
}


The program keeps going as within the do loop (s), the value(s) of x and y aren't changed. So if the while condition is true first, it's always true - hence the infinite loop.

Perhaps you want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
	int x {}, y {};
	cin >> x, cin >> y;

	if (x == y)
		cout << "they are the same " << x << '\n';
	else if (x < y) {
		cout << "the smaller value is " << x << '\n';
		cout << "the larger value is  " << y << '\n';
	} else {
		cout << "the smaller value is " << y << '\n';
		cout << "the larger value is: " << x << '\n';
	}
}

I still dont understand why my code goes on forever?
what do you think happens when you have code like this:

do
not_change_x_or_y();
while(x > y)
...and x is actually > y, how does it stop?
you have to change x or y inside the loop, or the condition is always true. its something like
x = 10
y = 0
is 10 > 0? Yes, loop!
is 10 > 0? yes, loop!
is 10> 0? yes, loop!
... and on and on it goes.

Why did you use int x {}, y {} and why doesnt int x = 0 , y = 0 work?
It's just an alternative initialization style. You can use = 0 if you want.
{} has some advantages with objects. For integers, its just the current trend to use the braces on everything.
For integers, its just the current trend to use the braces on everything

It does prevent narrowing conversions in the initialization. For example this won't compile:
1
2
double x = f();
int y{x}; // error 

Supposedly this feature can prevent errors, but I've never had the issue it tries to prevent.
Last edited on
Hello Pepeforever,

I think it is time to go back to the beginning and start with answering these questions:

1. Explain what the purpose of the program is and what you want it to do.

2. Explain why have you chosen a do/while loop.

3. Do you know the difference between an if/else, for loop, do/while loop and while loop?

4. What IDE and operating system are you using? Please mention so people will know what you are working with.

Along with what jonnin posted earlier this should help you understand what you program is doing.

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

using namespace std;  // <--- Best not to use.

int main()
{
    int x{}, y{};  // <--- ALWAYS initialize your variables.

    std::cout << "\n Enter 2 numbers (1 2): ";  // <--- ALWAYS provide a prompt before input.
    cin >> x >> y;

    do
    {
        cout << "\n The smaller value is " << x << '\n';

        std::cout <<
            "\n    The value of 'x' is: " << x <<
            "\n    The value of 'y' is: " << y << '\n';
    } while (x < y);

    do
    {
        cout << "\n The larger value is: " << x << '\n';

        std::cout <<
            "\n    The value of 'x' is: " << x <<
            "\n    The value of 'y' is: " << y << '\n';
    } while (x > y);

    do
    {
        cout << "the smaller value is " << y << '\n';

    } while (y > x);

    do
    {
        cout << "the larger value is  " << y << '\n';
    } while (y > x);

    return 0;
}


Given the x = 5 and y = 10 you get this:


 Enter 2 numbers (1 2): 5 10

 The smaller value is 5

    The value of 'x' is: 5
    The value of 'y' is: 10

 The smaller value is 5

    The value of 'x' is: 5
    The value of 'y' is: 10

 The smaller value is 5

    The value of 'x' is: 5
    The value of 'y' is: 10

 The smaller value is 5

    The value of 'x' is: 5
    The value of 'y' is: 10



Given that x = 10 and y = 5 you get this:

 Enter 2 numbers (1 2): 10 5

 The smaller value is 10  // <--- From the 1st do/while.

    The value of 'x' is: 10
    The value of 'y' is: 5

 The larger value is: 10  // <--- From the 2nd do/while.

    The value of 'x' is: 10
    The value of 'y' is: 5

 The larger value is: 10

    The value of 'x' is: 10
    The value of 'y' is: 5

 The larger value is: 10

    The value of 'x' is: 10
    The value of 'y' is: 5

From the above code put a break point on lines 14 and 23 to watch what your program is doing.

If you are not sure about anything just ask.

It also helps to compile your code before you post it. This way you can include the complete error message(s) that you do not understand.

Andy
Topic archived. No new replies allowed.