0

I get segmentation fault at this point in my code and I can't figure out why

string convert(string text, int num_copy)
{
string new_index = NULL, answer;
for (int j = 0; ; j ++)
{
    if (isupper(text[j]))
    {
        new_index[j] = text[j] - 65;
    }
    else if (islower(text[j]))
    {
        new_index[j] = text[j] - 97;
    }
    else
    {
        new_index[j] = text[j];
    }
}    
for (int x = 0; x < num_copy; x ++)
{
    string cipher;
    cipher[x] = new_index[x] + num_copy;
    printf("%c\n", cipher[x]);
    answer = cipher;
}
return answer;
}

this is the whole code

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

string convert(string text, int num_copy);
bool number(int num, string copy);

int main(int argc, string argv[])
{
string text;
//string text = get_string("Plaintext: ");
string copy = argv[1];
int num = atoi(copy);
int num_copy = num;
if (argc != 2)//if argc != 2, program will not run
{
    printf("Usage: ./caesar key\n");
    return 1;
}
else
{
    if (number(num, copy))
    {
        printf("yes\n");
    }
}

text = get_string("Plaintext: "); //plaintext by user
for (int i = 0; ; i ++)
{
    if (isalpha(text[i]))
    {
        //if isupper then leave it as upper
        //if islower then leave it as lower
        convert(text, num);
    }
    else
    {
        //leave it as it is
    }
}
//create a function for caesar
}

string convert(string text, int num_copy)
{
string new_index = NULL, answer;
for (int j = 0; ; j ++)
{
    if (isupper(text[j]))
    {
        new_index[j] = text[j] - 65;
    }
    else if (islower(text[j]))
    {
        new_index[j] = text[j] - 97;
    }
    else
    {
        new_index[j] = text[j];
    }
}    
for (int x = 0; x < num_copy; x ++)
{
    string cipher;
    cipher[x] = new_index[x] + num_copy;
    printf("%c\n", cipher[x]);
    answer = cipher;
}
return answer;
}

bool number(int num, string copy)//function to check if argv is a number
{
for (int q = 0; q != '\0';q ++)
{
    if (isdigit(copy[q]) && (num > 0))//if the key is numeric
    {
        return true;
        
    }
    else//if key is non-numeric
    {
        printf("Usage: ./caesar key\n");
        return false;
    }
}
return true;
}
0

1 Answer 1

0

At a quick glance, I'd say your problem lies here:

string text;
text = get_string("Plaintext: ");

The problem results because text is declared without being initialized or being declared as an array of a specific size. Simply put, because it was only declared, without initialization, it wasn't allocated enough memory to store anything.

Instead, try combining the declaration and the initialization:

   string text = get_string("Plaintext: ");

By doing both in one line, the compiler knows to write machine code that allocates the space required for the text that is entered.

This was just a quick glance inspection. There may be more issues.

Programming tip: When validating input, one should, whenever possible contain the test code in a small block and decouple it from the code that follows. To put it another way, this code checks to see if the parameters are correct, i.e., argc == 2 and then surrounds the bulk of the program in an if code block. Then, the code to handle the case of argc != 2 is buried at the end of the program.

In this case, it is far better to check if argc != 2 at the beginning and exit the program immediately if true. This contains the test in a few lines of code that can stand by itself while the rest of the code can follow this without having to be enclosed in an "if" or an "else" code block.

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

9
  • I used if (argc != 2) initially, then I started overthinking it, like what if the user inputs 3 or 0, but I just realized the condition will still hold....thank you, I'll modify the string text Jul 21, 2020 at 21:51
  • I modified the code but I'm still getting segmentation error Jul 21, 2020 at 21:55
  • I don't know what to tell you. I can't duplicate your seg fault. Did you recompile the code? When I did, I keep getting the incorrect usage error. I also see that get_string(plaintext) is duplicated in the code posted above. I suggest that you go through the code, clean up the copy/paste errors, recompile, and try again. Also, while you did follow my advice on the argc check, there's more to do. You need to remove the else clause and it's associated curly brace pair.
    – Cliff B
    Jul 21, 2020 at 22:23
  • Finally, instead of a for loop, for (; num < (2^31) - 26;) you should use a while loop. Not sure why you're doing that calculation. Since the ^ operator is a bitwise XOR and not exponent, this equation evaluates to 3. What are you trying to do here?
    – Cliff B
    Jul 21, 2020 at 22:24
  • it's supposed too be 2 raised to the power of 31 minus 26 Jul 22, 2020 at 1:57

You must log in to answer this question.

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