Thread: CS50 Caesar - Checking a string for digits

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    2

    CS50 Caesar - Checking a string for digits

    I'm currently going nuts with a problem called Caesar. Essentially, you need to build a program that encrypts text that the user enters (so, for example - if you entered the number 1 and the plaintext 'Hi' the code that would come back would read 'Ij' as the program would have encrypted the plaintext message by moving the numbers forward by 1).


    I have been trying to write the program step by step - it's really easy to find 'complete' versions of the code online but I want to work out how to do each stage. The walkthrough says that you should try and build the program in the following stages:



    1. Write pseudocode
    2. Count command-line arguments
    3. Access the key
    4. Validate the key
    5. Peek under the hood (which is when you insert the 'formula' to encode the text)



    I'm stuck on section 4 - which is when you have to check that the value that the user has entered (in my example above the value was 1) and check it is a digit. As things stand, my program is simply checking whether I have one 'argument' but is not checking whether that argument is a digit, so - if the user typed:


    ./caesar 20 (This should work and does, it has one argument, and both '2' and '0' are digits)

    My program would respond with:

    Success
    20

    If the user typed:
    ./ caesar 20x (This should fail but doesn't. It has only one argument but one character in the argument 'x' is not a digit)
    My program would respond with:

    Success
    20x

    The issue seems to be that my program should be looking at each separate character that the user types in their argument. To do this I think I need to run a loop that iterates through each character in the string (which I think I have done). However, something clearly isn't right and I'm struggling! Any help or advice very much appreciated.



    Code:
    #include <cs50.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <math.h>
    
    
    int main(int argc, string argv[])
    {
        if (argc != 2)
        {
            printf ("Usage: ./caesar key\n");
        }
    
    
        else if (argc == 2)
        {
            int Key = atoi(argv[1]);
            bool isKeyValid = true;
            int len = strlen(argv [1]);
    
    
            for (int i = 0; i < len; i++)
            {
                if (isdigit(argv[1][i]) == false)
                {
                    isKeyValid = false;
                    printf ("Usage: ./caesar key\n");
                    i = len;
                }
                if (isKeyValid == true)
                {
                    printf ("Success\n");
                    printf ("%s\n", argv[1]);
                    i = len;
                }
            }
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    The problem is perhaps that atoi doesn't care about trailing garbage on the string.

    Use strtol if you want to see how much of the string you actually used.
    strtol - C++ Reference

    > if (isdigit(argv[1][i]) == false)
    The 'is...' functions don't return true or false.
    Well, you can compare with false, just don't try and compare them with true.

    > if (isKeyValid == true)
    Put this outside your loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    2
    I think that your last point - pulling the if (isKeyValid == true) section out of the loop has fixed the problem! Thank you!

    Any idea why this worked???

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    > Any idea why this worked???
    Of course - that's why I suggested you look at it.

    Now look at it again, and see if you can analyse why it is so.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Harvard CS50 Online Help -- Mario Pyramid
    By mcavanaugh8 in forum C Programming
    Replies: 14
    Last Post: 02-01-2016, 02:40 PM
  2. Code for CS50 functions?
    By VEN0M in forum C Programming
    Replies: 19
    Last Post: 07-25-2012, 11:53 AM
  3. How to occurrence digits in string function.
    By so6ick in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2010, 11:21 PM
  4. Plz help me to include cs50.h and cs50.c into Code::Block
    By Huncowboy in forum C Programming
    Replies: 4
    Last Post: 03-09-2010, 10:05 AM
  5. Replies: 9
    Last Post: 06-25-2009, 04:25 AM

Tags for this Thread