0

I would appreciate any help that could be given with my code below (+ error codes below that):

My code will not compile and the primary error apparently is an "undeclared identifier" refering to argv. however i believe i have identified the string array in the initial line (int main argc string argv[MaxprobaleInput])

Any other flaws in my code that anyone can see would also be greatly appreciated.

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

//DECLARE VARIABLES TO BE USED IN CODE GLOBALLY

int MaxProbableInput = 12;
int MinimumProgInput = 2;
int IterateUserInput = 0;
int UserInputCharCount = strlen(argv);

int main(int argc, string argv [MaxProbableInput])
{
    //MAKE SURE THE USER INPUTS A NAME AT COMMAND LINE

    if (argc <= MinimumProgInput)

    {
        printf("The proramme requires your full name to execute. e.g: ./initials Chengetai Chiwanga\n");

    return 1;
}
    //PRINT THE FIRST LETTER OF THE USER INPUT IN CAPS

    if  (argv[0] != ' ' && argv[0] != NULL)

    {

        printf("%c,toupper argv [0][1]);

    }

    //PRINT CONCURRENT INITIALS OF THE USERS INPUT IN CAPS

        for (IterateUserInput; IterateUserInput < UserInputCharCount; IterateUserInput++)

        if (UserInputCharCount == ' ' && UserInputCharCount [IterateUserInput + 1] != NULL)
        {

        printf("%c", topupper argv[IterateUserInput + 1] );

        return 0;

        }

    }

}

Error codes:

clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow    initials.c  -lcrypt -lcs50 -lm -o initials

initials.c:11:33: error: use of undeclared identifier 'argv'
int UserInputCharCount = strlen(argv);
                            ^
initials.c:26:22: error: comparison between pointer and integer ('string' 
(aka 'char *') and 'int') [-Werror]
    if  (argv[0] != ' ' && argv[0] != NULL)
         ~~~~~~~ ^  ~~~
initials.c:30:33: error: expected ')'
        printf("%c",toupper argv [0][1]);
                            ^
initials.c:30:19: note: to match this '('
        printf("%c",toupper argv [0][1]);
              ^
initials.c:38:69: error: subscripted value is not an array, pointer, or vector
            if (UserInputCharCount == ' ' && UserInputCharCount [IterateUserInput + 1] != NULL)
                                             ~~~~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~
initials.c:41:38: error: expected ')'
            printf("%c", toupper argv[IterateUserInput + 1] );
                                 ^
initials.c:41:23: note: to match this '('
            printf("%c", toupper argv[IterateUserInput + 1] );
                  ^
initials.c:36:22: error: expression result unused [-Werror,-Wunused-value]
            for (IterateUserInput; IterateUserInput < UserInputCharCount; IterateUserInput++)
                 ^~~~~~~~~~~~~~~~
6 errors generated.
make: *** [initials] Error 1

Thanks in advance.

PotNoodle

1 Answer 1

0

First, argv is a parameter of main, so you cannot access it in a global context. I don't see the point in those global variables either. Better have as few global variables as possible.

Second, function signature of main should be int main(int argc, char *argv[]) (string is an alias for char*, so string argv[] is the same as char*argv[] or even char**argv). No array length.

Third, as argv is a list of strings, with the number of strings stored in argc. First element (at index 0) is usually the programme name, the others are the arguments passed on command line.

5
  • No need for those global variables. Also, just saw this is initials, this one doesn't use argc and argv at all. The name should be taken from stdin, for example using get_string() function.
    – Blauelf
    Commented Nov 20, 2017 at 9:56
  • Ok cool Blauelf. So what your suggesting is initialise my code with * int main (void)* and then receive a user array input via get string and a “do while” loop in order to access the variable globally. But would I still be able keep count of the strings within the user input array of I declare it within a do while loop?
    – Pot Noodle
    Commented Nov 20, 2017 at 10:12
  • I don't get the do while part, and I don't get the "globally". You read one string from stdin and print one line of initials to stdout. That's all main does. Involves a loop over that single input string, which definitely shouldn't be a do..while, but either for or maybe regular while.
    – Blauelf
    Commented Nov 20, 2017 at 10:19
  • initials, both in less and more version, is much simpler than caesar or vigenere. Maybe you should read the problem specs again, and not think that complicated.
    – Blauelf
    Commented Nov 20, 2017 at 10:24
  • Ok I’m probably overthinking the problem. Thanks for your help.
    – Pot Noodle
    Commented Nov 20, 2017 at 18:30

You must log in to answer this question.

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