944,093 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 672
  • C++ RSS
Nov 29th, 2007
0

Making my void functions look at little better

Expand Post »
Hey all,

I was wondering if any of you could help me out a little but with my functions which I'm writing for a program. I recently have noticed that I'm simply assuming the person using my program would be someone who also uses Bloodshed C++, so I think I've left some loopholes in my functions. Given a smart user the program should do fine but given a normal user, or even an accidental click, the program would start giving out nonsense to the user.

Its basically a money managing program (i'd posted a question for it earlier on when it was in the preliminaries). The initial part of the program is not required I think, all it does is declare functions and then the int main () runs them. So I'll be omitting them.


C++ Syntax (Toggle Plain Text)
  1. void income_menu ()
  2. {
  3.  
  4. for (;;) // C/C++ idiom for (loop) forever
  5. {
  6. system ("cls");
  7. int update_income_choice;
  8.  
  9. const string KeywordIncomePaycheck = "Paycheck: ";
  10. const string KeywordIncomeInterest = "Income: ";
  11. double paycheck;
  12. double interest;
  13. ofstream income;
  14. income.open ("Income.txt", ios::in);
  15.  
  16. cout << "You have chosen to update Income entries" << endl;
  17. cout << "Please enter the number for the option you wish to modify" << endl;
  18. cout << "*******************" << endl;
  19. cout << "* 1. Paychecks *" << endl;
  20. cout << "* 2. Interest *" << endl;
  21. cout << "*******************" << endl;
  22. cin >> update_income_choice;
  23.  
  24. if (update_income_choice == 1)
  25. {
  26. cout << "Please enter the ammount (in RM) of the paycheck" << endl;
  27. cin >> paycheck;
  28. income << KeywordIncomePaycheck << paycheck << endl;
  29. }
  30. else if (update_income_choice == 2)
  31. {
  32. cout << "Please enter the ammount (in RM) of the Interest" << endl;
  33. cin >> interest;
  34. income << KeywordIncomeInterest << interest << endl;
  35. }
  36.  
  37. bool tcp(true);
  38. while ( tcp )
  39. {
  40. char ans1;
  41.  
  42. cout << "do you wish to go back to main?"
  43. "(y for yes and n for no)"
  44. << endl;
  45.  
  46. cin >> ans1;
  47.  
  48. switch (ans1)
  49. {
  50. case 'y':
  51. case 'Y':
  52. mainmenu(); // return to caller
  53. break;
  54.  
  55. case 'n':
  56. case 'N':
  57. tcp = false; // input OK, set tcp false
  58. // to quit while loop
  59. break; // break now required !!!
  60.  
  61. default:
  62. cout << "\nPlease enter y or n\n";
  63. break;// not required but add it anyway!
  64. } // end of switch
  65. } // end of while ( tcp )
  66.  
  67. } // end for-ever for loop
  68. }; // end of function


^that would be the menu that lets the user key in income ammounts, a paycheck and/or the interest. but see this is assuming the user keys in 1 or 2. What if the user presses in a letter? What measures can I take to ensure the user knows when he/she is making a error and how can I take them through the function smoothly? Sample functions? Anyone? I'm posting the rest of the functions too. One's on fixed expenses and one's on variable expenses.


C++ Syntax (Toggle Plain Text)
  1. void fixed_expenses_menu ()
  2. {
  3. for (;;)
  4. {
  5. system ("cls");
  6. int fixed_expenses_choice;
  7.  
  8. double car_insurance_ammount;
  9. double cell_phone_charges_ammount;
  10. double student_loan_ammount;
  11. double rent_ammount;
  12. double others_ammount;
  13.  
  14. const string KeywordCarInsurance = "Car Insurance: ";
  15. const string KeywordCellPhoneChargesAmmount = "Cell Phone Charges Ammount: ";
  16. const string KeywordStudentLoanAmmount = "Student Loan: ";
  17. const string KeywordRentAmmount = "Rent Ammount: ";
  18. const string KeywordOthersAmmount = "Others: ";
  19.  
  20. ofstream fixed_expenses;
  21. fixed_expenses.open ("Fixed Expenses.txt", ios::in);
  22.  
  23. cout << "You have chosen to update Fixed Expenses" << endl;
  24. cout << "Which Fixed Expense would you like to add?" << endl;
  25. cout << "%%%%%%%%%%%%%%%%%%%%%" << endl;
  26. cout << "% 1. Car Insurance %" << endl;
  27. cout << "% 2. Cell Phone %" << endl;
  28. cout << "% 3. Student Loans %" << endl;
  29. cout << "% 4. Rent %" << endl;
  30. cout << "% 5. Others %" << endl;
  31. cout << "%%%%%%%%%%%%%%%%%%%%%" << endl;
  32. cout << "Please enter the number for the option you wish to modify" << endl;
  33. cin >> fixed_expenses_choice;
  34. if (fixed_expenses_choice == 1 )
  35. {
  36. cout << "Enter Car Insurance Expense ammount" << endl;
  37. cin >> car_insurance_ammount;
  38. fixed_expenses << KeywordCarInsurance << car_insurance_ammount << endl;
  39. }
  40. else if (fixed_expenses_choice == 2)
  41. {
  42. cout << "Enter Cell Phone Expenses ammount" << endl;
  43. cin >> cell_phone_charges_ammount;
  44. fixed_expenses << KeywordCellPhoneChargesAmmount << cell_phone_charges_ammount << endl;
  45. }
  46. else if (fixed_expenses_choice == 3)
  47. {
  48. cout << "Enter Student Loans ammount" << endl;
  49. cin >> student_loan_ammount;
  50. fixed_expenses << KeywordStudentLoanAmmount << student_loan_ammount << endl;
  51. }
  52. else if (fixed_expenses_choice == 4)
  53. {
  54. cout << "Enter Rent ammount" << endl;
  55. cin >> rent_ammount;
  56. fixed_expenses << KeywordRentAmmount << rent_ammount << endl;
  57. }
  58. else if (fixed_expenses_choice ==5)
  59. {
  60. cout << "Enter ammount of the other expenses: " << endl;
  61. cin >> others_ammount;
  62. fixed_expenses << KeywordOthersAmmount << others_ammount << endl;
  63. }
  64. bool tcp(true);
  65. while ( tcp )
  66. {
  67. char ans2;
  68.  
  69. cout << "do you wish to go back to main?"
  70. "(y for yes and n for no)"
  71. << endl;
  72.  
  73. cin >> ans2;
  74.  
  75. switch (ans2)
  76. {
  77. case 'y':
  78. case 'Y':
  79. mainmenu(); // return to caller
  80. break;
  81.  
  82. case 'n':
  83. case 'N':
  84. tcp = false; // input OK, set tcp false
  85. // to quit while loop
  86. break; // break now required !!!
  87.  
  88. default:
  89. cout << "\nPlease enter y or n\n";
  90. break;// not required but add it anyway!
  91. } // end of switch
  92. } // end of while ( tcp )
  93.  
  94. } // end for-ever for loop
  95.  
  96.  
  97.  
  98. };




now, the variable expenses menu...

C++ Syntax (Toggle Plain Text)
  1. void variable_expenses_menu ()
  2. {
  3. for (;;)
  4. {
  5. system ("cls");
  6. int variable_expenses_choice;
  7.  
  8. double food_expenses_ammount;
  9. double book_expenses_ammount;
  10. double variable_others_ammount;
  11.  
  12. const string FoodExpensesAmmount = "Food Expenses Ammount: ";
  13. const string BookExpensesAmmount = "Book Expenses Ammount: ";
  14. const string VariableOthersAmmount = "Others Ammount: ";
  15.  
  16. ofstream variable_expenses;
  17. variable_expenses.open ("Variable Expenses.txt", ios::in);
  18.  
  19. cout << "You have chosen to update Variable Expenses" << endl;
  20. cout << "Which Variable Expense would you like to add?" << endl;
  21. cout << "##############" << endl;
  22. cout << "# 1. Food #" << endl;
  23. cout << "# 2. Books #" << endl;
  24. cout << "# 3. Others #" << endl;
  25. cout << "##############" << endl;
  26. cout << "Please enter the number for the option you wish to modify" << endl;
  27. cin >> variable_expenses_choice;
  28. if (variable_expenses_choice ==1)
  29. {
  30.  
  31. cout << "Enter Food Expenses Ammount: " << endl;
  32. cin >> food_expenses_ammount;
  33. variable_expenses << FoodExpensesAmmount << food_expenses_ammount << endl;
  34. }
  35.  
  36.  
  37. else if (variable_expenses_choice ==2)
  38. {
  39. cout << "Enter Book Expenses Ammount: " << endl;
  40. cin >> book_expenses_ammount;
  41. variable_expenses << BookExpensesAmmount << book_expenses_ammount << endl;
  42. }
  43.  
  44.  
  45. else if (variable_expenses_choice ==3)
  46. {
  47. cout << "Enter ammount of other expenses: " << endl;
  48. cin >> variable_others_ammount;
  49. variable_expenses << VariableOthersAmmount << variable_others_ammount << endl;
  50.  
  51. }
  52.  
  53.  
  54. bool tcp(true);
  55. while ( tcp )
  56. {
  57. char ans3;
  58.  
  59. cout << "do you wish to go back to main?"
  60. "(y for yes and n for no)"
  61. << endl;
  62.  
  63. cin >> ans3;
  64.  
  65. switch (ans3)
  66. {
  67. case 'y':
  68. case 'Y':
  69. mainmenu(); // return to caller
  70. break;
  71.  
  72. case 'n':
  73. case 'N':
  74. tcp = false; // input OK, set tcp false
  75. // to quit while loop
  76. break; // break now required !!!
  77.  
  78. default:
  79. cout << "\nPlease enter y or n\n";
  80. break;// not required but add it anyway!
  81. } // end of switch
  82. } // end of while ( tcp )
  83.  
  84. } // end for-ever for loop
  85.  
  86. };



Also, I plan to add in a function that lets me calculate total values. For example, its menu should be something like:
1. total income
2. total expenses (fixed)
3. total expenses (variable)
4. total expenses (fixed and variable)
5. entire financial report
etc.

so could you please give me tips on how I can go about writing that function, as well as getting information from a file (and sending it to the screen?)

Finally, I know I am new to C++ so please don't scold me for my rather lame coding. I'm just trying my best. I know some of you can solve this in less than 30 minutes, but its been over a week since I last asked help for this and I worked on this code every single night of the week. Everything I've written down here wasn't really taught to me, most of the things I'd to learn myself. So please, I know you're all pros. Just try not to scold me. Also, I'm not expecting you to give me the code for ALL the functions. I think even if you told me how to fix one up I could easily apply it to the others. Same with the total functions. You could just give me good ol' psuedo code and I would try to work it out from there. Also, I'll be posting this same question on another forum. If you answer it here (or there) then you need not re-write the answer in the other forum, I'd be checking for replies myself.

I would like to thank you all in advance for reading this and providing any help to me.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ice_tea_lemon is offline Offline
7 posts
since Mar 2007
Nov 29th, 2007
0

Re: Making my void functions look at little better

> mainmenu(); // return to caller
No it doesn't, it calls it once more.

So eventually, you end up with
mainmenu calls income_menu calls mainmenu calls income_menu calls mainmenu calls income_menu calls and so on.
Keep going long enough, and you'd run out of stack.

If you want to get back, it's just return;
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: selamat pagi semua ^^ ...question about stream
Next Thread in C++ Forum Timeline: Re: please!!!!!!c++ projects for 12th [moved from hijacked attempt]





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC