How to make input sentence into one string? or other method?

Hey Guys! I am writing a program on C++ with linux that will require the user to use the program by typing:
./contactadd 12 June 2010 Jessica Daniels Description
It is a contact saver, I think I have the birthday (eg. 12 June 2010) saved and sorted, however I want the name and description to be converted to one string or something like that because I don't know how many words will make up the description.

This is what I have so far:

int main(int argc, char* argv[])
{
saveContact(string(argv[1]) +" "+ string(argv[2]) +" "+ string(argv[3]) +": "+ argv[4]);
}

as you can see the user can only input one word for the description and name.
How could I make it so the user can input as much words for the description as she/he wants?

Thanks!
Last edited on
argc is the size of the argv array, so you can get the date and such, then loop through the remaining arguments, appending them to a string.
Just implementing what Zhuge said:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main (int argc, char** argv)
{
    std::string line = "";

    for (int i = 1; i < argc; i++)
    {
        line += argv [i];
    }

    // Just for testing
    std::cout << "Output: " << line << "\n";

    return 0;
}
You could require the user to quote the various fields

./contactadd "12 June 2010" "Jessica Daniels" "Description of some or other very young person"

or

./contactadd "21 June 1982" "William Arthur Philip Louis Windsor" "Second in the line of succession to the throne"

this will arrive as arg[1] - arg[3]

Or you could require some sort of delimiter between the fields.

./contactadd 12 June 2010 + Jessica Daniels + Description of some or other very young person + extra info (only first 2 pluses are special!)


(this choice of delimiter might cause problems for B.S.

./contactadd 30 December 1950 + Bjarne C++ Stroustrup  + C++ is his middle name!

)

You might also consider implementing a "beginner" mode, where you start the app with no command line params and it prompts for each field and then confirms before committing the data.
Last edited on
Topic archived. No new replies allowed.