During a university project, I had to print some int arrays to the screen, and I wanted to do it with printf.
So I decided to include a function that converts an integer array into a string type, and passes the string out to the calling function.
But the function in question crashes the program after calling it for the second time in show().
And I can't figure out why... Any help would be appreciated.

#include <stdio.h>
#include <ctype.h>
#include <conio.h>

#define SIZE_A      30

unsigned arraylen (int _a[])	/*(Inserted Int Array)*/
{
    unsigned int _cntr=0;
    
    while (_a[_cntr]!='\0' && _cntr<SIZE_A)		//
		_cntr+=1;
    return _cntr;
}

char Int2Str (int _rrA[SIZE_A], char _XtStr[SIZE_A])	/*(Inserted Array)*/
{
	unsigned _cnt=0;
	
	while (_XtStr[_cnt]= (isdigit(_rrA[_cnt]) && _cnt<arraylen(_rrA)) ? _rrA[_cnt] : '\0')
		_cnt+=1;
	return;
}

void show (int _InSq[SIZE_A], int _KSq[SIZE_A], int _OutSq[SIZE_A], char _cs)	/*(String for Output, Key String, String from Input, Choice)*/
{
	char _InStr[SIZE_A], _KStr[SIZE_A], _OutStr[SIZE_A];
	
	Int2Str(_InSq, _InStr);
	Int2Str(_KSq, _KStr);
	Int2Str(_OutSq, _OutStr);
    if (_cs=='q' || _cs=='Q')
		printf("\nThe number %s encoded with the key %s is %s.\n\n", _InStr, _KStr, _OutStr);
	else if (_cs=='w' || _cs=='W')
		printf("\nThe number %s decoded with the key %s is %s.\n\n", _InStr, _KStr, _OutStr);
		 else {
					puts("Shit");
					puts("\t->This error derived from a misconduct during the printing function");
			  }
	return;
}

int main (void)
{
     int shh, i=0, array1[SIZE_A], array2[SIZE_A], array3[SIZE_A];
     char chRA[SIZE_A], choice, throwaway;
     puts("Choice:");
     choice=getchar();
     throwaway=getchar();
     puts("Array 1");
     while (array1[i]= (isdigit(shh=getchar()) && i<SIZE_A) ? shh : '\0')
              i++;
     puts("Array 2");
     while (array2[i]= (isdigit(shh=getchar()) && i<SIZE_A) ? shh : '\0')
              i++;
     puts("Array 3");
     while (array3[i]= (isdigit(shh=getchar()) && i<SIZE_A) ? shh : '\0')
              i++;
     Int2Str(array1, chRA);
     puts(chRA);
     system("pause");
     show(array1, array2, array3, choice);
     system("pause");
     return;
}

The problem appears at line 30 while calling Int2Str() for second time while in show().

Take a step back and ask yourself, WHY?

You can use printf() to print out your int array, in a loop, and control just exactly how you want it to appear. So why change the data type, at all?

But let's say you want to really use a char for printing out your int's. What are you going to do when your small range of a char, is exceeded?

I'm just bothered by the choice to go from the direct, to some circuitous route just to print up an array.

Rube Goldberg would love this, but it really bothers me. Google that name if you don't know what it stands for.

Comments
I thought about the Rube Goldberg thing too.

I know, you are right saying that the other way it is more simple and all, but the point is why isn't this one working.
I used the printf in loop, and it is working quite well, but...
It should be working with the Int2Str function.
And I just wonder why it doesn't.

When a function says it shall return a char, and it returns nothing instead. I don't have a lot of confidence that it's working right. ;)

then there's this:

while (_XtStr[_cnt]= (isdigit(_rrA[_cnt]) . . .

Which should be:

while (_XtStr[_cnt]==(isdigit(_rrA[_cnt]) . . .

and a few other compiler errors and warnings. You should at least try to compile your code, and pay attention to the errors that stop it from compiling. There are some other errors, but I'm not doing your homework for you.

If you can't be bothered to pay attention to your compiler errors, there's no reason why I should do it for you.

Edited 6 Years Ago by Adak: n/a

Compiler does not show any errors...
I dont think I would ask for insight if I had something to work with.

Thanks

Edited 6 Years Ago by Yggdrasil: n/a

This question has already been answered. Start a new discussion instead.