Here is my code to print the string
char accname[MAX][MAXSTRING], transname[MAX][MAXSTRING];
printf ("Enter title for new account: ");
accname[i][i] = validatestring();
printf ("\n");
printf ("Enter title for transaction: ");
transname[i][i] = validatestring();
printf ("\n");
printf ("%s %s", accname[i], transname[i]);
my code for validatestring()
char validatestring() {
int keeptrying = 1, rc;
char i[31];
do
{
rc = scanf("%30[^\n]", &i);
if (rc == 0)
{
printf (" **Invalid input try again: ");
clear();
}
else if (getchar() != '\n')
{
printf (" **Title is longer than 30 characters: ");
clear();
}
else
{
keeptrying = 0;
}
} while (keeptrying == 1);
return i;
}
at the printf stage for accname[i] / transname[i] i don’t get the value I entered, I get weird conversion string types, sometimes I got the first character of my input but now I get something completely different. Can anyone figure out why?
Your
validatestringfunction is hopelesely broken.Firstly, you declared
validatestringas returningchar. Yet you attempt too return achar *from it (seereturn i, whereiischar [31]). This should not even compile.Secondly, if you intend to return a string from
validatestring, you have to make sure that you are not returning a pointer to a local buffer from it.iis a local array. A pointer toicannot be returned fromvalidatestring.