Be a Supporter!

C++ Help :(

  • 496 Views
  • 12 Replies
New Topic Respond to this Topic
dat-boi-Monk
dat-boi-Monk
  • Member since: Jun. 19, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
C++ Help :( 2005-06-24 01:04:59 Reply

wow, C++ is hard :( but anyway

int main ( int argc, char *argv[] )

i just don't get that, i have read tuts on it and they don't explain it good enough, does it mean that main funciton accepts argc int, and a pointer to a string?? can someone pelase explain this to me explicitly

thanks in advance :)

LordAba
LordAba
  • Member since: Nov. 2, 2000
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++ Help :( 2005-06-24 01:33:45 Reply

At 6/24/05 01:04 AM, X-Zile wrote: int main ( int argc, char *argv[] )

i just don't get that, i have read tuts on it and they don't explain it good enough, does it mean that main funciton accepts argc int, and a pointer to a string?? can someone pelase explain this to me explicitly

It's for command line processing. argc is the number of arguements, the argv are the actual values of those arguements.


What may man within him hide, though angel on the outward side.

BBS Signature
dat-boi-Monk
dat-boi-Monk
  • Member since: Jun. 19, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to C++ Help :( 2005-06-24 01:54:18 Reply

i know i know (sigh)

im saying what do those arguments do??? how do they work, explain this please

TurnipClock
TurnipClock
  • Member since: Apr. 6, 2002
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to C++ Help :( 2005-06-24 02:51:13 Reply

say for instance you drag an image file over mspaint or photoshop, the program will open up with the image file loaded into it. theyare for doing things like that, the input from the image file is acceseed through "int argc, char *argv[]" and loaded up without you having to do it.

MainThink
MainThink
  • Member since: Apr. 5, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to C++ Help :( 2005-06-24 03:42:39 Reply

Imagine you write a program to sum 2 numbers. You can ask the user to input them using two cin >> calls, or you can also unleash the power of command line arguments (actually, it would be a good idea to to both).

Consider this source code:

#include <iostream.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
int number_1, number_2;

if (argc > 1)
{
number_1 = atoi(argv[1]);
number_2 = atoi(argv[2]);
}
else
{
cout << "\nPlease enter a number: ";
cin >> number_1;
cout << "Now another one please: ";
cin >> number_2;
}

cout << "\nThe sum is " << number_1 + number_2 << endl;

return 0;
}

You have to know that argv[0] is always the name of the program (for example, I compiled it as sum.exe and argv[0] = "sum"). That is why argc will be always at least = 1.
If you want to know wether the user has indeed specified arguments, you can check if argc > 1, like I do.

If there are arguments (assuming they are integers, otherwise you can add error checking) I use those as numbers.
If there are no arguments, I ask for them. After all that, I display the sum.

If you call "sum.exe" you'll be asked to input the numbers, if you call for example "sum.exe 5 5" you will only see "The sum is 10".

Is this a little bit clearer for you? If not, don't hesitate to ask!

MainThink.

dat-boi-Monk
dat-boi-Monk
  • Member since: Jun. 19, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to C++ Help :( 2005-06-24 12:01:17 Reply

o.....k..., but u can still run a program without those argc and stuff in parenthesis, why do u need them? and how can argc be an argument?

thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++ Help :( 2005-06-24 12:51:14 Reply

At 6/24/05 12:01 PM, X-Zile wrote: o.....k..., but u can still run a program without those argc and stuff in parenthesis, why do u need them? and how can argc be an argument?

You don't get it. Argc isn't an arguement, it's the number of arguements entered.

Okay, let me exaplain it to you. You know what a CLI is right? No? Too bad.

If you have

main()

This means, if the program is given command line arguements, they cannot be accessed because there isn't a variable to store them in.

If you do

main(int argc, char *argv[])

You have a spot to keep the arguements, hence a pointer array.

The reason Command line arguements even EXIST is to avoid the user getting asked 2398573958734 questions before the program actually executing. You can instead open your CLI, and type this:

>test.exe -arguementONE

argc is a NUMBER OF ARGUEMENTS, NOT AN ARGUEMENT ITSELF. ARGV IS A POINTER ARRAY THAT HOLDS THEM. You only NEED those two variables when YOU WANT SOMETHING DONE FROM THE FUCKING COMMAND LINE.

If I do this:

>myCMDProgram -asdf 423 -blah

The variable argv will be filled with those values. You know how an array indexes at zero? Well, with CMD Line shit, argv[0] is ALREADY taken up, by the name of the program. argv[1] will hold the value "-asdf", argv[2] will hold "423" (note it will be counted as a STRING), and argv[3] will hold "-blah"
argc will hold a value of 4. Because argv[0] is counted.

argv can hold an infinate amount of shit like that. It's unlimited pretty much.

So if I have this program:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
if(argc != 3) { return -1; }
int a = atoi(argv[1]); int b = atoi(argv[2]); int c = a + b;
printf("%d",c);
return 0;
}

If you just double click on that it's not going to work.

You instead have to open up your CLI, and type in

test 23 42

And it will print "65"

Get it? Got it? Good.


omg.
Playstation Network tag: muffin-noodle
the empty set

DraculKaiser
DraculKaiser
  • Member since: Jun. 24, 2005
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to C++ Help :( 2005-06-24 13:02:49 Reply

I was just wondering, can anyone send me C++ and maybe a link to somewhere that can teach me how to use it?

dat-boi-Monk
dat-boi-Monk
  • Member since: Jun. 19, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to C++ Help :( 2005-06-24 13:08:52 Reply

ok, u need to calm your little ass down, if u going to reply like that don' reply at FUCKIN ALL

thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++ Help :( 2005-06-24 13:14:54 Reply

I answered your fucking question. I have the fucking right to reply any way I want. Just because you made the topic doesn't give you authority over it. Despite what you may believe.

Look, it's a personal fucking thing. I can't stand it when people ask 20395739485739485734958374 questions instead of trying to figure the rest out on their own after they've been given the answer or a tip because the person want's to test your aptitude.

At 6/24/05 01:02 PM, DraculKaiser wrote: I was just wondering, can anyone send me C++ and maybe a link to somewhere that can teach me how to use it?

http://austin.youareinferior.n...gramming_Language_3rd_Edition_(Stroustrup)
.rar


omg.
Playstation Network tag: muffin-noodle
the empty set

thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++ Help :( 2005-06-24 13:16:47 Reply

At 6/24/05 01:14 PM, Sinnernaut wrote: http://austin.youareinferior.n...gramming_Language_3rd_Edition_(Stroustrup)
.rar

Scratch that: This.


omg.
Playstation Network tag: muffin-noodle
the empty set

dat-boi-Monk
dat-boi-Monk
  • Member since: Jun. 19, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to C++ Help :( 2005-06-24 13:37:10 Reply

i'm so fuckin sorry sinneranugt, u r so right.......................

MainThink
MainThink
  • Member since: Apr. 5, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to C++ Help :( 2005-06-24 14:31:01 Reply

Geez...
Me.remember("do not piss Sinnernaut off");

Btw the suggested book is probably the best around.

As a matter of fact it should be good practice to include the cmd-line args stuff even if you don't need it. Just to keep a certain continuity.
When you start your program, you should hear a little voice saying "int main open round int argc char star argv square-square close round"

At least that's what happen to me... wait a minute: I hear voices in my head telling me strange things. I think I'm crazy.
I gotta see someone ... someone good.