+ 1

reprompt user is char and int is combined

Hey guys, My code works fine, the only thing is, it allows you to input a letter with the "key". For an example, when I go to run it, I have to write: ./caesar , then the key: ./caesar 1 or ./caesar 2, any number, it doesn't matter. The only issue is it will not repromt you if you put a letter after ./caesar. example: ./caesar 2x. If anyone has any idea how to fix this, id really appreciate it! THANK YOU #include <cs50.h> #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(int argc, string argv[]) { if (argc != 2) { printf("Usage: ./caesar key\n"); return 1; } int k = atoi(argv[1]); do { string p = get_string("plaintext: "); printf("ciphertext: "); for (int i = 0, n = strlen(p); i < n; i++) { if (islower(p[i])) { printf("%c", (((p[i] + k) - 97) % 26) + 97); } else if (isupper(p[i])) { printf("%c", (((p[i] + k) - 65) % 26) + 65); } else { printf("%c", p[i]); } } printf("\n"); return 1; } while (argc == 2); }

3rd Sep 2019, 8:54 PM
David
David - avatar
3 ответов
+ 2
Iterate through argv[1] before you call atoi and test if every character is a number. ( or whatever other valid character ) Display error and return if it contains any invalid character.
3rd Sep 2019, 9:32 PM
Dennis
Dennis - avatar
0
how are you using string in C like in C++???
4th Sep 2019, 12:24 AM
✳AsterisK✳
✳AsterisK✳ - avatar
0
✳AsterisK✳ im just pulling it from the cs50 library.
4th Sep 2019, 2:40 AM
David
David - avatar