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?
Need help writing a function.
Collapse
X
-
Originally posted by gator6688I 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? -
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.
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
-
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
-
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
-
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
Comment