0

Here is what I am attempting, and my errors. I am gathering that I am not grasping the "array of strings nature of argv[], and since I am following along with the material from class, I am pretty nervous about delving into using *char (I don't remember pointers being introduced). I am also unsure about when to involve the second brackets in argv[][i].

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

bool is_only_digits(string argv[]);

int main(int argc, string argv[])
{
    // Reject wrong argument count or not digits
    if(argc != 2 || is_only_digits(argv[1]) == false)
    {
        printf("Usage: ./caesar key\n");
    }
    
    if(argc == 2 && is_only_digits(argv[1]) == true)
    {
        printf("Success!\n");
        printf("%d\n", atoi(argv[1]));
    }    
        
    return 0;

   //Get plaintext

   //Encipher 

   //Print cipher text
}
 
 
bool is_only_digits(string argv[1])
{
    int length = strlen(argv[1]);
            
    for (int i = 0; i < length; i++)
    {
        if (isdigit(argv[1]))
        return true;
    }
            
    return false;
}
caesar2.c:12:36: error: incompatible pointer types passing 'string' (aka 'char *') to parameter of type 'string *' (aka 'char **'); take the address with & [-Werror,-Wincompatible-pointer-types]
    if(argc != 2 || is_only_digits(argv[1]) == false)
                                   ^~~~~~~
                                   &
caesar2.c:7:28: note: passing argument to parameter 'argv' here
bool is_only_digits(string argv[]);
                           ^
caesar2.c:17:36: error: incompatible pointer types passing 'string' (aka 'char *') to parameter of type 'string *' (aka 'char **'); take the address with & [-Werror,-Wincompatible-pointer-types]
    if(argc == 2 && is_only_digits(argv[1]) == true)
                                   ^~~~~~~
                                   &
caesar2.c:7:28: note: passing argument to parameter 'argv' here
bool is_only_digits(string argv[]);
                           ^
2 errors generated.

Thank you in advance!

1

Its a simple and common issue with new programmers. ;-)

Yes, argv[] is an array of strings. So, argv[1] is a string. Now, say that argv[1] contains the string "cat". Then argv[1][0] contains the char 'c', argv[1][1] contains 'a', argv[1][2] contains 't' and argv[1][3] contains the end of string marker, '\0'. That last one is very important, remember that strings always end with the EOS marker.

Just FYI, The isdigit() function and all of it's issomething cousin functions take a single char as input. If you ever try to feed it a string, it fails at run time.

However, your problem is related to your is_only_digits() function. In short, the use of the reserved keyword var name of "argv[]" in the function prototype and in the function is a problem. The function should use a non-reserved name for the string that will be input into the function. (You can pass argv[1] as a parameter and it'll work fine, but using it in the prototype is the issue.) It's always a bad idea to use any reserved words or names in function prototypes or for other similar purposes.

For example, you could alter the function to look like this:

bool is_only_digits(string mystring)
{
    int length = strlen(mystring);
            
    for (int i = 0; i < length; i++)
    {
        if (isdigit(mystring[i]))  //note the selection of a single char from the string
        return true;    
    }            
    return false;
}

Be sure to edit the function signature line at the top of the program to match.

However, although this will solve your current issues, there are other problems with this function. You deserve a chance to figure it out. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Super helpful, thank you! I'll watch for that next issue now, too. 🙂 – Angela Stoddard Dec 15 '20 at 13:56

You must log in to answer this question.

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