Help please with function program.

This is a discussion on Help please with function program. within the C++ Programming forums, part of the General Programming Boards category; I am rather new to C++ and I have to write a program that will accept data for today's date, ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    23

    Help please with function program.

    I am rather new to C++ and I have to write a program that will accept data for today's date, first name, last name, and amount to an enterData() function and pass it to a printCheck() function. Then I need it to display in the correct spots on a check. I am stuck and am not really sure what to do next. Please help.

    [code]
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    // Function prototypes:
    void enterData();
    void printCheck();
    
    
    int _tmain(int argc, _TCHAR* argv[])
    
    {
    	enterData();  // call to function
    	printCheck(); // call to function
    
    	return 0;
    }
    
    void enterData()  // function
    {
    string todaysDate, firstName, 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;
    }
    
    void printCheck() //function
    {
        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 << "UnderSecurity Bank                                         "<<endl;
    	cout << "Missoula, MT                                               "<<endl;
    	cout << "                                                ____________________"<<endl;
    	cout << "                                                Authorized Signature";
    
    	cout << endl << endl;
    	
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    The strings you are entering are local variables, that will be destroyed when they go out of scope. IE: at the end of the function.

  3. #3
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57
    You could bundle your stuff up in a vector, then have
    Code:
    #include <vector>
    #include <iterator>
    
    ...
    
    std::vector<string> getData();   // returns vector of strings
    
    printCheck(std::vector<string>);   // takes vector of strings for an argument
    
    ...
    
    printCheck(getData());

    Joe


    EDIT: or you could make a class
    Last edited by avgprogamerjoe; 09-29-2007 at 06:07 PM.
    A quality job is not necessarily a *good* job. A quality job is, by definition, conformance to requirements. Therefore a *good* job, may not be a quality job.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You need a method to preserve the data entered in the enterData function so that it can later be displayed in the printCheck function. You can do this best by declaring the variables in the main function and passing them (by reference) into the enterData function. Any changes made to them in that function will be reflected in the values the variables have in the main function. Then you can again pass them to the printCheck function so they can be output.

    Code:
    void enterData(string&,string&,string&,double&);
    void printCheck();
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        string todaysDate, firstName, lastName;
        double amount;
    
        enterData(todaysDate,firstName,lastName,amount);  // call to function
        printCheck(); // call to function
    
    	return 0;
    }
    
    void enterData(string& todaysDate, string& firstName, string& lastName, double& amount)  // function
    {
        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;
    }
    
    void printCheck() //function
    {
        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 << "UnderSecurity Bank                                         "<<endl;
    	cout << "Missoula, MT                                               "<<endl;
    	cout << "                                                ____________________"<<endl;
    	cout << "                                                Authorized Signature";
    
    	cout << endl << endl;
    	
    }
    Now you just need to modify the bits in red above, the printCheck function related code, to accept those arguments and print them out. I'll leave that as an exercise for you.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    mco
    mco is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    1
    The quick way is to use global variables:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    // file scope variables
    static string todaysDate;
    static string firstName;
    static string lastName;
    static double amount;
    
    // Function prototypes:
    void enterData();
    void printCheck();
    
    int _tmain(int argc, _TCHAR* argv[])
    
    {
    	enterData();  // call to function
    	printCheck(); // call to function
    
    	return 0;
    }
    
    void enterData()  // function
    {
    
        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;
    }

    A better way is to wrap in a struct

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    struct Data {
        string todaysDate;
        string firstName;
        string lastName;
        double amount;
    }
    
    // Function prototypes:
    void enterData();
    void printCheck();
    
    int _tmain(int argc, _TCHAR* argv[])
    
    {
            Data d;
    	enterData(d);  // call to function
    	printCheck(d); // call to function
    
    	return 0;
    }
    
    void enterData(Data& d)  // function
    {
    
        cout << "Enter today's date: ";
    	cin  >> d.todaysDate;
    	cout << "Enter the first name: ";
    	cin  >> d.firstName;
    	cout << "Enter the last name: ";
    	cin  >> d.lastName;
    	cout << "Enter the amount: ";
    	cin  >> d.amount;
    }

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    Could anyone tell me if I have the check set up right? And if it is in the right place?

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    How can I get the data I enter to show up on the check?

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    I figured out how to get the data displayer but when I enter the date as Sep. 30, 2007 it only shows up as Sep. Any ideas what I am doing wrong?

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    When I type in the date it is putting the Sept. in the date spot but it is putting 30, 2007 in the pay to the order slot. Any ideas why?

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    string todaysDate;
    string firstName;
    string lastName;
    double amount;
    
    void enterData();
    void printCheck();
    
    
    int _tmain(int argc, _TCHAR* argv[])
    
    {
    	enterData();
    	printCheck();
    
    	return 0;
    }
    
    void enterData()
    {
    
    
        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 << endl << endl;
    }
    
    void printCheck()
    {
        cout << "Zzyz Corp                                      Date:     "<< todaysDate <<endl;
    	cout << "1164 Sunrise Avenue                                        "<<endl;
    	cout << "Kalispell, Montana\n                                         "<<endl;
    
    	cout << "Pay to the order of:" << firstName << lastName <<      "$" << amount << endl;
    	
        	
    
    	cout << "UnderSecurity Bank                                         "<<endl;
    	cout << "Missoula, MT                                               "<<endl;
    	cout << "                                                ____________________"<<endl;
    	cout << "                                                Authorized Signature";
    
    	cout << endl << endl;
    	
    }

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    I don't know what's wrong with your code, but the style is horrible. If you are going to use C++ you should learn how to use it right.

    In this case it means learning how to put data into a class instead of using global variables. Global variables are generally considered a bad thing and should be avoided.

    The way to do it is this:
    Your program deals with one thing: a check.
    With this check it does two things: it reads the data, and it prints the check.
    These two actions are properly viewed as features of the check. As such they should be made into methods of a check object that you create.

    So you need to learn how to use classes (objects), meathods(functions belonging to classes), and how to store data in classes.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    I just replied to this in another thread - stop posting the same thing multiple times.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    My teacher said to use global variables.

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    cout << "Enter today's date: ";
    cin  >> todaysDate;
    cin beaks up input according to whitespace (tabs, spaces, newlines). So If you enter "Sept. 30, 2007", only "Sept." gets stored into the todaysDate variable. And "30," and "2007" get stored into firstName and lastName (respectively). So it should be no surprise that you are getting weird output. What you need to do is change:
    Code:
    cin >> todaysDate ;
    Into:
    Code:
    getline(cin,todaysDate);
    This will read an entire line of input (up to the newline) and store it into todaysDate.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21