Jump to content
Clubplanet Nightlife Community

any C++ peepz? help a brotha out!


Recommended Posts

ok, i got this program to write for a operating sys class, anyway, it has to read in variables from command line, and then use a function (atoi) to convert the string into an integer, and assign variables appropiately

example: cshell> a.out myprog.cpp 45 200

so a.out would be argv[0], myprog.cpp would be argv[1] etc.. the ??? is how do i assign the numbers 45 and 200 as integers in the main() ?? im a fucking retard with this language, heeeelp!

------------------

perf_russ.gif

.....its dark in here, you can feel it all around....the UNDERGROUND...

Link to comment
Share on other sites

Guest danwang

i think the first number is argv[2] and the second one is argv[3]

You might need to cast them to int

I haven't done much C++ recently (and what I have done & seen lately was in Visual C++ with all the cool IDE features), so I'm not sure of the details, but I think this is the general idea.

[This message has been edited by danwang (edited 04-20-2001).]

Link to comment
Share on other sites

hey perfecto...

i got you man...

no problem...

the way it works is like this:

after you compile your program, you get an executable called a.out or whatever you named it...

when you want to run it, you will call it like this:

prompt$> a.out 45 200

your main routine should be declared the following way:

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

{

...

your code...

...

}

1. argc is number of argument passed to your program(always equal to one or greater...)

2. *argv[] is a pointer to the array of parameters...(your parameters are all saved in string format...)...

in this example:

prompt$> a.out 45 200

argc is equal to 3...

you have three parameters...two that you supplied and one that is automatic...the first parameter is always your program's name...which is why argc is always at least equal to one or greater...

so now...

how do you access the paramters inside your program...

very easy:

again for this example:

prompt$> a.out 45 200

argv[0] is equal to the string "a.out"

argv[1] is equal to the string "45"

argv[2] is equal to the string "200"

so inside your program you have access to all the variables...

all you have left to do is to use atoi to convert your would-be-numeric-but-presently-string parameters to numbers...

example:

int param1 = atoi(argv[1]);

int param2 = atoi(argv[2]);

...get it...

so here is a quick sample program that sould run under unix:

#include <iostream.h>

#include <stdio.h>

#include <stdlib.h>

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

{

int i;

int* param_array;

cout << "number of arguments: " << argc << endl;

// print the args since they are strings...

cout << "here are all your parameters, including the first one appname):" << endl;

for(i = 0; i < argc; i++)

{

cout << "argument number " << i << ": " << argv << endl;

}

//now convert the numerical args from string to int and save them in

//an int array

param_array = new int[argc - 1];

for(i = 0; i < argc - 1; i++)

{

param_array = atoi(argv[i + 1]);

}

//now display you int parameters...

cout << "here are your user-supplied parameters as ints:" << endl;

for(i = 0; i < argc - 1; i++)

{

cout << "user-supplied parameter " << i+1 << ": " << param_array << endl;

}

return 0;

}

Link to comment
Share on other sites

Guest danwang

Originally posted by emiliep:

wow i didn't understand one word you wrote

Geek-speak smile.gif

Stuff that helps pay the bills...

------------------

We're alive, take a breath

We're alive, take a deep breath

Link to comment
Share on other sites

French bread, u are a fucking lifesaver, i just got to school, the computer lab closes in 15 min, im crying my ass off cause i cant compile this shit, and u, u come in and save the day, dude thanks a mil! im buying u drinks when i see u, <applause>!!! holy shit i am saved by frenchbread's wisdom. may he go to heaven with a dozen brazilian virgins! oh artur, i dont do this for play, this is for school, its a fucking hemroid, i gotta simulate a round robin scheduling algorithm, yawwwwn! thanks for the help guys!!!

------------------

perf_russ.gif

.....its dark in here, you can feel it all around....the UNDERGROUND...

Link to comment
Share on other sites

Guest danwang

frenchbread, perfecto, did you make it?

Too bad I didn't get to meet with you guys. We probably see each other though. maybe next time...

------------------

We're alive, take a breath

We're alive, take a deep breath

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...