for example there are some functions about strings.for eample "sortstring()", How can I use command argument line with this function?
Example: in dos command prompt you write
s.exe byname
then this program sorts strings by name. How can I do this?
Printable View
for example there are some functions about strings.for eample "sortstring()", How can I use command argument line with this function?
Example: in dos command prompt you write
s.exe byname
then this program sorts strings by name. How can I do this?
Hi,
Unless one knows what kind of contents you are storing and what kind of sorts you want to perform, answering your question ain't gonna be easy. Still hope the below mentioned code helps you to start off...
Code:#include <iostream>
#include <cstring>
int main(int argc, char *argv[])
{
if(!(strcmp(argv[1],"byname") )
{
// you would be sorting by name
}
else
{
// sort it otherwise
}
return 0;
}
>>if(!(strcmp(argv[1],"byname") )
Dont forget to check that argc > 1, else the prog will crash and burn ;)
than a lot but what is strcmp and why do we use "!"
and also I am talking about functions "sortbyname()" is a function. How can I use this function with command argument line.
1. strcmp() is a function that compares two strings
2. ! is used to mean not equal to 0 because this function returns 0 when the two strings aren't equal.
3. As explained in the code:
the argv[1] is the command line argument so if you were to say (using your first example) "s.exe byname" the program would execute the first if:Code:#include <iostream>
#include <cstring>
int main(int argc, char *argv[])
{
if(!(strcmp(argv[1],"byname") )
{
sortbyname()// you would be sorting by name
}
else
{
sortbysex()// sort it otherwise
}
return 0;
}
and would sort it by name. Undersatnd now?Code:if(!(strcmp(argv[1],"byname") )
{
sortbyname()// you would be sorting by name
}
-Devouring One-