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.

2

There are 2 best solutions below

7
On

For starters the type of the argument in this call

scanf("%[^\n]", &message);

is invalid. You need to write

scanf("%[^\n]", message);

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 type char. But you are passing an expression of the type char *.

if (!isalpha(message))

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.

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

int all_letters( const char *s )
{
    while ( *s && isalpha( ( unsigned char )*s  ) ) ++s;
    
    return *s == '\0';
}

int main(void) 
{
    char message[81];
    int valid = 0;
    
    do
    {
        printf( "Enter a message: " );
        scanf( "%80[^\n]", message );
        while ( getchar() != '\n' );
        
        valid = all_letters( message );
        
        if ( !valid )
        {
            printf( "Invalid input. Enter only letters.\n" );
        }
    } while ( !valid );
    
    return 0;
}

Its output might look like

Enter a message: 123
Invalid input. Enter only letters.
Enter a message: Hello
9
On

I ran this code under valgrind and the error is actually at this line here:

if (!isalpha(message))

Remember that isalpha takes in a char (technically an int, but it's expected to just hold a single character). By passing in message, you're passing in a char [81], which isn't of the right type.

What mystifies me is why this compiles and runs at all, even with the warning level cranked up to the maximum, since there's no implicit conversion from a char [81] to a single char.

To fix this, you'll need to change the code you've written here so that it more explicitly tests that all the characters entered are letters. You may also want to check the return value from scanf to make sure that it doesn't fail and to clear the invalid characters from stdin in the event that it fails.