Hi all, i have written a program to demonstrate while loops and arrays however when i exit the program by pressing n i get the following error message.
*** stack smashing detected ***: <unknown> terminated Aborted (core dumped)
process returned 134 (0x86) etc
this only happens when i try to quit the program after the first time round.
here is the code
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=0;
int i;
char answer;
char words[4];
while (a != 2)
{
//check to see if first time run and ask the appropriate question
if (a == 0)
{
printf("Would you like to enter a word y/n\n");
scanf(" %c",&answer);
}
else if (a == 1)
{
printf("Would you like to enter another word y/n\n");
scanf(" %c",&answer);
}
else
{
printf("Sorry I didnt understand that Please enter "y" to enter a word or "n" to quit\n");
scanf(" %c",&answer);
}
//check what the answer was and run for loop if need be
if (answer == 'y' || answer == 'Y')
{
printf("Please enter a five letter word\n");
scanf(" %s",words);
for (i = 0; i < 5; i++)
{
printf("The letter in posistion %d is %c\n",i ,words[i]);
}
printf("the word entered was %s\n",words);
a=1;
}
else if (answer == 'n' || answer == 'N')
{
printf("goodbye!!\n");
a=2;
}
else // somthing else typed in // todo error trapping for more than one character entered
{
a=3;
}
}
return 0;
}
many thanks
Coop