2 Replies - 5189 Views - Last Post: 01 April 2014 - 11:27 PM

#1 DogFreeZone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-September 13

OverLoad Main to execute program

Posted 20 September 2013 - 01:25 PM

I have the following code:

#include <iostream>
#include <fstream>

char** read(const char* fileName, int& count);

class KeyedWord
{
public:
	KeyedWord(char* word); // TO BE IMPLEMENTED
	char* getWord(); // TO BE IMPLEMENTED
	char* getKey();  // TO BE IMPLEMENTED
	~KeyedWord();
private:
	char * _word;
	char* _key;
};


KeyedWord::KeyedWord(char* word)
{
	
}

void main(int argumentCount, char** arguments)
{
	if (argumentCount <= 1) 
	{
		std::cout << "No file name given as argument" << std::endl;
	}
	else 
	{
}



I am confused ,because in order for the program to excute properly it needs 2 arguments passed to it ( argumentCount and arguments). How do I pass arguments to main? I would suspect it somehow involves the useage of #define but I have not gotten any further.

This post has been edited by modi123_1: 20 September 2013 - 01:36 PM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: OverLoad Main to execute program

#2 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: OverLoad Main to execute program

Posted 20 September 2013 - 04:52 PM

It is the text after exe.You can set up parameters in most ide to be used or you can configure the exe itself at os level or you can type into command prompt.

mv f1.text f2.txt


mv is program and text files are parameters to it.

This post has been edited by MentalFloss: 20 September 2013 - 04:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 jeaye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-April 14

Re: OverLoad Main to execute program

Posted 01 April 2014 - 11:27 PM

View PostDogFreeZone, on 20 September 2013 - 12:25 PM, said:

#include <iostream>
#include <fstream>

char** read(const char* fileName, int& count);

void main(int argumentCount, char** arguments)
{
	if (argumentCount <= 1) 
	{
		std::cout << "No file name given as argument" << std::endl;
	}
	else 
	{ }



1. main does not return void. The two valid forms of main in standard C++ are:
int main(); // disregards runtime arguments
int main(int argc, char **argv); // provides access to runtime arguments


2. Passing arguments to main happens when you invoke the executable. Assuming you're in a terminal emulator (like cmd.exe, xterm, or iTerm) and your program is called myprog, you pass arguments like so:
myprog.exe arg1 arg2 arg3
or
./myprog arg1 arg2 arg3


3. It's important to note that argc should always be at least 1. This is because the operating system fills in the first (zeroth) argument for you with the name of the program being invoked. Example:
#include <iostream>

void process(int const argc, char ** const argv) // do some work on these
{}

int main(int argc, char **argv)
{
	std::cout << "program name: " << argv[0] << std::endl; // myprog.exe or ./myprog
	if(argc > 1) // actual arguments were passed
	{ process(argc, argv); }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1