0

https://pastebin.com/giThivuC

The above is part of my work.I would like to know how to ensure the user input the alpha after ./vigenere ? Coz k = atoi(argv[1]) seems doesn't work. Besides, what is segmentation fault?

2 Answers 2

2

Unlike Caesar, the key is now a string, not an integer. Why is the code trying to convert argv[1] to an int with a call to atoi()?

A segmentation fault is a runtime error where the program is attempting to access an unauthorized area of memory. In this case, it's a common mistake by new programmers. The isalpha() function takes a single char and checks whether it is a letter or not. Now, remember that argv[ ] is an array of strings (actually an array of char arrays). The code above, isalpha(argv[1]) tries to shove an entire string down it's throat and it chokes, throwing a seg fault error. The code has to look at each individual character in the string, one at a time, using isalpha. Good place to use a for loop to check argv[1][x].

Finally a hint. Chars can be treated as unsigned, one byte integers. You don't have to convert them using atoi or any other function. You might want to do some basic math on them, depending on your logic, and that is allowed.

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

-2

Maybe you have to check if (isalpha(argv[1])) before of use atoi function,

You have at line 15:

k = atoi(argv[1]);
if (isalpha(k))
{
   ...
}

Try this way:

if (isalpha(argv[1]))
{
   k = atoi(argv[1]);
   ...
}

Segmentation fault indicates that you're trying to read/write on something that you shouldn't.

2
  • No, this will still generate a seg fault.
    – Cliff B
    Jan 25, 2018 at 20:34
  • isalpha() accepts only char type variables and as @CliffB said passing strings to isalpha() will cause segmentation fault Jan 27, 2018 at 10:04

You must log in to answer this question.

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