Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
642 views
in Technique[技术] by (71.8m points)

cs50 - array subscript is of type 'char' [-Werror,-Wchar-subscripts] - C - Coding a Cypher (Caesar)

There are other threads related to this error, but none have solved my problem. Thank you for taking the time to read this - any help is appreciated!

I am coding a cypher in which you are meant to receive plaintext via user input and encode the message using a "key" entered by the user as a command-line argument to create the cyphertext. Only letters within the plaintext should be "shifted" using the key, while numbers and punctuation remain unchanged.

As of now, I believe I have the correct equations and structure in place, but I have multiple instances of the following error:

error:

array subscript is of type 'char' [-Werror,-Wchar-subscripts]"

I have tried casting different areas of this code as (char) to see if this solves the problem, but as I have been unable to find a fitting solution online, I'm stuck on where to go from here.

To ensure I am not going against the policies of the CS50 course, I will only be copying the necessary section of code.

string text = get_string("plaintext: ");
// ci = (pi + k) % 26 
for (char i = 0; i < strlen(text); i++)
{
    if (isupper(text[i]))
    {
        printf("%c", ((((text[i] + k) - 'A') % 26) + 'A'));
    }
    else if (islower(text[i]))
    {
        printf("%c", ((((text[i] + k) - 'a') % 26) + 'a'));
    }
    else
        printf("%c", text[i]);
printf("ciphertext: %s
", c);
}

Terminal Window w/ Errors

question from:https://stackoverflow.com/questions/66050873/array-subscript-is-of-type-char-werror-wchar-subscripts-c-coding-a-cyp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...