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().