Home > Back-end >  Command line arguments
Command line arguments

Time:10-21

Generate a program like my exe, execute my. Exe 12 zhangsan enter
Output line 12 string, each row are strings zhang SAN,
What is the code, how to input my DOS interface. Exe 12 zhangsan come out the result

CodePudding user response:

The following is a reference program
 
#include
#include
using namespace std;
//the string is converted into digital
Int stringToInt (char * s) {
Int len=strlen (s);
Int ans=0;
for(int i=0; i If (s [I] <[I] '0' | | s & gt; '9') break;
Ans *=10;
Ans +=(s [I] - '0');
}
Return ans.
}
//arg c for the parameters of the receiving number + 1, argv parameters for the received array
Int main (int arg c, char * argv [])
{
int num=0;
//parameter number is 2, no error
If (arg c! {
=3)Cout<" Call the parameter error "& lt; return 0;
}
//get cycles
Num=stringToInt (argv [1]);
//output
for(int i=0; iCout}
return 0;
}

CodePudding user response:

#include
#include

Int main (int arg c, char * * argv) {
//arg c is the number of parameters
//argv [0] is my exe file itself. The exe
//argv [1] is the first parameter 12
//argv [2] is zhang SAN
Int row;
Sscanf (argv [1], "% d", & amp; The row);
For (int I=0; iPrintf (" % s \ n ", argv [2]).
return 0;
}
  • Related