I'm working on my PSet2 Caesar Problem. After finishing my code here is the mistakes I got. Any advice how to fix them? Really appreciated.
caesar.c:46:10: error: use of undeclared identifier 'i'
for (i = 0; i < strlen(plaintext); i++)
^
caesar.c:46:17: error: use of undeclared identifier 'i'
for (i = 0; i < strlen(plaintext); i++)
^
caesar.c:46:40: error: use of undeclared identifier 'i'
for (i = 0; i < strlen(plaintext); i++)
^
caesar.c:48:31: error: use of undeclared identifier 'i'
if (isupper(plaintext[i]))
^
caesar.c:50:39: error: use of undeclared identifier 'i'
printf("%c", (((plaintext[i] - 65) + k) %26) + 65);
^
caesar.c:52:36: error: use of undeclared identifier 'i'
else if (islower(plaintext[i]))
^
caesar.c:54:39: error: use of undeclared identifier 'i'
printf("%c", (((plaintext[i] - 97) + k) %26) + 97);
^
caesar.c:58:36: error: use of undeclared identifier 'i'
printf("%c", plaintext[i]);
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
//Check that there is one command-line argument
if (argc != 2)
{
printf ("Usage: ./caesar key\n");
return 1;
}
else printf ("Success!\n");
//Define the key
string key = argv[1];
//Check if input is a digit
for (int i = 0; i < strlen(argv[1]); i++)
{
if (!isdigit(argv[1][i]))
{
printf ("Usage: ./caesar key\n");
return 1;
}
else printf ("Success!\n%s\n", key);
}
//Get plain text from user
string plaintext = get_string("Plaintext: ");
//Define key
int k = atoi(key);
printf("ciphertext: ");
//Obtain ciphertext
for (i = 0; i < strlen(plaintext); i++)
{
if (isupper(plaintext[i]))
{
printf("%c", (((plaintext[i] - 65) + k) %26) + 65);
}
else if (islower(plaintext[i]))
{
printf("%c", (((plaintext[i] - 97) + k) %26) + 97);
}
else
{
printf("%c", plaintext[i]);
}
}
printf("\n");
}
"use of undeclared identifier 'i'" means that you have not declared the identifier i
before using it - the compiler doesn't recognize the name.
In this case the code should be for (int i = 0; ...
Good to see you're doing cs50. You have missed a little, yet important thing. Since you have not declared the variable i
outside any loop, thus, it is only available to that loop. So, when you are using it anywhere outside the loop, it's actually undeclared variable, and that's what is happening here. So, you need to declare i
in the last for
loop too. I've attached the modified code.
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
//Check that there is one command-line argument
if (argc != 2)
{
printf ("Usage: ./caesar key\n");
return 1;
}
else printf ("Success!\n");
//Define the key
string key = argv[1];
//Check if input is a digit
for (int i = 0; i < strlen(argv[1]); i++)
{
if (!isdigit(argv[1][i]))
{
printf ("Usage: ./caesar key\n");
return 1;
}
else printf ("Success!\n%s\n", key);
}
//Get plain text from user
string plaintext = get_string("Plaintext: ");
//Define key
int k = atoi(key);
printf("ciphertext: ");
//Obtain ciphertext
for (int i = 0; i < strlen(plaintext); i++) // declared i
{
if (isupper(plaintext[i]))
{
printf("%c", (((plaintext[i] - 65) + k) %26) + 65);
}
else if (islower(plaintext[i]))
{
printf("%c", (((plaintext[i] - 97) + k) %26) + 97);
}
else
{
printf("%c", plaintext[i]);
}
}
printf("\n");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.