0

So here's my code:

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

 int main(int argc, string argv[])
 {
 (void)argc;
 (void)argv;

 //k production function starts here
 int k = get_int("Enter a non negative k: ");

 while (k < 1)
 {

     k = get_int("Enter a non negative k: ");
     if (k >= 1)
     {
         break;
     }
 }
 //return 0; above function works
 //k function ends here



 //cipher function starts here
 string p = get_string("Enter message here: ");
 int i = strlen(p);
 while (true){
     if (isalpha(p[i]))
     {
         break;
     }
     printf("Please enter an alphabetical string to encipher...");
     p = get_string("Enter message here: ");

     if (isalpha(p))
     {
        break;
     }
 }


return 0;
}

And I think I know why I'm getting the segmentation fault (I think it's because of my isalpha function's variable: if (isalpha(p[i])) but I don't know how to pass an integer as the number of characters in the char array.Can you guys give me some pointers? P.S. I know my code is far from done, I'm just trying to get this out of the way first.

1 Answer 1

1

I didn't test all of your code, but you're close. Look a few lines later. This will definitely cause a seg fault:

     if (isalpha(p))

The isalpha() function takes a single char. p is a string. This statement tries to stuff a string down isalpha's throat, so it chokes on it and throws up a seg fault. (Nice analogy, huh? ;-) ) You can pass it a single char, but not a string.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .