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 › Help for a formatting error
- This topic is empty.
-
AuthorPosts
-
January 26, 2022 at 5:47 am #164
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” 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> #include<ctype.h> int main(void) { string name = get_string("Enter: "); printf("Entered text by user: %s\n", name); int n = strlen(name); printf("length of entered text: %i\n", n); int key = get_int("enter key: "); char newuppercase_array[n]; for (int i = 0; i < n; i++) { if (isupper(name[i])) { newuppercase_array[i] = ((((int newuppercase_array[i] - 65) + key)%26) + 65); printf("ciphertext %c",newuppercase_array[]); } else { newuppercase_array[i] = newuppercase_array[i]; } } }
[/dm_code_snippet]
In line 17:
newuppercase_array[i] = ((((int newuppercase_array[i] – 65) + key)%26) + 65);
There is a parenthesis error (https://www.canva.com/design/DAE2bi-j2DQ/MztV_c1ft77ouCAv6J_tjA/view?utm_content=DAE2bi-j2DQ&utm_campaign=designshare&utm_medium=link&utm_source=sharebutton). Help appreciated.
Reply
If you want to cast a value as ‘int’ the format is: (int) value
The way you wrote it it looks like you are attempting to declare a new variable 🙂
Query
Revised:
newuppercase_array[i] = (((((int) newuppercase_array[i] – 65) + key)%26) + 65);
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” 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> #include<ctype.h> int main(void) { string name = get_string("Enter: "); printf("Entered text by user: %s\n", name); int n = strlen(name); printf("length of entered text: %i\n", n); int key = get_int("enter key: "); char newuppercase_array[n]; for (int i = 0; i < n; i++) { if (isupper(name[i])) { newuppercase_array[i] = (((((int) newuppercase_array[i] - 65) + key)%26) + 65); printf("ciphertext %i",newuppercase_array[i]); } else { newuppercase_array[i] = newuppercase_array[i]; } } }
[/dm_code_snippet]
Not getting the correct ciphertext.
Reply
`newuppercase_array[i] = (((((int) newuppercase_array[i] – 65) + key)%26) + 65);
-
Converting to int here does nothing
-
You are ciphering the value of
newuppercase_array
(which isn’t initialized yet) rather than the character inname
.
Query
For the second point raised by you, this is one more area where I am struggling.
Will it be:
newuppercase_array[i] = (((((int) name[i] – 65) + key)%26) + 65);
Before that, do I still need to create a new array as it appears name[i] array yet not in existence.
Just checked putting
newuppercase_array[i] = (((((int) name[i] – 65) + key)%26) + 65);
and it is giving correct output for uppercase.
However, not getting any ouput other than cipher text of uppercase characters.
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
for (int i = 0; i < n; i++) { if (isupper(name[i])) { newuppercase_array[i] = ((((name[i] - 65) + key)%26) + 65); printf("ciphertext %c",newuppercase_array[i]); } else { newuppercase_array[i] = name[i]; } }
[/dm_code_snippet]
printf(“%c”,newuppercase_array[i]);
The above giving output the way I desire.
[learn_press_profile]
-
-
AuthorPosts
- You must be logged in to reply to this topic.