-1

I want to use a given string (given as console app argument) within the main-method and don't know how to assign the console app argument to a string variable which is declared and used in the main method:

console:

~/substitution/ $  ./test FOEFJEOWJFWEFOJ (<- wanted argument)

code:

int main (int argc, char* argv[])
{
  string argumentString;

In other words: How do I create a string argumentString with the content of the main-method argument argv[]?

Until now the following is my closest approach.. but with that code I get an "expected expression" with a pointer to key = argv[]

    int main(int argc, char *argv[])
{

string alphabet = "abcdefghijklmnopqrstuvwxyz";
string plain;
string key;
string cipher;
int i;
int j;
int n;
int o;

    if (argc > 2)
{
    printf("Only 1 Argument allowed");

    return 1;
}

if (argc < 2)
{
    printf("./substitution KEY\n");

    return 1;
}

key = argv[];

if (strlen(key) == 26)
{
ALL
  • 9
  • 1
  • 6
  • Can you post your code and what you have done so far? – Ram May 02 '20 at 18:44
  • 2
    C don't have standard `string`. Are you using `cs50.h`? – MikeCAT May 02 '20 at 18:46
  • Are you doing the CS50 course? There is no `string` type in standard C, but that course uses it as an alias for `char *`. – Jonathan Leffler May 02 '20 at 18:46
  • 2
    You want to refer to `argv[1]`. Don't you? https://en.cppreference.com/w/c/language/main_function. – Simón May 02 '20 at 18:48
  • Wow.. the comments are coming faster than I can read them ^^. First... Thanks to all and yes, it is about the CS50.. (how embarassing.. should have the cs50.h in mind :-D) – ALL May 02 '20 at 18:53
  • @Simón: Thank you, I think you've led me to the right direction – ALL May 02 '20 at 18:54
  • @SuperStormer: Thanks a lot to you, too :-) – ALL May 02 '20 at 18:57
  • Please post a [mcve] so we can reproduce the problem and help you debug it. – user3629249 May 02 '20 at 19:46
  • regarding: `int main (int argc, char* argv[]) { string argumentString;` Then need to assure that the value in `argc` is (at least) 2, to show that the user actually entered a parameter (the desired string) and if the didn't enter a parameter, then output to `stderr` a USAGE message (I.E.if( argc <2 ) {fprintf( stderr, "USAGE: %s stringParm\n, argv[0] ); exit( EXIT_FAILURE ); }` Note: `exit()` and `EXIT_FAILURE` are exposed via `#include `. If the parameter is available: `subfunction( argv[1] );` Signature for subfunction: `void subfunction( char *userString );` – user3629249 May 02 '20 at 19:50

1 Answers1

0
int main(int argc, char *argv[])
{

string alphabet = "abcdefghijklmnopqrstuvwxyz";
string plain;
string key;
string cipher;
int i;
int j;
int n;
int o;

    if (argc > 2)
{
    printf("Only 1 Argument allowed");

    return 1;
}

if (argc < 2)
{
    printf("./substitution KEY\n");

    return 1;
}

key = argv[1];

if (strlen(key) == 26)
{

Sooo damned easy... Couldn't see the forest because of the trees (sry, don't know the proper phrase in English).

It was all correct so far. There was just the need of the "1" in the key-assignment line ("0" would be the programm's name as argument). facepalm

:-)

ALL
  • 9
  • 1
  • 6