Making a command line argument function

deleted
Last edited on
when calling a function, as you are doing in the main function, you do not include the type of the parameters. It should just be cout << foo(a, b);

And you also need to declare a and b in the main function.
Last edited on
1. You need to pass thru argc, and argv to foo().
2. You shouldn't write into argc or argv, they really should be declared as const, but they historically aren't.
3. If you want to return a string, use the standard string.
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
#include <iostream>
#include <string>

std::string foo(int argc, char* argv[])
{
    std::string temp;

    for (int i = argc - 1; i > 0; i--)
    {
        for (int a = 0; argv[i][a]; ++a)
        {
            if (argv[i][a] < 'a' || argv[i][a] > 'z')
            {
                temp += ' ';
                temp += argv[i][a];
            }
        }
    }

    return temp;
}

int main(int argc, char* argv[])
{
    std::cout << foo(argc, argv) << std::endl;
}
Last edited on
Topic archived. No new replies allowed.