Reputation: 1
I'm very new to programming (started properly some 4 weeks back now), and i'm currently going through the CS50 course online. I seem to have run into a bit of a hurdle on the week 2 problem set - 'Caesar' where my if-statement isn't executing at all.
Right now, the programme should start when a number is typed into the command line i.e './caesar 10' (which will eventually by my 'key' but haven't got around to implement this yet). It should request the plaintext to be encrypted and, at the moment, just +1 to each character in plaintext.
The programme just doesn't execute the if-statement even when all boolean expressions appear to be true. I've ran it through the debug50 feature in the cs50 IDE and everything seems to look correct, i.e, the value of argc is 2, k will have a value of '10' when I input '10' at the command line.
I've been trying to figure this out now for two days - but to no avail! Any advice would be really appreciated. I've tried my best to explain the problem as best I can, but this is all very new to me, like I said earlier.
Thanks in advance for any/all help! :)
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
int k = atoi(argv[1]);
if (argc == 2 && isdigit(k))
{
string s = get_string("Plaintext: ");
for(int i = 0, n = strlen(s); i < n; i++)
{
printf("Cipertext: %c", s[i] + 1);
if (s[i] == ' ')
{
return 0;
}
}
}
else
{
printf("Usage: ./caesar key\n");
}
}
Upvotes: 0
Views: 69
Reputation: 2884
isdigit()
function checks if char
variable represents digit, i.e. if its value is between 48 and 57 (assuming ASCII coding). Your k
has value 10
- what is not digit (let's leave aside downgrading from int
to char
).
Upvotes: 2