473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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?
Sep 28 '07 #1
23 1902
sicarie
4,677 Expert Mod 4TB
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?
Sep 28 '07 #2
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.
Sep 28 '07 #3
oler1s
671 Expert 512MB
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.com, or open up your textbooks, and do some reading.
Sep 28 '07 #4
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?
Sep 28 '07 #5
This program is C++.
Sep 28 '07 #6
Would this be the correct prototype?
Sep 28 '07 #7
Would this prototype be correct?

void enterData(int date, char firstName, char lastName, double amount);
Sep 28 '07 #8
Does this program look any better and how do I use printCheck to assign the data to the check?
Sep 28 '07 #9
Ganon11
3,652 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. void enterData(int date, char firstName, char lastName, double amount);
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:

Expand|Select|Wrap|Line Numbers
  1. enterData(myDate, myFirstName, myLastName, myAmount);
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':

Expand|Select|Wrap|Line Numbers
  1. void enterData(int &date, char &firstName, char &lastName, double &amount);
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)?
Sep 28 '07 #10
I did change it to string. Does this look any better?
Sep 29 '07 #11
Could someone look this over and tell me what else I could do?

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void enterData(string todaysDate, string firstName, string lastName, double amount);
  7. void printCheck(string todaysDate,string firstName,string lastName,double amount);
  8.  
  9.  
  10.  
  11.  
  12. int _tmain(int argc, _TCHAR* argv[])
  13. {
  14.  
  15.     string todaysDate,firstName, lastName;
  16.     double amount;
  17.  
  18.  
  19.  
  20.     cout << "Enter today's date: ";
  21.     cin  >> todaysDate;
  22.     cout << "Enter the first name: ";
  23.     cin  >> firstName;
  24.     cout << "Enter the last name: ";
  25.     cin  >> lastName;
  26.     cout << "Enter the amount: ";
  27.     cin  >> amount;
  28.  
  29.  
  30.     cout << "Zzyz Corp                                      Date: (today's date)"<<endl;
  31.     cout << "1164 Sunrise Avenue                                        "<<endl;
  32.     cout << "Kalispell, Montana\n                                         "<<endl;
  33.  
  34.     cout << "Pay to the order of: (firstName lastName)      $ (amount)\n "<<endl;
  35.     cout << firstName << " " <<  lastName;
  36.     cout << "      $" << amount << endl;
  37.  
  38.  
  39.     cout << "UnderSecurity Bank                                         "<<endl;
  40.     cout << "Missoula, MT                                               "<<endl;
  41.     cout << "                                                ____________________"<<endl;
  42.     cout << "                                                Authorized Signature";
  43.  
  44.     cout << endl << endl;
  45.     return 0;
  46. }
Sep 29 '07 #12
Anyone have any suggestions?
Sep 29 '07 #13
Anyone out there with any suggestions?
Sep 29 '07 #14
Ganon11
3,652 Expert 2GB
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.
Sep 29 '07 #15
Could you please give me an example of declaring a function.
Sep 29 '07 #16
Ganon11
3,652 Expert 2GB
You've already declared your functions:

Expand|Select|Wrap|Line Numbers
  1. void enterData(string Date, string FirstName, string LastName, double amount);
Thia is an example of declaring a function. You have to define it now:

Expand|Select|Wrap|Line Numbers
  1. void enterData(string Date, string FirstName, string LastName, double amount) {
  2.    // Your code here.
  3. }
Sep 29 '07 #17
oler1s
671 Expert 512MB
Could you please give me an example of declaring a function.
We could, but we won't. We're here to help you learn, but if you need us to show you how to declare a function, the problem is you just aren't doing the necessary reading. If you are struggling with writing functions, I suggest reading some online tutorials (preferably the ones at cplusplus.com or cprogramming.com or your textbook).
Sep 29 '07 #18
How long would it take you to write this program?
Sep 29 '07 #19
Plus, all the tutorials I read is for a function adding two intergers a+b which is easy. I have to be able to input the info into one function and have it passed on to another and have it displayed in the check. Yes for most of you it would probably be easy but I have only been learning C++ for 5 weeks and most of this stuff is still difficult for me.
Sep 29 '07 #20
Somebody please put me out of my misery and write this for me. Please.
Sep 29 '07 #21
Ganon11
3,652 Expert 2GB
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

Then when you are ready post a new question in this thread.

MODERATOR

We've given you a lot of help so far. You've got the code in your function! You've already written the majority of your program! All you need to do is copy and paste the relevant code and put it in the correct function - as we've already demonstrated. There is nothing else we can do for you.
Sep 29 '07 #22
How can I get the data I enter to be sent to the check? Also, how come when it asks me for the first and last name and amount it all goes on the same line?

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. string todaysDate;
  7. string firstName;
  8. string lastName;
  9. double amount;
  10.  
  11. void enterData();
  12. void printCheck();
  13.  
  14.  
  15. int _tmain(int argc, _TCHAR* argv[])
  16.  
  17. {
  18.     enterData();
  19.     printCheck();
  20.  
  21.     return 0;
  22. }
  23.  
  24. void enterData()
  25. {
  26.  
  27.  
  28.     cout << "Enter today's date: ";
  29.     cin  >> todaysDate;
  30.     cout << "Enter the first name: ";
  31.     cin  >> firstName;
  32.     cout << "Enter the last name: ";
  33.     cin  >> lastName;
  34.     cout << "Enter the amount: ";
  35.     cin  >> amount;
  36. }
  37.  
  38. void printCheck()
  39. {
  40.     cout << "Zzyz Corp                                      Date: (today's date)"<<endl;
  41.     cout << "1164 Sunrise Avenue                                        "<<endl;
  42.     cout << "Kalispell, Montana\n                                         "<<endl;
  43.  
  44.     cout << "Pay to the order of: (string firstName lastName)      $ (amount)\n "<<endl;
  45.  
  46.  
  47.  
  48.     cout << "UnderSecurity Bank                                         "<<endl;
  49.     cout << "Missoula, MT                                               "<<endl;
  50.     cout << "                                                ____________________"<<endl;
  51.     cout << "                                                Authorized Signature";
  52.  
  53.     cout << endl << endl;
  54.  
  55. }
Oct 1 '07 #23
sicarie
4,677 Expert Mod 4TB
How can I get the data I enter to be sent to the check?
You declare check to be void check() - so you're not passing anything to it, and you are not returning anything from it. You need to pass it the relevant variables, and return the relevant type.
Also, how come when it asks me for the first and last name and amount it all goes on the same line?
Because you haven't told it not to. I would recommend putting a newline characters around what you want to be on a different line. The newline char is '\n', and can be incorporated into any string to create a new line.
Oct 1 '07 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dariusz | last post by:
I would like to try writing my own functions, so as to keep some code within the same PHP file rather than make multiple PHP files. What I am having a problem is is how to call a function when a...
2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
3
by: Buddy Robbins | last post by:
Hey folks, I'm trying to use the PathCleanupSpec function from the shell library. The function prototype is: int PathCleanupSpec( LPCWSTR pszDir, LPWSTR pszSpec) In the old days of VB6, I...
3
by: Chris H | last post by:
bassically i am buiding a simple shopping cart type feature and am using sessions to store the cart contents and then when teh user checks out and submits the order it will email me the cart...
1
by: Rizwan | last post by:
I am writing this code for file upload in asp 3.0 on my webpage........ It is working on local system but when i want to upload an image on my website from client machine it is not working ..........
43
by: SLH | last post by:
hi people. im trying to validate input received via a text area on an ASP page before writing it to a database. i cant use client side javascript due to policy, so it all has to happen on the...
5
by: elsa | last post by:
hi i need help in writing a function integerPower(base,exponent) that returns the value base^exponent with assuming that the exponent is a positive, nonzero integer and that the base is an integer....
1
by: Smita Prathyusha | last post by:
I am facing a problem in writing to COM1. I am using a Win 32 Console mode Program in VC++ the following is the code: If anyone can help me out it will be of great help : // SC_Using_Classes.cpp...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.