i need help again :)))

please tell me why it does not work

1
2
3
4
5
6
7
8
9
10
11
  int x,y,c,n;
cout<<"x=";
cin>>x;
n=0;
while(x!=0) {x=y;c=0;
                while(y>0){if(y%10>c) c=y%10 ;y=y/10;}
            n=n*10+c; cin>>x;}
cout<<"n="<<n<<endl;

    return 0;
}

I don't know what this program is supposed to do,it is just my homework for tomorow so please help me as fast as you can
y appears to be used without being assigned a value, producing random results.
x had a valid value but you overwrote it with y.
Last edited on
Hello queen13,

This should give yo an idea of what your program is doing. With my VS IDE I had problems getting the program. It might work for you. If it does either you have an old compiler or you may need to change some settings.

The comments in the program show what I received when I tried to compile the program. Line 3 is what I had to use just to get the program to compile. Then it crashed when the program ran.

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>

//#pragma warning(disable : 4700)  // <--- You may or may not need this line.

int main ()
{
	int x, y, c, n;
	
	// Error C4700	uninitialized local variable 'n'
	// Same error for the other variables.

	std::cout << "\n x = " << x << ", y = " << y << ", c = " << c << ", n = " << n << std::endl;

	// What I finally got it to produce.
	// x = -858993460, y = -858993460, c = -858993460, n = -858993460

	std::cout << "Enter a value for x: ";
	std::cin >> x;

	n = 0;

	while (x != 0)
	{
		x = y; c = 0;
		while (y>0)
		{ 
			if (y % 10>c) c = y % 10; y = y / 10;
		}

		n = n * 10 + c; std::cin >> x;
	}

	std::cout << "n=" << n << std::endl;

	return 0;
}


What this means is that you need to initialize your variables. The simplest way would be:int x{}, y{}, c{}, n{}; . The empty {}s will set the numeric variables to zero.

It is always a good practice to initialize your variables when defined. This program is a good example of what happens when you do not initialize your variables.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.