printing char array recursive

User enters char array from and I need to print only the ones he used
(there are 9 slots he can used) couldn't figure why this doesn't work.
all the slots are empty before


void printstring(char strings[][STRING_SIZE], int number)
{
if (number == 10)
{
return;
}
if (strings[number][STRING_SIZE] != "")
{
printf("%d)", number+1);
printf("%4s\n", strings[number]);
}

printstring(strings, number + 1);


}

if (strings[number][STRING_SIZE] != "")
what is this supposed to do? It is comparing a character to a string. It may be ok, but it may be better to check against zero or something explicit.

also, your explanation of the problem makes no sense, the user has all empty slots before (before what? a hidden scanf statement? the superbowl? The apocalypse?) something and only print the ones used (so, could enter less than 9? is that what you mean?) etc?

why is it a 2-d array? An array of strings, so are there 9 letters or 9 strings?
you treat it partly like characters and partly like strings, in a confusing way.
Last edited on
Do you mean something like:

1
2
3
4
5
6
7
8
9
void printstring(char strings[][STRING_SIZE], int number)
{
	if (number < 10) {
		if (strings[number][0] != 0)
			printf("%d) %s\n", number + 1, strings[number]);

		printstring(strings, number + 1);
	}
}

Hello jordandan,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



The next tip: It is always best to provide the full program code or enough code to demonstrate your problem. This should be code that can be compiled and run. The shell program here can be very helpful.

The function that you posted may be a problem in part or in whole, but the problem may start before you get to this function.

You wrote:

User enters char array from and I need to print only the ones he used


My question here is what is in the array before you change it with input?

If you do not initialize the array before you use it there is no way to know what garbage your if statement is trying to compare to.

Here the full code would be helpful to see what you are doing before the function call.

Andy
Topic archived. No new replies allowed.