C++/c++
Expert: vijayan - 12/3/2009
QuestionQUESTION: int main(int argc, char *argv[])
{
for(i=0;i<argc;i++)
{cout<<"\tArgument #"<<i<<": "<<argv[i]<<endl;}
}
the user passes some arguments to main.....
now i have to check these argument types....
eg if a user enters integers or characters????
how can i check?? using atoi can i check???
and also how can i check if the user entred this 72hho98????
or if the user did not enter anything??
can u please answer me today????
i have to submit a project tmrw....
thnx in advance....
ANSWER: for(i=0;i<argc;i++)
{cout<<"\tArgument #"<<i<<": "<<argv[i]<<endl;}
The first argument argv[0] (which is always present) is a string containing the program's name or a null string if that is not available. Remaining elements of argv represent the arguments supplied to the program.
> now i have to check these argument types....
> eg if a user enters integers or characters????
The argument type is a c style string - a null terminated array of characters. You can check if every char in the string is a literal for a digit (as in "45678") - use the library function isdigit for this.
http://www.cppreference.com/wiki/c/string/isdigit
> how can i check if the user entred this 72hho98?
The simplest way is to convert the c style string in argv[i] into a C++ string and then work with the C++ string.
http://www.cppreference.com/wiki/string/start
for example:
std::string arg = argv[i] ;
if( arg == "72hho98" ) std::cout << "user entered 72hho98\n" ;
To also convert a string with a valid literal representation of an integer it to the int, use a stringstream.
for example:
std::istringstream stm(arg) ;
int number ;
if( ( stm >> number ) && stm.eof() ) std::cout << "this arg is a number " << number << '\n' ;
else std::cout << "this arg is not a number\n" ;
> if the user did not enter anything
Then argc will be equal to one.
---------- FOLLOW-UP ----------
QUESTION: thnx alot......
by 72hh098 i meant the user is entering both char and digits......
and can u tell me how to declare a new in a 2D array of a size entered by user but the columns are only 6 i mean : array[n][6].......where n is taken from the user.....
plus tell me also how can i make this above array to b a constant char???? so i can use strcmp and atoi etc on it????
ANSWER: > can u tell me how to declare a new in a 2D array of a size entered by user but the columns are only 6
> i mean : array[n][6].......where n is taken from the user.....
char (*array)[6] = new char[n][6] ;
// ...
// use the array
// ...
delete[] array ;
> plus tell me also how can i make this above array to b a constant char????
> so i can use strcmp and atoi etc on it????
You do not need to make it an array of const char. There is an implicit conversion from char* to const char*
for example:
char* cstr = array[1] ;
strcmp( cstr, "abcd" ) ;
// or directly,
strcmp( array[1], "abcd" ) ;
isupper( array[1][2] ) ;
// etc
---------- FOLLOW-UP ----------
QUESTION: sorry for disturbing u again but u didnt tell me that how will i get to know that user has entered both characters and digits in a string.....
wen i use atoi and compare it only returns integers before a character and ignores alphabets...
eg 9is9sj
it will only give 9......
AnswerI've already answered this in your first question; reproduced here for convenience:
To also convert a string with a valid literal representation of an integer it to the int, use a stringstream.
for example:
std::istringstream stm(arg) ;
int number ;
if( ( stm >> number ) && stm.eof() ) std::cout << "this arg is a number " << number << '\n' ;
else std::cout << "this arg is not a number\n" ;