#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    11
    Rep Power
    0

    Functions question


    the following is a little program im working on to increase my knowledge of functions... it gives me 2 errors that i cant figure out...

    on lines 47 and 61, compiler indicates there are too few arguments... what does this mean

    here is the code

    T.I.A.

    Code:
    /*
      Name: jCalc
      Author: Jarrod Swart
      Date: 22/09/03 19:05
      Description: A simple 4 function calculator to improve my knowlege and understanding of functions.
    */
    
    
    #include <iostream>
    using namespace std;
    
    // This begins the "graphical menu"
    void menuText()
        {
            cout << "[][][][][][][][][][][][][][][][][][][]\n";
            cout << "[]                                  []\n";
            cout << "[] jCalc - 4 function calculator    []\n";
            cout << "[]   ----------------------------   []\n";
            cout << "[] 1. Add                           []\n";
            cout << "[] 2. Subtract                      []\n";
            cout << "[] 3. Multiply                      []\n";
            cout << "[] 4. Divide                        []\n";
            cout << "[] 5. About                         []\n";
            cout << "[]   ----------------------------   []\n";
            cout << "[] Input your choice, press enter   []\n";
            cout << "[]                                  []\n";
            cout << "[][][][][][][][][][][][][][][][][][][]\n";
            
        }
    // end menu
     
     
    // begin menu option 5 - about   
    void aboutText()
        {
            cout << "jCalc was created by Jarrod Swart as a means to improve understanding of functions and general C++ knowledge.\n";
        }
    // end menu option 5  
    
    // a function to accept a value and go to the specified part of the program
    // ie: if 5 is entered, it displays aboutText()
    // currently only five is an option    
    int menuSelection(int menuOption)
        { 
            cin >> menuOption;
                if (menuOption == 5)
                    {
                        aboutText();
                    }
        }
        
        
        
        
    int main()
        {
            menuText(); //display graphic text
            menuSelection(); //allow user to input option
            system("pause");
            return 0;
        }
    Last edited by azrael; September 22nd, 2003 at 06:41 PM.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    menuSelection() takes an argument, you must supply it when you call it. Also, use of system calls should always be avoided. Use getchar() or something to keep the program from closing until you have viewed the results.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Actually, only one of your errors should be too few arguments. The other error should be that you neglected to return a value.

    You declared the function menuSelection as taking one argument and returning an int. However, there is no return statement within the function. If you tell the compiler that you intend to return a value (by declaring the function's return type to be non-void), then you are required to include a return statement that returns that value. If you do not, then it is an error.

    Now, when you call that function, you are not required to look at that return value, so that's not the error. However, when you call menuSelection, you failed to give it an argument. Again, since you told the compiler that the function takes one argument, you are required to provide it with one argument every time you call it. If you do not, then that is also an error.

    Which accounts for the errors you got.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    11
    Rep Power
    0
    thanks for the help but i have no idea what im supposed to do in order to make the program function properly...i am an utter newbie
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by azrael
    thanks for the help but i have no idea what im supposed to do in order to make the program function properly...i am an utter newbie
    You declared the function, menuSelection, to expect one argument (int menuOption), so give it one!

    You declared the function, menuSelection, to return an int value, so put a return statement in there!
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    11
    Rep Power
    0
    the code now reads:

    Code:
    #include <iostream>
    using namespace std;
    
    // This begins the "graphical menu"
    void menuText()
        {
            cout << "[][][][][][][][][][][][][][][][][][][]\n";
            cout << "[]                                  []\n";
            cout << "[] jCalc - 4 function calculator    []\n";
            cout << "[]   ----------------------------   []\n";
            cout << "[] 1. Add                           []\n";
            cout << "[] 2. Subtract                      []\n";
            cout << "[] 3. Multiply                      []\n";
            cout << "[] 4. Divide                        []\n";
            cout << "[] 5. About                         []\n";
            cout << "[]   ----------------------------   []\n";
            cout << "[] Input your choice, press enter   []\n";
            cout << "[]                                  []\n";
            cout << "[][][][][][][][][][][][][][][][][][][]\n";
            
        }
    // end menu
     
     
    // begin menu option 5 - about   
    void aboutText()
        {
            cout << "jCalc was created by Jarrod Swart as a means to improve understanding of functions and general C++ knowledge.\n";
        }
    // end menu option 5  
    
    // a function to accept a value and go to the specified part of the program
    // ie: if 5 is entered, it displays aboutText()
    // currently only five is an option    
    int menuSelection(int menuOption)
        { 
            cin >> menuOption;
                if (menuOption == 5)
                    {
                        aboutText();
                        return 0;
                    }
        }
        
        
        
        
    int main()
        {
            menuText(); //display graphic text
            menuSelection(int menuOption); //allow user to input option
            system("pause");
            return 0;
        }
    yet it gives me a parse error on line 59...

    what should i do now
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    11
    Rep Power
    0
    line 59 being

    Code:
    menuSelection(int menuOption); //allow user to input option
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by azrael
    the code now reads:

    Code:
    #include <iostream>
    using namespace std;
    
    // This begins the "graphical menu"
    void menuText()
        {
            cout << "[][][][][][][][][][][][][][][][][][][]\n";
            cout << "[]                                  []\n";
            cout << "[] jCalc - 4 function calculator    []\n";
            cout << "[]   ----------------------------   []\n";
            cout << "[] 1. Add                           []\n";
            cout << "[] 2. Subtract                      []\n";
            cout << "[] 3. Multiply                      []\n";
            cout << "[] 4. Divide                        []\n";
            cout << "[] 5. About                         []\n";
            cout << "[]   ----------------------------   []\n";
            cout << "[] Input your choice, press enter   []\n";
            cout << "[]                                  []\n";
            cout << "[][][][][][][][][][][][][][][][][][][]\n";
            
        }
    // end menu
     
     
    // begin menu option 5 - about   
    void aboutText()
        {
            cout << "jCalc was created by Jarrod Swart as a means to improve understanding of functions and general C++ knowledge.\n";
        }
    // end menu option 5  
    
    // a function to accept a value and go to the specified part of the program
    // ie: if 5 is entered, it displays aboutText()
    // currently only five is an option    
    int menuSelection(int menuOption)
        { 
            cin >> menuOption;
                if (menuOption == 5)
                    {
                        aboutText();
                        return 0;
                    }
        }
        
        
        
        
    int main()
        {
            menuText(); //display graphic text
            menuSelection(int menuOption); //allow user to input option
            system("pause");
            return 0;
        }
    yet it gives me a parse error on line 59...

    what should i do now
    I said give menuSelection an argument, not insert a declaration. What you wrote is not C. You need to pass the function either an integer literal or an int variable; e.g.: menuSelection(0); .

    Also, when a function requires a return statement, ALL paths through that function must eventually end with a return statement. As you now have it, menuSelection will only return a value if the option is 5 and will otherwise not return a value. The compiler should have could that error as well.
    Code:
    int menuSelection(int menuOption)
        { 
            cin >> menuOption;
                if (menuOption == 5)
                    {
                        aboutText();
                    }
            return 0;
        }
    Now, what are you trying to do in that function? You are passing in a menuoption, but then you immediately replace it with user input. Why pass it in then? That doesn't make any sense. And what value are you returning? A function is supposed to perform a function. What function is this function supposed to perform?
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    2
    Rep Power
    0

    Question to mitakeet


    mitakeet
    just curious as to why you said "use of system calls should always be avoided" ??...is it just from a portability issue or is there another reason.?? :confused:
  18. #10
  19. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    One issue is certainly portablity. Another is security. It is fairly trivial to substitute another version of the same application on a different path and have that executed instead. This is not to say that I haven't done that myself from time to time, but since almost all OS's have been developed in C it doesn't make much sense to make system calls when the full OS functionality is available with just a little bit of research. In this particular case, the system call is being used to 'preserve' a DOS popup screen long enough to see what is on it. That could be done just as easily with a getchar() or something similiar. Don't use a screwdriver to do the job of a hammer!

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    2
    Rep Power
    0

    Lightbulb mitakeet


    point taken.......Never thought about security ....Still to new to C++ to have reached security issues, but I'll file this under make a "habit" not to use system calls-same catagory as goto.
    Thanks..........:)
  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    11
    Rep Power
    0
    dwise... the following is unfinished and just a small part, the purpose of the function is to take a number 1-5 and then execute that particular function...
    1. Add
    2. Subtract
    etc.

    I have only done #5 thus far...

IMN logo majestic logo threadwatch logo seochat tools logo