Need help writing a function.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gator6688
    New Member
    • Sep 2007
    • 63

    Need help writing a function.

    I have to write a program using an enterData() function and a printCheck() function. It has to be accepted by enterData() and passed to printCheck(). I need to be able to input the date, a persons first and last name, and an amount. Can anyone help me get started?
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by gator6688
    I have to write a program using an enterData() function and a printCheck() function. It has to be accepted by enterData() and passed to printCheck(). I need to be able to input the date, a persons first and last name, and an amount. Can anyone help me get started?
    So when you enter data, what type are you reading in? What will you do if it fails? How will you test the fail on condition? Do you know how to return values?

    Comment

    • gator6688
      New Member
      • Sep 2007
      • 63

      #3
      I hardly know any of this stuff. I don't want anyone to do it for me but I would like someone to take the time to help me and talk me through it so that I learn it.

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        I don't want anyone to do it for me but I would like someone to take the time to help me and talk me through it so that I learn it.
        Sure, not a problem. But what's your question? If you're not sure where to get started, then look at the question at hand. There are these two central functions, and it's obvious that you need to enter data before you can print a check. So focus on getting enter data to work perfectly for now. Forget about print check.

        You have four objects to store. Start with one of them. Let's make it easy. Start with the first name. How would you store a name? Is this C or C++? Your options vary depending on that. So focus on just one thing right now. Get enterData working, and for now, all it does is ask the person for his first name, and store that in the program.

        If you aren't sure how to go about even that much, then the problem here is just not knowing your language at all. The solution is to go to cplusplus.com or cprogramming.co m, or open up your textbooks, and do some reading.

        Comment

        • gator6688
          New Member
          • Sep 2007
          • 63

          #5
          Well i'm pretty sure that this isn't a user prompted program so I don't think I should be asking for the first name should I?

          Comment

          • gator6688
            New Member
            • Sep 2007
            • 63

            #6
            This program is C++.

            Comment

            • gator6688
              New Member
              • Sep 2007
              • 63

              #7
              Would this be the correct prototype?

              Comment

              • gator6688
                New Member
                • Sep 2007
                • 63

                #8
                Would this prototype be correct?

                void enterData(int date, char firstName, char lastName, double amount);

                Comment

                • gator6688
                  New Member
                  • Sep 2007
                  • 63

                  #9
                  Does this program look any better and how do I use printCheck to assign the data to the check?

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    Well, it actually DOES look like this program will be user-driven, so you'll have cin's to take care of setting values for those variables. However, let's talk about local variables:

                    [CODE=cpp]void enterData(int date, char firstName, char lastName, double amount);[/CODE]

                    What you have here are 4 copies of variables that you (presumably) will declare in main() and pass to enterData(). By the name of enterData, you should be reading data into these variables, which can then be used back in main(). However, these are only copies. When you call enterData:

                    [CODE=cpp]enterData(myDat e, myFirstName, myLastName, myAmount);[/CODE]

                    the computer takes the values that myDate, myFirstName, etc. had, makes a copy of them, and gives them to the enterData function to work with. You then read values into these copies. Once the function is done, these copies are destroyed - and you've done nothing to the original copies of those variables!

                    We need a way to let enterData see those variables. One (very bad) way would be to declare these variables outside of main(). That way, every function can see the varables. You don't need parameters for your enterData function anymore - it should just get the variables directly. This is a bad approach, however, because printCheck might be called before these variables are given values. That, and countless other reasons, are why you should not use these 'global variables'. So what do you do?

                    Use 'pass by reference':

                    [CODE=cpp]void enterData(int &date, char &firstName, char &lastName, double &amount);[/CODE]

                    Ahh...this will work. That little & sign means that, instead of receiving a copy, the function will basically receive the actual variable. Anything done to the variables in enterData will be reflected back in main(). Cool, eh?

                    As a side note...should you be using chars for firstName and lastName? Wouldn't an std::string be better? Or (if you like C legacy style), a character array (called a CString)?

                    Comment

                    • gator6688
                      New Member
                      • Sep 2007
                      • 63

                      #11
                      I did change it to string. Does this look any better?

                      Comment

                      • gator6688
                        New Member
                        • Sep 2007
                        • 63

                        #12
                        Could someone look this over and tell me what else I could do?

                        [CODE=cpp]#include "stdafx.h"
                        #include <iostream>
                        #include <string>
                        using namespace std;

                        void enterData(strin g todaysDate, string firstName, string lastName, double amount);
                        void printCheck(stri ng todaysDate,stri ng firstName,strin g lastName,double amount);




                        int _tmain(int argc, _TCHAR* argv[])
                        {

                        string todaysDate,firs tName, lastName;
                        double amount;



                        cout << "Enter today's date: ";
                        cin >> todaysDate;
                        cout << "Enter the first name: ";
                        cin >> firstName;
                        cout << "Enter the last name: ";
                        cin >> lastName;
                        cout << "Enter the amount: ";
                        cin >> amount;


                        cout << "Zzyz Corp Date: (today's date)"<<endl;
                        cout << "1164 Sunrise Avenue "<<endl;
                        cout << "Kalispell, Montana\n "<<endl;

                        cout << "Pay to the order of: (firstName lastName) $ (amount)\n "<<endl;
                        cout << firstName << " " << lastName;
                        cout << " $" << amount << endl;


                        cout << "UnderSecur ity Bank "<<endl;
                        cout << "Missoula, MT "<<endl;
                        cout << " _______________ _____"<<endl;
                        cout << " Authorized Signature";

                        cout << endl << endl;
                        return 0;
                        }[/CODE]

                        Comment

                        • gator6688
                          New Member
                          • Sep 2007
                          • 63

                          #13
                          Anyone have any suggestions?

                          Comment

                          • gator6688
                            New Member
                            • Sep 2007
                            • 63

                            #14
                            Anyone out there with any suggestions?

                            Comment

                            • Ganon11
                              Recognized Expert Specialist
                              • Oct 2006
                              • 3651

                              #15
                              Yes, there is: however, remember that this is a site with volunteers that are willing to help you out. Please have a little more patience when waiting for people to help you out.

                              It looks like you aren't even using your functions! You declare them, but then have all the code inside main(). Try actually writing the functions.

                              Comment

                              Working...