Arguments and Functions HELP QUICK! PLEASE!

edited September 2006 in Internet & Media
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:
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 );


??

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited September 2006
    1) post your code in code tags, so it can be easily read:
    ex:
    [php]
    code here!
    
    [/php]

    2) to call your function just:
    func(usernumcount);
    
    You'll want to store the return value somewhere.

    3) You should have started your homework earlier this weekend.
  • edited September 2006
    "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.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited September 2006
    did you allocate memory for your usernum array?
    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?
  • edited September 2006
    I'm trying to allocate memory for usernum with:
    double usernum[1];
    

    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?

    :scratch:
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited September 2006
    why bother with an array that is 1 in length? Just store it in an int.

    also:
    ...
    	usernum[usernumcount++] = atof(*argv);
          }
          ++argv;
        }
    *****
        if (usernumcount != 4) {
          cerr << "picalculation: ERROR: incorrect number of arguments" << endl;
          return -1;
        }
    

    *****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.
  • edited September 2006
    "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.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited September 2006
    Keep asking questions, I'll gladly answer them (for a while - it's only 9pm here anyways). As long as you're not expecting me to turn the code you had into something you can copy/paste. And I don't think you were really asking for that anyways.

    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.
  • edited September 2006
    I'm not at all expecting you to edit my code. I'm looking for guidance. If I think of anything else to ask I will.

    Thanks for the help you could provide.
  • edited September 2006
    I don't understand one part of a statement:
    usernum[usernumcount++]
    

    Does this mean that usernum is a function with respect to "usernumcount++"

    If so, what does the "++" indicate?
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited September 2006
    usernum is an array (you declared it as one).

    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;
  • edited September 2006
    Thanks for your help. I've done all I can and I'm starting to get somewhere, but I'm about to hit the sac because I am exhausted.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited September 2006
    Good luck.

    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.
Sign In or Register to comment.