0

So I am still struggling to get my Caesar program to read argv1[1]. my program compiles and returns a value of "1" if the incorrect number of arguments are entered. The problem seems to be that it is not reading argv[1] at all. I feel like i have set up a function that should look at every index in argv[1] to determine if they are decimals or not. BUT, it appears that this is not happening.

The evidence is that I am not returning the "Usage: key" message when i enter to many arguments at the command line. Also, no matter what I enter whether it is "2" or "2blab" i still get prompted to enter "plaintext".

my code might look cluttered with comments but I tried to comment every area to show my thinking. Can someone help me out?

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdbool.h>

//function that takes in argv[1], reads it, and determines if it meets criteria
//criteria defined as only decimal digits between 0 and 9.  letters etc are not accepted.
int verifyinput(string b);

int main (int argc, string argv[])
{
    //if correct number of arguments are entered check argv[1] for compliance with entry critera.
    if (argc==2)
    {
        //sends argv[1] to verify its contents
        //returns an integer that is the size of the number of acceptable digits
        int verify = verifyinput(argv[1]);
        //if the number of acceptable digits is different then the length of argv[1]
        //the user is given the following error message and the program ends
        if (verify != strlen(argv[1]))
        {
        printf("Usage: ./caesar key \n");
        }
        //if the number of acceptable digits is the same as the length of the argv[1]
        //the user is prompted to enter text to encipher
        else if (verify == strlen(argv[1]))
        {
        string k = get_string("PlainText: ");
        }
    }
    //if user arguments are not entered correctly
    //error message 1 is displayed
    if (argc!=2)
    {
    printf("1 \n");
    }
}


int verifyinput(string b)
{
    int length = strlen(b);
    int check = 0;

        // loop that reads each digit of string b
        for (int i = 0; i<=length; i++)
        {
            //ascii code for zero through nine are 0 through 9.  any digit larger then ascii 9 is outside accepted range
            //check is added to in the event that b[i] is less then or equal to 9
            if (b[i]<=9)
            {
                check++;
            }
            //check is subtracted from if b[i] is greater then 9
            if (b[i]>9)
            {
                check--;
            }
        }
 //returns value to  int verify in main 
return check;
}

1 Answer 1

1

When compare a char with a int you have two options. For the int use the ascii value for the letter or number you are comparing to, or you can use the ' symbol ie.: '9'. Also when checking if a char is inside a range maybe you find useful something like: ( c >= 'a' && c <= 'z') only goes true if c is a lowercase letter.

2
  • Thank you so much. that helped for sure...also my lack of diligence with the ascii table revealed my ability to make silly mistakes. Another change I made was to change my int check in verify input to 1. I think this accounts for the '\0' null at the end of each element in an array. It is running like a fine tuned machine now. well, sort of anyways.
    – Rob Watt
    Commented Apr 11, 2020 at 19:15
  • Hi Rob, don't beat you up. We are all learning here. I find that debug50 is very useful when a variable is not what I was expecting. You only have to set some breakpoints in your code and you can check the value of your variables at every step of your program
    – Tritum
    Commented Apr 11, 2020 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .