Arguments and Functions HELP QUICK! PLEASE!
Sorry, for this post as this probably not the place to post this, but I'm desperate.
I'm writing a program right now that I am having trouble setting up arguments so the user can input a command line argument "-p #" and use said number to set the number of decimals to calculate Pi to. I am getting error messages when running my compiled program. I'm trying to get a project done for school and I don't have any more time to work on it besides now. So if anyone would could look at this code and tell me if they see anything screwy, please let me know:
Also I have function:
Is this how you refer to said function in main ():
??
I'm writing a program right now that I am having trouble setting up arguments so the user can input a command line argument "-p #" and use said number to set the number of decimals to calculate Pi to. I am getting error messages when running my compiled program. I'm trying to get a project done for school and I don't have any more time to work on it besides now. So if anyone would could look at this code and tell me if they see anything screwy, please let me know:
if ( argc <= 1 ) { cerr << "Please specify how many decimal points\n"; cerr << "that you would like to see Pi in by\n"; cerr << "typing a command line argument in the\n"; cerr << "form of {-p #} where # is a number\n"; cerr << "that is chosen from 1 through 10\n"; return 2; } else { argv++; while ( --argc > 1 ) { if ( **argv == '-' ) { switch ( *(*argv+1)) { case 'p': userchoice = true; break; default: cerr << "picalculation: ERROR: illegal argument: " << *argv << endl; return -1; } } else { usernum[usernumcount++] = atof(*argv); } ++argv; } if (usernumcount != 4) { cerr << "picalculation: ERROR: incorrect number of arguments" << endl; return -1; } } cout << "You entered: " << usernum[0] << endl;
Also I have function:
int func( int userchoice ) {
Is this how you refer to said function in main ():
int func( usernumcount );
??
0
Comments
ex:
[php] [/php]
2) to call your function just: You'll want to store the return value somewhere.
3) You should have started your homework earlier this weekend.
I know.
I cannot get "usernum[1]" to store properly. It is displaying some crazy value.
what type array is usernum?
why are you using atof() to convert the argument? Is the user ever going to ask for 3.5 decimal places?
I want usernum to be an array that accepts integers of whole values.
I wasn't real sure what exactly it was atof() did. Is there an alternative to atof() that will convert *argv into an integers?
also:
*****after this code, usernumcount, will likely never be 4.
atof turns a string into a float (double, whatever). atoi turns a string into an...(I'll let you look it up)
It looks to me like you're not really iterating through the design. I usually start by assuming that the user will input the arguments correctly. Once that works, then I parse the actual arguments and add error checking. Compile and test after each minor change - it looks like you wrote all that, based on some example you got in class, and now it all doesn't work, and you have no idea what the problem is.
I'm not really trying to be a dick, but you should be the one doing your homework.
I didn't perceive you as being one, but I have been doing my ****ing homework for 7 hours and I have no where else to go for help.
I started working on this Friday.
The problem is that my professor just sits up there and types examples and casually explains what he's doing without any reference to our book or any preparation we were supposed to have done (we aren't assigned anything to read we just go in there blindly. I would read ahead but I can't because he is not following the book at all)- he does it as if it's a refresher course. I have been working on this since 4pm today - Our project isn't based on anything in the book since we don't follow it and it is very loosely based on the examples that he does in class.
The best advice I have is to just do things in small steps. It stops the "HOLY CRAP I HAVE 8 MILLION ERRORS" problem when you compile a lot of changes for the first time.
Thanks for the help you could provide.
Does this mean that usernum is a function with respect to "usernumcount++"
If so, what does the "++" indicate?
using the brackets means index it. So:
usernum[0] means get the zeroeth value in the array.
usernum[1] means get the first value
...
and so on.
usernumcount is the index you're using to get the value from the array.
by saying usernumcount++, you're getting the value from usernumcount, then storing usernumcount + 1 back in usernumcount.
Its the same as
usernum[usernumcount];
usernumcount = usernumcount + 1;
If you're really fudged, I'd talk to the prof about an extension, or just about the class in general. But do it asap, not right before class.