how to utilize "(int argc, const char * argv[]) " in a program
-
I found the following program that count the number of words within a phrase but I don't understand the initial code defined on the int main : " int argc,const char * argv[] just want learn. thanks -------------------------------------- #include using namespace std; int main(int argc, const char * argv[]) { // ask the user to enter a string cout << "Enter a string: "; // save the string entered by the user string input; getline(cin, input); int numberOfWords = 0; // loop through the input and count all the spaces int inputLen = (int)input.length(); for (int i=0; i<inputLen; i++) { char current = input.at(i); if (isspace(current)) { numberOfWords++; } } // in case there were words, and not empty string, remember that there's always // one space less than the actual number of words if (numberOfWords > 0) { numberOfWords++; } // print the number of words cout << "\nNumber of words = " << numberOfWords << endl;; return 0; }
-
I found the following program that count the number of words within a phrase but I don't understand the initial code defined on the int main : " int argc,const char * argv[] just want learn. thanks -------------------------------------- #include using namespace std; int main(int argc, const char * argv[]) { // ask the user to enter a string cout << "Enter a string: "; // save the string entered by the user string input; getline(cin, input); int numberOfWords = 0; // loop through the input and count all the spaces int inputLen = (int)input.length(); for (int i=0; i<inputLen; i++) { char current = input.at(i); if (isspace(current)) { numberOfWords++; } } // in case there were words, and not empty string, remember that there's always // one space less than the actual number of words if (numberOfWords > 0) { numberOfWords++; } // print the number of words cout << "\nNumber of words = " << numberOfWords << endl;; return 0; }
argc = arg count *argv[] = an array of argc number of strings. Consider the following program:
#include <stdio.h>
#include <stdlib.h>int main(int argc, char *argv[])
{
int i;
for(i=0; i<argc; i++)
printf("Argument index %d: %s\n", i, argv[i]);return 0;
}
When I call the program from the command line like so:
argcdemo arg1 arg2 -someFlag -somethingElse
I get the following output:
Argument index 0: argcdemo
Argument index 1: arg1
Argument index 2: arg2
Argument index 3: -someFlag
Argument index 4: -somethingElseSo as you can see, the first one takes the name of the program as invoked, the others get a string from the command line, where each string is separated by a space. Or more simply, the argc will always be at least 1,since you have to specify the name of the program to run. Any subsequent arguments will increment argc and have their values added to the argv array. :)
"When I was 5 years old, my mother always told me that happiness was the key to life. When I went to school, they asked me what I wanted to be when I grew up. I wrote down 'happy'. They told me I didn't understand the assignment, and I told them they didn't understand life." - John Lennon
-
I found the following program that count the number of words within a phrase but I don't understand the initial code defined on the int main : " int argc,const char * argv[] just want learn. thanks -------------------------------------- #include using namespace std; int main(int argc, const char * argv[]) { // ask the user to enter a string cout << "Enter a string: "; // save the string entered by the user string input; getline(cin, input); int numberOfWords = 0; // loop through the input and count all the spaces int inputLen = (int)input.length(); for (int i=0; i<inputLen; i++) { char current = input.at(i); if (isspace(current)) { numberOfWords++; } } // in case there were words, and not empty string, remember that there's always // one space less than the actual number of words if (numberOfWords > 0) { numberOfWords++; } // print the number of words cout << "\nNumber of words = " << numberOfWords << endl;; return 0; }
Google is your friend. See for instance: "argc and argv"[^].
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite