0

Trying to generate loop to check for alphabetical inputs in the user command argument.

But i get a segmentation fault when testing it.

error outcome.

~/Week_2/pset2/ $ ./caesar a
Segmentation fault

the aim is to obtain the "correct" input from the user and "reject" incorrect inputs;

correct input - ./caesar 2 incorrect input - ./caesar a or ./caesar 2 5

full code below:

 // INCLUDE THE APPROPRIATE LIBRARIES

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


int eoc;
int feoc;
int eolc;
int feolc;

// INITIALIZE PROGRAMME THAT REQUIRES A COMMAND 
LINE ARGUMENT

int main (int argc, string argv[])

// OBTAIN A VALID KEY INPUT FROM USER e.g: 
./caesar 2
{
int minimumkeyinput = 2;

if (argc < minimumkeyinput || argc > 
minimumkeyinput)
{
printf("Caesar requires a single Key input in 
order to execute e.g.           
./caesar  2\n");
return 1;
}

int i = 0;
string key = argv[1];
int g = strlen(key);

for(; i < g; i++)
{

if (isalpha (key))
{

    printf("Caesar requires a single NUMERICAL 
Key input in order to      
execute e.g. ./caesar  2\n");
    return 1;

}

}

// KEY CONVERSION (kc) DECLARED AS AN INT 
CONVERTED FROM ARGV

int kc = atoi(argv[1]);

if (kc < 0)

// ENSURE A POSITIVE INTEGER IS GIVEN AS 
PROGRAMME "KEY"
{
printf("programme requires a positive integer in 
order to execute\n");
return 1;
}

// OBTAIN PLAIN TEXT INPUT (pti) FROM USER

string pti;

do

{

pti = get_string("plaintext: ");

}

while (pti == NULL);

// BEGIN ENCRYPTION PROCESS OF PLAINTEXT INPUT

printf("ciphertext: ");

int ptic = strlen(pti);

// ITERATE OVER PLAINTEXT

int ptii = 0;

for (; ptii < ptic; ptii++)

{
// SHIFT THE CHARACTERS OF PLAINTEXT INPUT (pti) 
TO ENCRYPTED OUTPUT (eo) 
 BY USER KEY CONVERSION (kc)

// CONVERT USER INPUTS ENCRYPTION RESULT INTO AN 
ALPHABETICAL INDEX

// LOOP AROUND THE APLHABET IF REQUIRED BEFORE 
PRINTING TO SCREEN 
(UPPPERCASE ASCII 65-90)

eoc = (pti[ptii] -65 + kc) %26;

// LIMITING THE SCOPE OF THE UPPERCASE LOOP

// CONVERT CHARACTERS FROM ALPHABETICAL INDEX 
BACK TO ASCII INDEX 
 (UPPERCASE)

if (isalpha (pti[ptii]))

{

    if isupper (pti[ptii])

    {

        feoc = eoc + 65;
        printf("%c", feoc);

    }

    // LOOP AROUND ALPHABET IF REQUIRED BEFORE PRINTNG TO SCREEN 
   (LOWERCASE ASCII 97 - 122)

    // LIMIT THE SCOPE OF THE LOWERCASE LOOP

    if islower (pti[ptii])

    {

        eolc = (pti[ptii] -97 + kc) %26;

        // CONVERT CHARACTERS FROM ALPHABETICAL 
 INDEX BACK TO ASCII INDEX 
  (LOWERCASE)

        feolc = eolc + 97;

        printf("%c", feolc);

    }

}

if (isalpha (pti[ptii]) == false)

{

    printf("%c", pti[ptii]);

}

}

printf("\n");

 }
1
if (isalpha (key))

In this line, isalpha is written to take an int, but key is a string. There's no automatic conversion from string to int, the way there is from char to int. I think you intend to write isalpha(key[i]) instead.

Also, it's not an error, but you have some variables that are outside of the scope where they're used. An example is:

int i = 0;
int g = strlen(key);

You never use these variables again, so it's good practice to declare them in the for (... part, so they won't be hanging out for the rest of your program. (This gets more important when the programs you write get longer. You just want to have the variables around in the scope they're needed, not floating around available outside that.)

| improve this answer | |
  • dude thank you! really happy because this means I was closer to the solution then i thought i was. – Pot Noodle Jun 16 at 21:14

You must log in to answer this question.

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