Noob question, probably. I've looked through other answers, and I've tried what they've done. But I'm still getting the error every time. This is the first snippet from my caesar cipher program.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
char message[81];
char cipher[81];
do
{
printf("Enter a message: ");
scanf("%[^\n]", &message);
//scanf("%s", &message) = error as well
printf("Test"); //to see if it prints. doesn't print, so seg fault must be on above line
if (!isalpha(message))
{
printf("Invalid input. Enter only letters.\n");
}
else
{
valid = 1;
}
} while (valid == 0);
// ...
As input, I will enter anything - a string, one char, a number, etc, and I still get a seg fault.
I'm compiling my code with -W -Wall -ansi -pedantic , and I'm getting no errors or warnings related to the seg fault.
For starters the type of the argument in this call
is invalid. You need to write
Also you need to remove the new line character '\n' from the input buffer before the next call of scanf.
The function
isalpha
expects an object of the typechar
. But you are passing an expression of the typechar *
.that invokes undefined behavior.
Also a question arises to your code snippet where the variable
valid
is declared.If you want to check whether a string contains alpha symbols or/and maybe white spaces then you need to check all symbols of the entered string.
Here is a demonstrative program.
Its output might look like