IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2: [Arrays] – Functions, Variable and Scope, Debugging, Arrays, and Command Line Arguments › Caesar project: error: redefinition of ‘i’
Tagged: arrays, Caesar project
- This topic is empty.
-
AuthorPosts
-
January 12, 2022 at 9:29 am #147
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include<stdio.h> #include<cs50.h> #include<string.h> int main(void) { string name = get_string("Enter: "); printf("%s\n", name); int n = strlen(name); printf("%i\n", n); for (int i = 0, i < n, i++) while name[i] != ("\0"); printf("%i", name[i]); }
[/dm_code_snippet]
Getting this error message (with helpcs50 make caesar):
[dm_code_snippet background=”yes” background-mobile=”no” slim=”no” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
Asking for help... caesar.c:10:16: error: redefinition of 'i' Not quite sure how to help, but focus your attention on line 10 of caesar.c! caesar/ $
[/dm_code_snippet]
Unable to figure out redefinition of i, i is not defined earlier in this code. %i on the previous line is something different.
Update:
After getting replies and reworking, here is the code that seems to achieve the intermittent goal of finding ASCII code of entered characters. There was no need of while loop and it was an error as the same leads to infinite loop. Even if there was a need, I should perhaps have opted for if command.
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include<stdio.h> #include<cs50.h> #include<string.h> int main(void) { string name = get_string("Enter: "); printf("Entered text by user: %s\n", name); int n = strlen(name); printf("lenght of entered text: %i\n", n); for (int i = 0; i < n; i++) { printf("ASCII code %i\n", name[i]); } }
[/dm_code_snippet]
Reply
https://edstem.org/us/courses/176/discussion/996396[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.