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.

void income_menu ()
{
    
for (;;)  // C/C++ idiom for (loop) forever
       {
  system ("cls");
  int update_income_choice;
  
  const string KeywordIncomePaycheck = "Paycheck: ";
  const string KeywordIncomeInterest = "Income: ";
  double   paycheck;
  double   interest;
  ofstream income;
  income.open ("Income.txt", ios::in);
   
  cout << "You have chosen to update Income entries" << endl;
  cout << "Please enter the number for the option you wish to modify" << endl;
  cout << "*******************" << endl;
  cout << "*  1. Paychecks   *" << endl;
  cout << "*  2. Interest    *" << endl;
  cout << "*******************" << endl;
  cin >> update_income_choice;

      if (update_income_choice == 1)
      {
         cout << "Please enter the ammount (in RM) of the paycheck" << endl;
         cin >> paycheck;
         income << KeywordIncomePaycheck << paycheck << endl;
      }
      else if (update_income_choice == 2)
      { 
         cout << "Please enter the ammount (in RM) of the Interest" << endl;
         cin >> interest;   
         income << KeywordIncomeInterest << interest << endl;
      }
      
      bool tcp(true);
           while ( tcp )
           {
               char ans1;  

               cout << "do you wish to go back to main?"
                       "(y for yes and n for no)"
                    << endl;

               cin >> ans1;

               switch (ans1)
               {
               case 'y':
               case 'Y':
                   mainmenu();  // return to caller
                   break;

               case 'n':
               case 'N':
                   tcp = false; // input OK, set tcp false
                                // to quit while loop
                   break;   // break now required !!!

               default:
                   cout << "\nPlease enter y or n\n";
                   break;// not required but add it anyway!
               } // end of switch
           } // end of while ( tcp )

       } // end for-ever for loop
   }; // 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.

void fixed_expenses_menu ()
{
     for (;;)
     {
       system ("cls");
       int fixed_expenses_choice;
       
       double car_insurance_ammount;
       double cell_phone_charges_ammount;
       double student_loan_ammount;
       double rent_ammount;
       double others_ammount;
       
       const string KeywordCarInsurance = "Car Insurance: "; 
       const string KeywordCellPhoneChargesAmmount = "Cell Phone Charges Ammount: ";
       const string KeywordStudentLoanAmmount = "Student Loan: ";
       const string KeywordRentAmmount = "Rent Ammount: ";
       const string KeywordOthersAmmount = "Others: ";
       
       ofstream fixed_expenses;
       fixed_expenses.open ("Fixed Expenses.txt", ios::in);
       
          cout << "You have chosen to update Fixed Expenses" << endl;
          cout << "Which Fixed Expense would you like to add?" << endl;
          cout << "%%%%%%%%%%%%%%%%%%%%%" << endl;
          cout << "%  1. Car Insurance %" << endl;
          cout << "%  2. Cell Phone    %" << endl;
          cout << "%  3. Student Loans %" << endl;
          cout << "%  4. Rent          %" << endl;
          cout << "%  5. Others        %" << endl;
          cout << "%%%%%%%%%%%%%%%%%%%%%" << endl;
          cout << "Please enter the number for the option you wish to modify" << endl;
          cin >>  fixed_expenses_choice;
              if (fixed_expenses_choice == 1 )
              {
                 cout << "Enter Car Insurance Expense ammount" << endl;
                  cin >> car_insurance_ammount;         
                  fixed_expenses << KeywordCarInsurance << car_insurance_ammount << endl;
              }   
              else if (fixed_expenses_choice == 2)
              { 
                   cout << "Enter Cell Phone Expenses ammount" << endl;
                   cin >> cell_phone_charges_ammount;
                   fixed_expenses << KeywordCellPhoneChargesAmmount << cell_phone_charges_ammount << endl;       
              }
              else if (fixed_expenses_choice == 3)
              {
                   cout << "Enter Student Loans ammount" << endl;
                   cin >> student_loan_ammount;
                   fixed_expenses << KeywordStudentLoanAmmount << student_loan_ammount << endl;
              }
              else if (fixed_expenses_choice == 4)
              {
                   cout << "Enter Rent ammount" << endl;
                   cin >> rent_ammount;
                   fixed_expenses << KeywordRentAmmount << rent_ammount << endl;
              }
              else if (fixed_expenses_choice ==5)
              {
                   cout << "Enter ammount of the other expenses: " << endl;
                   cin >> others_ammount;
                   fixed_expenses << KeywordOthersAmmount << others_ammount << endl;
              }
     bool tcp(true);
           while ( tcp )
           {
               char ans2;  

               cout << "do you wish to go back to main?"
                       "(y for yes and n for no)"
                    << endl;

               cin >> ans2;

               switch (ans2)
               {
               case 'y':
               case 'Y':
                   mainmenu();  // return to caller
                   break;

               case 'n':
               case 'N':
                   tcp = false; // input OK, set tcp false
                                // to quit while loop
                   break;   // break now required !!!

               default:
                   cout << "\nPlease enter y or n\n";
                   break;// not required but add it anyway!
               } // end of switch
           } // end of while ( tcp )

       } // end for-ever for loop
   
     
     
};

now, the variable expenses menu...

void variable_expenses_menu ()
{     
  for (;;)
     {
     system ("cls");
     int variable_expenses_choice;
     
     double food_expenses_ammount;
     double book_expenses_ammount;
     double variable_others_ammount; 
     
     const string FoodExpensesAmmount = "Food Expenses Ammount: ";
     const string BookExpensesAmmount = "Book Expenses Ammount: ";
     const string VariableOthersAmmount = "Others Ammount: "; 
      
      ofstream variable_expenses;
      variable_expenses.open ("Variable Expenses.txt", ios::in);
      
          cout << "You have chosen to update Variable Expenses" << endl;
          cout << "Which Variable Expense would you like to add?" << endl;
          cout << "##############" << endl;
          cout << "# 1. Food    #" << endl;
          cout << "# 2. Books   #" << endl;
          cout << "# 3. Others  #" << endl;
          cout << "##############" << endl;
          cout << "Please enter the number for the option you wish to modify" << endl;
          cin >> variable_expenses_choice;
          if (variable_expenses_choice ==1)
            {  
              
                  cout << "Enter Food Expenses Ammount: " << endl;
                  cin >> food_expenses_ammount;
                  variable_expenses << FoodExpensesAmmount << food_expenses_ammount << endl;
               }
          
          
          else if (variable_expenses_choice ==2)
              {
                   cout << "Enter Book Expenses Ammount: " << endl;
                   cin >> book_expenses_ammount;
                   variable_expenses << BookExpensesAmmount << book_expenses_ammount << endl;   
              }
          
              
          else if (variable_expenses_choice ==3)
              {
                   cout << "Enter ammount of other expenses: " << endl;
                   cin >> variable_others_ammount;
                   variable_expenses << VariableOthersAmmount << variable_others_ammount << endl;
                    
              }
               
      
    bool tcp(true);
           while ( tcp )
           {
               char ans3;  

               cout << "do you wish to go back to main?"
                       "(y for yes and n for no)"
                    << endl;

               cin >> ans3;

               switch (ans3)
               {
               case 'y':
               case 'Y':
                   mainmenu();  // return to caller
                   break;

               case 'n':
               case 'N':
                   tcp = false; // input OK, set tcp false
                                // to quit while loop
                   break;   // break now required !!!

               default:
                   cout << "\nPlease enter y or n\n";
                   break;// not required but add it anyway!
               } // end of switch
           } // end of while ( tcp )

       } // end for-ever for loop
    
};

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.

> 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;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.