Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to utilize "(int argc, const char * argv[]) " in a program

how to utilize "(int argc, const char * argv[]) " in a program

Scheduled Pinned Locked Moved C / C++ / MFC
csstutorial
3 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stefan61
    wrote on last edited by
    #1

    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; }

    enhzflepE CPalliniC 2 Replies Last reply
    0
    • S Stefan61

      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; }

      enhzflepE Offline
      enhzflepE Offline
      enhzflep
      wrote on last edited by
      #2

      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: -somethingElse

      So 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

      1 Reply Last reply
      0
      • S Stefan61

        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; }

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        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

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups