Need help with this for loop please, can't make it work.

it works through the cout << test print for smallDigitLoop 2. but can't get the rest of the code to work they way I want to. trying to compare all the values in the array and store the largest digit to compareIntGreater.

thanks for any advice.

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
void printSmallestLargestDigit(int iVal) {
	
	int tempInteger = iVal;
	int	count = 0; 
	int countTwo = 0;
	int modifyIValTwo = iVal;
	int modifyIVal3 = iVal;

cout << "test print for smallDigitLoop" << endl;	
	do {									// Determine the number of digits of the integer

         tempInteger = tempInteger / 10;

         count++;
		 countTwo++;

     } while (tempInteger != 0);

	

	int* digit = new int[count];			// Dynamically allocating array to store all the digits

	int* digitTwo = new int[countTwo];

	for(int index = count - 1; index >= 0; index--) {		// Find the remainder, then move 1 place to the left

        digit[index] = modifyIValTwo % 10;

        modifyIValTwo = modifyIValTwo / 10;

    }

	cout << "test print for smallDigitLoop 2" << endl;
	
	for(int indexTwo = countTwo - 1; indexTwo >= 0; indexTwo--) {
		
		digitTwo[indexTwo] = modifyIVal3 % 10;

        modifyIVal3 = modifyIVal3 / 10;

		
		if(compareIntGreater > digitTwo[countTwo]) {
		return;
		} else if(digitTwo[countTwo] > digitTwo[countTwo--]) {
		  compareIntGreater = digitTwo[countTwo -1];
		  return;
		} else(digitTwo[countTwo] < digitTwo[countTwo--]);
		  compareIntGreater = digitTwo[countTwo--];
	}
cout << "test print for smallDigitLoop 3" << endl;	
	cout << "Greatest digit: " << compareIntGreater << endl;

	delete [] digit;
	delete [] digitTwo;

    digit = 0;
	digitTwo = 0;
	
return;
}
closed account (owX8T05o)
I don't know if the it's the problem but,

When you do digitTwo[countTwo--] --> countTwo = countTwo-1

Since countTwo is used in the loop maybie it's the reason ... I'd put debugging points to be sure.
closed account (owX8T05o)
Ok i've run your code. The problem (maybie) is the return in the code :

if(compareIntGreater > digitTwo[countTwo]) {
return;
} else if(digitTwo[countTwo] > digitTwo[countTwo--]) {
compareIntGreater = digitTwo[countTwo -1];
return;
} else(digitTwo[countTwo] < digitTwo[countTwo--]);
compareIntGreater = digitTwo[countTwo--];

return will escape from main.
I've tried to comment these lines and the execution goes trougth the second loop.
Maybie you meant break and not return ?

I've not tested the code so far, can't tell if it works properly.

Bye
Don't write codes like this.
(digitTwo[countTwo] > digitTwo[countTwo--])
The value of this statement is always false as it is equal to this.
1
2
(digitTwo[countTwo] > digitTwo[countTwo]);
countTwo--; 

Maybe you wanted to write (digitTwo[countTwo] > digitTwo[--countTwo]) ;
However, this is also dangerous because we don't know exactly how the compiler will deal with this statement, the right side will be calculated first, or the left side?
else if(digitTwo[countTwo] > digitTwo[countTwo--])

Never do this. There's no way of knowing whether or not the -- operator will be evaluated before the first countTwo expression is evaluated. You get undefined behavior.

That is... if you have the below code:
1
2
int a = 1;
if( foo[a] > foo[a--] ) // <- 


that line might compare foo[0] to foo[1]. Or it might compare foo[1] to foo[1]. There's no way to know which you'll get.


EDIT: chenqi beat me to it, although note the behavior is undefined so it won't necessarily always be true. The actual result you get is completely unreliable.
Last edited on
Topic archived. No new replies allowed.