Command line tests. How to satisfy tests in one line.

Jan 30, 2017 at 8:15pm
Hi, I'm a noob here plz be gentle. My code is supposed to print out all of the command line arguments passed to it. Plus, each argument should be separated from the others with a comma and space. So i just begin to code.

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
int i;
for (i=1; i < argc; ++i)

{
cout << argv[i] << "," << " ";
}
}
So i ran it;


RESULT: no-arg-case [1/1]

one-arg-case:
testing,
ERROR: expected testing

RESULT: one-arg-case [0/1]

five-arg:
./runcase: line 39: five-argis: command not found

RESULT: five-arg [1/1]

five-arg-with-comma:

check, if,, this, thing, works,

ERROR: expected check, if, this, thing, works

RESULT: five-arg-with-comma [0/1]

comma=at-end:
a, little, tricky,,

ERROR: expected a, little, tricky,
RESULT: comma-at-end [0/1]

multiple-comma:

help, me, rhonda,, help, help,me, rhonda,, yeah,

ERROR: expected help, me, rhonda, help, help,me, rhonda, yeah

RESULT: multiple-comma [0/1]

quoted-string:

this is true, this is fact,

ERROR: expected this is true, this is fact

RESULT: quoted-string [ 0 / 1 ]


so now I think i need multiple outputs so satisfy each test but thats wrong. there can only be one output for the command line arguments.

I also did
nt i;
for (i=1; i < argc; ++i)

{
cout << argv[i] << "," << " ";
cout << '\b';
}
}

{ cout << argv[1]<< ","<< " " << argv[2]<< " "<< argv[3]<< ",";cout << " "<< argv[4]<< ","<< " "<< argv[5]; }}

This satisfied five-arg-with-comma.

What im confused about is how to satisfy each test with one output line?. Because if a change one argv[] the next test gets changed.
Jan 31, 2017 at 1:16am
closed account (3vX4LyTq)
Hi turtleman1,

In the future, format your code as so:

 
this is c++

by using {code} and {/code} using normal brackets. It makes it a lot easier to understand.

Jared

P.S. Hope this helps
Last edited on Jan 31, 2017 at 1:19am
Jan 31, 2017 at 1:51am
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main( int argc, char* argv[] )
{
    // print the first n-1 command line args with a comma and a space after each arg
    for( int i = 1 ; i < (argc-1) ; ++i ) std::cout << argv[i] << ", " ;

    // print the last arg
    std::cout << argv[argc-1] << '\n' ;
}

http://coliru.stacked-crooked.com/a/ff351341751fdcee
Jan 31, 2017 at 4:27pm
Thank you for the responce. I ran the code above and it satisfied all the test except 2. I will keep working and try to satisfy the rest.
Jan 31, 2017 at 4:33pm
That was careless on my part.
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main( int argc, char* argv[] )
{
    // print the first n-1 command line args with a comma and a space after each arg
    for( int i = 1 ; i < (argc-1) ; ++i ) std::cout << argv[i] << ", " ;

    // print the last arg  
    if( argc > 1 ) std::cout << argv[argc-1] << '\n' ;
}
Jan 31, 2017 at 6:27pm
how did you pass all the command line arguments into the program?
Feb 1, 2017 at 1:29am
Command line arguments are strings in the command line, typically separated by white space.

For example, if the name of the program to be run is ./my_program, with the command line
./my_program one two, buckle my shoe.

argc == 6
argv[0] == "./my_program"
argv[1] == "one"
argv[2] == "two,"
argv[3] == "buckle"
argv[4] == "my"
argv[5] == "shoe."

http://coliru.stacked-crooked.com/a/a2316fb27c7da2b0

The specifics of how the characters on the command line are parsed is implementation defined.
Microsoft C++: https://msdn.microsoft.com/en-us/library/17w5ykft.aspx
Posix: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_01
Feb 1, 2017 at 2:43am
Thank you
Feb 6, 2017 at 7:49pm
If a command line argument ends in a comma, then another comma should NOT be added

how can I add this part to the previous code ?

Thanks
Feb 7, 2017 at 2:19am
As JLBorges mentions above:
Command line arguments are strings in the command line ...

So use http://www.cplusplus.com/reference/string/string/back/ to check last character
Topic archived. No new replies allowed.