0

In the code below I check whether or not the string is or isn't a digit. To do that I iterate through every single value entered in the argv[1]. Unfortunately when the entered key is larger than one digit my for loop executes multiple times not just to check the value of each character, but also the request for the plainText string.

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int ASCII[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};

string cipher(string word, int p);

int main(int argc, string argv[])

{
    // Ensure only two values are entered in command-line
    if( argc == 2)
    {
        //Cycle through each individual value in the second value of command-line
        for(int i = 0, n = strlen(argv[1]); i < n; i++)
        {
            //Ensure second value is an integer
            if(isdigit(argv[1][i]) == 0)
            
            printf("It's not a number");
          
            else
            {
                string plainText = get_string("Plaintext: ");
            
                string cipherText = cipher(plainText, (atoi(argv[1])));
                printf("%s\n", cipherText);
            }
            
            
        }
        
    }
    else
    printf("Enter key");
}

How can I make the forloop execute to check for digits and only after completing it's full loop then execute the if statement.

0

I solved my own problem by using a boolean function that returns true or false depending on whether or not the value of argv[1] is numeric by using isdigit.

You must log in to answer this question.

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