Function restarting itself.

Heya,

I was wondering how I should go about fixing this problem I'm having: I made a function that restarts itself, instead of terminating and returning a value to the variable that called it. This is the function:

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
int getNewVal(int z, int u, int s){
	int newValPos = 0;
	int newVal = 0;
	int h = 0;
	int returnVal[8];
	char gridChar;

	std::ostringstream ostr;			//
	ostr << grid[z][u];	//Convert the grid value to a string type.
	std:string placeholderVal = ostr.str();		//
	
	gridChar = -(grid[z][s] - '0') + 6;

	while(((int)placeholderVal[newValPos]) != gridChar && newValPos < 9){
		newVal = (((newVal-48)-0) + placeholderVal[newValPos]);
		returnVal[h] = newVal;
		newVal = 0;
		h += 1;
		newValPos += 1;
	}
	if(((int)placeholderVal[newValPos]) == gridChar && newValPos < 9){
		newValPos += 1;
		while(placeholderVal[newValPos - 1] != '\0' && newValPos < 9){
			newVal = (((newVal-48)-0) + placeholderVal[newValPos]);
			returnVal[h] = newVal;
			newVal = 0;
			h += 1;
			newValPos += 1;
		}
	}
	return (int)returnVal;
}


A couple of things to note are: It's part of a Sudoku-puzzle solving program. The 'grid' variable is a 9 by 9 2-dimensional array, with each of it's 'boxes' filled by the value '123456789'. This function itself is only called once another function (which I haven't posted here) finds a location in the grid where only 1 number has been entered.
The point of this function is to remove that specific number from the corresponding horizontal row in the grid-array. The value that should be returned at the end of the function should be something similar to '123456789' with one of those numbers removed from the list. (That number-to-be-removed is actually stored in the variable gridChar in this function.

Now like I said before, once the function encounters the line

return (int)returnVal;

it restarts itself, where it should actually return that value to the place in the original code where it was called.

Just to be on the safe side, here's the code that calls this function:

grid[z][s] = getNewVal(z, u, s);

with grid[][] being an int[9][9]. z and s are the variables indicating the y and x coordinates respectively of the grid location with that 'number-to-be-removed' and u indicating the x coordinate of on the same y coordinate from the box where the specific number has to be removed from.

Hope it's clear enough for who ever wants to help with this ;)

P.S: Anyone got a simple way to convert an int[] to an int? The returnVal variable is from type int[] and it has to be stored into an int spot on a grid[][] location.

Last edited on
Edit : I see your point with the multiple control for loop too disch, thanks for pointing that out

converting int[] to int;

1
2
3
4
5
6
7
8
9
{
	int f[5] = {1,2,3,4,5};
	int d;
	double c;
	double result = 0.0;
	for (d=4,c=1;d>=0,c<=5; d--,c++) result += f[d] *  (pow (10,c))/10;
	cout << result;
	return 0;
}






b = 16

Last edited on
Thanks for replying BettyBoopTS, but what I want to do is this: I got an int[] with content like {1, 2, 3, 4, 5, 6}. Now I want to convert the entire int[] to a single int position inside of a int[9][9].

So for example:

int a[5] = {1, 2, 3, 4, 5};

would have to become:

int b[2][4] = 12345; (Where the 2 and 4 are just as an example as well.)


And about the main problem (to which the title is not referring, unfortunatly...), anyone got some thoughts on why its doing what its doing?
Last edited on
Simple what is 12345 composed of? Well start of with the size of a which is 5 subtract 1 now you have 4.
Now set up a for loop for 5 iterations, and multiply the content in a by 10^x starting with 10^4 and decrement the powers by 1. 1*10^4 + 2*10^3 + ... You get the picture.
About you return at the bottom its not working because you are casting type int onto a pointer. Arrays without the notation [x] are just pointers essentially. Since you are trying to return 12345, I would just use a sum variable for the for loop I described above and return that instead.
Does that help?
Thanks for your reply. And I must say I find your solution pretty genius ;). So I've tried what you said but now the problem im having is that my returnInt variable, as seen below, get's a value of 13 the first time the for loop is executed. If you're wondering where returnVal[] and the variable h come from, You can check that in the original code i posted in the first post of this thread.

h is just like what you (kevinkjt2000) used in your example as a, therefore size would contain the length of variable returnVal

1
2
3
4
5
6
7
8
	size = h;
	h -= 1;
	for(int a = 0; a < size; a++){
		returnInt += (returnVal[a] * (10 ^ h));
		h -= 1;
	}

	return returnInt;
Last edited on
*bump* anyone?
Edit : I see your point with the multiple control for loop too disch, thanks for pointing that out , silly mistake




this will work for you rycul(lol@bump):

1
2
3
4
5
6
7
8
9
10
11
12

#include <iostream>
#include <math.h>
using namespace std;

int f[5] = {1,2,3,4,5};
	int d;
	double c;
	double result = 0.0;
	for (d=4,c=1;d>=0,c<=5; d--,c++) result += f[d] *  (pow (10,c))/10;
	cout << result;
	return 0;




12345


Last edited on
Thanks BettyBoopTS, that works great :) But the horror hasn't ended yet xD. When I run this code and it get's to that last line again. (which is now return result; it still returns to the start of the function. And after executing a couple more lines of code it gives me an error because a string subscript is out of range. (Ofcourse because it's not meant to run twice so certain variables don't make sense anymore at that point)
Last edited on
*bump* again :p
1
2
3
    double c;
    double result = 0.0;
    for (d=4,c=1;d<=0,c<=5; d--,c++) result += f[d] *  (pow (10,c))/10;


*cringe*

1) Why the love of pow in situations where it's uncalled for?
2) Why use doubles when you're clearly dealing with ints?
3) Why raise 10 to a power, only to divide by 10 afterwards?
4) The comma operator doesn't work like you expect in that for loop. d<=0,c<=5 <- doesn't do what you think.

1 and 2 in particular are things you should really work on, BettyBoop. I see pow and unnecessary double<->int conversions all over in the code snippits you post.

Honestly -- pow really is something you should avoid unless necessary. Pretty much all of <cmath> is. Such heavy computations can get expensive.


This code can be greatly simplified:

1
2
3
int c = 1;
for(int d = 4; d >= 0; --d, c *= 10)
  result += f[d] * c;



EDIT:

My intent is not to sound mean BettyBoop. If I come across that way, I really apologize. I actually have a lot of respect for you and appreciate the help you're giving others.

I just want to help you get pointed in more of the right direction is all =)


When I run this code and it get's to that last line again. (which is now return result; it still returns to the start of the function.


The function is not restarting itself. What's happening is your program is calling this function again after it exits. (EDIT: or your while loop is looping more times than expected? I didn't look that closely at it, honestly)

The problem is not in this function, but is in the function that is calling this function. Can you post how you are calling this?
Last edited on
(throwing self against wall and sobbing uncontrollably!!) jussst kidding!!! : D

Thanks Disch, I appreciate the advice, I am really hoping to get past the unnecessary code I seem to get mired down in a lot of the time. I need to buckle down and start refining the code and the understanding of its intricacies. I hope to keep getting the feedback you all have been giving me, don't stop!
Sure thing Disch. Here's my loop inside of my main function in which the call to the specific function we're talking about is highlighted:

1
2
3
4
5
6
7
8
9
10
11
for(int z = 0; z < 9; z++){
	for(int s = 0; s < 9; s++){
		if(countvalues(z, s) == 1){
			for(int u = 0; u < 9; u++){
				grid[z][u] = getNewVal(z, u, s);
			}
			for(int u = 0; u < 9; u++){
			}
		}
	}
}


The first two for-loops are there so that the entire [9] by [9] grid is covered.

EDIT: In case you need it: My 'countvalues' function:

1
2
3
4
5
6
7
8
9
10
11
12
int countvalues(int z, int s){	//Returns the number of values held by grid[z][s].
	int numbOfVal = 0;
	std::ostringstream ostr;
	ostr << grid[z][s];
	std:string gridstorage = ostr.str();
	
	while(gridstorage[numbOfVal] != '\0'){
		numbOfVal += 1;
	}
	
	return numbOfVal;
}


EDIT 2:Nevermind the above information, that's resolved. But I just found a weird thing, it keeps returning to the function because its in a loop ofcourse, but the second time the function is executed, it returns a value of -48 instead of 12456789 like it should do, given the way I was testing it. (With that I mean that '3' should be removed from '123456789', which it does for the first execute). Also, the third time it's executed it WANTS to return a value of 12456789 (like it should) but then it gives me the error

Run-Time Check Failure #2 - Stack around the variable 'returnVal' was corrupted.
Last edited on
once again, I'm bumping this thread ;)
I am going to get my white-out and cover up all your strikethrough! so there! :p
Haha BettyBoopTS, the problems I had are all solved at the moment so I guess I'll mark this thread as solved! Also, for the sake of the internet in it's problemsolving form I'm going to remove all strikethrough so that other people can review this thread in all of it's glory if they find themselves in any similar situation as I have been in ;)

In case you're wondering what ever happened to my 'bug' where it would return -48, that was because the function was called even when there wasn't anything the function should be changing. A basic, simple if-statement fixed that. (Seriously, it's nothing more than if(u != s){ ...} XD.

Thanks again everyone who helped me in here, we'll be seeing eachother again sometime I forsee ;)

Last edited on
Topic archived. No new replies allowed.