perfecto25 Posted April 20 Report Share Posted April 20 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 appropiatelyexample: cshell> a.out myprog.cpp 45 200so 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!------------------ .....its dark in here, you can feel it all around....the UNDERGROUND... Quote Link to comment Share on other sites More sharing options...
Guest danwang Posted April 20 Report Share Posted April 20 i think the first number is argv[2] and the second one is argv[3]You might need to cast them to intI 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).] Quote Link to comment Share on other sites More sharing options...
emiliep Posted April 20 Report Share Posted April 20 wow i didn't understand one word you wrote Quote Link to comment Share on other sites More sharing options...
frenchbread Posted April 20 Report Share Posted April 20 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 200your 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 200argc 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 200argv[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;} Quote Link to comment Share on other sites More sharing options...
Unbound Posted April 20 Report Share Posted April 20 anybody know e++? how you been bro?...is tha for school or work or play?...------------------I WANNA BE A TWILO CLOSET KID!!!!AIM: ArthurBrooklyn TëMA Quote Link to comment Share on other sites More sharing options...
gambitx73 Posted April 20 Report Share Posted April 20 French Bread...I'm impressed! If any recruiter should ask me of a C++ expert I'll give them your name. French Bread! Quote Link to comment Share on other sites More sharing options...
frenchbread Posted April 20 Report Share Posted April 20 thanks gambit...you coming to the meet tonight?... Quote Link to comment Share on other sites More sharing options...
Guest danwang Posted April 20 Report Share Posted April 20 Originally posted by emiliep:wow i didn't understand one word you wroteGeek-speak Stuff that helps pay the bills...------------------We're alive, take a breathWe're alive, take a deep breath Quote Link to comment Share on other sites More sharing options...
JackMeOff Posted April 20 Report Share Posted April 20 damn amateurs!------------------ Quote Link to comment Share on other sites More sharing options...
perfecto25 Posted April 20 Author Report Share Posted April 20 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!!! ------------------ .....its dark in here, you can feel it all around....the UNDERGROUND... Quote Link to comment Share on other sites More sharing options...
frenchbread Posted April 20 Report Share Posted April 20 hey no problem perfecto...are you coming to the meet tonight?...hopefully i see you there... Quote Link to comment Share on other sites More sharing options...
perfecto25 Posted April 20 Author Report Share Posted April 20 oh theeres a meetup? where is it, what time? maybe i can still make it, im gona buy u some drinks bro, im compiling this baby as we speak!------------------ .....its dark in here, you can feel it all around....the UNDERGROUND... Quote Link to comment Share on other sites More sharing options...
teklord310 Posted April 20 Report Share Posted April 20 I'm taking C++ for Computer Engineers now. I hate programming, otherwise i'd be comp sci. Damn C++. Good luck------------------"1,1,2,3,5,8,13,21 ....Mathematics is the language of nature" --BTAIM: teklord310 Quote Link to comment Share on other sites More sharing options...
Guest danwang Posted April 21 Report Share Posted April 21 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 breathWe're alive, take a deep breath Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.