need help fast

please explain me why this does not work and show me the correct form
#include <iostream>

using namespace std;

int main()
{int x,y,s;
cout<<"x=";
cin>>x;
while(x>0)
{cout<<"y="; cin>>y; if(x>y) s=x%10 ; else if(y>=x) s=y%10; x=y;};
cout<<"s="<<s<<endl;

return 0;
}

please tell me what is wrong and help me to fix it
How do you know there is something wrong with it?

Maybe it's working exactly correctly. It's hard for us know, since we have no idea what it's meant to do.
Repeater is right we don't know what it is supposed to do. I see some things that maybe your problem. First, please use code tags when posting code, it makes it easier to read. Next, space out your code, use brackets ({}) have white space, make it easier to read. Next, for the actual problems with the code. You don't need a ; at the end of a while loop, only a do-while. Next, I see you're running your loop while x > 0 but the only way to get x to be less than 0 you to enter y as a 0 or negative number. This technically works but I feel like that would through off whatever calculation you're doing but I don't know what calculation you're doing so I don't know if it is. You need to give more info. What problem are you trying to solve? Also, naming your variables better would be nice.

Heres something of what you code should look like, just needs some comments on whats going one.

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>

using namespace std;

int main()
{
	int x, y, s;

	cout << "x=";
	cin >> x;

	while (x>0)
	{
		cout << "y=";
		cin >> y; 

		if (x > y) {
			s = x % 10;
		}
		else if (y >= x){
			s = y % 10;
			x = y;
		}
	}
	cout << "s=" << s << endl;

	return 0;
}
Last edited on
Hello,

I don't know what you are trying to do, but creating a while loop with an unconditional return 0 in it seems weird to me.
I think you did not check your brackets. If you were using more conventional indentation of your code (and the the code-tags when posting it) this would probably become easier to notice and prevent.

Kind regards, Nico
It does not shows the solution. When it is running it shows x= and y= on and on.
Your while loop ends when x becomes zero or less.

So in the while loop, you need to make x become zero or less.

Why does you user enter y every time round the loop? Seems like they should only enter a value for y once.
Last edited on
so is there anything I can do to make x become 0 or less?
Well, sure. You could have

x = 0;

in it.

But what's meant to happen? What's it meant to do?
Topic archived. No new replies allowed.