Hey all,
I was wondering if you could help me out with parts of my C++ code that I'm writing. Overview goes something like this: im making a finance recording system. So i have 5 files. one file is to save all the incomes (like a paycheck). another file is for fixed expenses, the third file is for variable expenses, and the fourth file is called total. total does calculations like show total income, total fixed expenses, total variable expenses, etc. the last file (others) is for any special features i would later want to add into my program (like currency conversion calculations)

since total requires calculations, i left it out for now (along with others) and went on to make the first three files: income, fixed expenses, and variable expenses. thing is, thus far, everything in my code is right EXCEPT the part when i have to send in a value to the files (the files create text documents but later on i can't input values to the text documents). here's how it goes in short code:

//include statements, using std, and int main
//declarations of ints and doubles used to get input from user
//file declarations, all three made input files and i leave them open so i can send stuff to them

//basic output, prompting user for what he wants to do. he's given a menu and has to enter the number of choice for an action
//input the user number

//series of if, else if loops. like let's say, if he chose 1, then proceed to option 1 which is enter income amounts.

//show the list of choices he has in #1, like which entry he wants to make: income amount from a paycheck, or his interest

//take in his number of choice

//then, lets say from there he chose 1 (so he's actually on 1.1) where he enters his income from the paycheck

---> here's the error part: i will prompt something like "Enter your paycheck ammount"
---> get the ammount from him
---> file is open, try to send that entry into the file << [CANNOT DO THIS!]

that's the only part of the code that goes wrong. i went to some websites online to learn about files (since im not so pro at stuff like this), i actually learned pretty much everything from there for this source code (if you guys want, i can later on post my website link). the example they've given for inputting data into a file on that site is to enter a string so they use getline (file name, variable) but since im entering data type double and not string i cant use getline but i try to send that value to the file it says the code is wrong.

i can't even fix the code because normally when i have errors i look at the box at the bottom of the screen to see what the problem is but that box just shows a single quotation followed by a double quotation --> ' " so I don't know what it wants me to do.

could you guys please help me figure it out please?

my code thus far is this:

#include <iostream>
#include <fstream>

using namespace std;



int main ()
{
  
  int first_user_input;
  
  int update_income_choice;
  int update_expenses_choice;
  int fixed_expenses_choice;
  int variable_expenses_choice;
  int totals_choice;
  
  double paycheck;
  double interest;
  
  double car_insurance_ammount;
  double cell_phone_charges_ammount;
  double student_loan_ammount;
  double rent_ammount;
  double others_ammount;
  
  double food_expenses_ammount;
  double book_expenses_ammount;
  double variable_others_ammount;
  
  
  ifstream income;
  income.open ("Income.txt", ios::in);
  
  ifstream fixed_expenses;
  fixed_expenses.open ("Fixed Expenses.txt", ios::in);
  
  ifstream variable_expenses;
  variable_expenses.open ("Variable Expenses.txt", ios::in);
  
  
  cout << "Welcome to personal financial record keeping system" << endl;
  cout << "Please enter the number for the option of interest" << endl;
  cout << "1. Update Income" << endl;
  cout << "2. Update Expenses" << endl;
  cout << "3. Calculate totals" << endl;
  cout << "4. Other options" << endl;
  
  cin >> first_user_input;
  
  if (first_user_input = 1)
  {
  cout << "You have chosen to update Income entries" << endl;
  cout << "Please enter the number for the option you wish to modify" << endl;
  cout << "1. Paychecks" << endl;
  cout << "2. Interest" << endl;
  cin >> update_income_choice;
  
      if (update_income_choice = 1)
      {
         cout << "Please enter the ammount (in RM) of the paycheck" << endl;
         cin >> paycheck;
         income >> "Paycheck: " >> paycheck >> endl;
      }
      else if (update_income_choice = 2)
      {
         cout << "Please enter the ammount (in RM) of the Interest" << endl;
         cin >> interest;   
         income >> "Income: " >> interest >> endl;
      }
  }  
  else if (first_user_input = 2)  
  {
  cout << "You have chosen to update Expenses entries" << endl;
  cout << "Please enter the number for the option you wish to modify" << endl;
  cout << "1. Fixed Expenses" << endl;
  cout << "2. Variable Expenses" << endl;  
  cin >> update_expenses_choice;
  
      if (update_expenses_choice = 1)
      {
          cout << "You have chosen to update Fixed Expenses" << endl;
          cout << "Which Fixed Expense would you like to add?" << endl;
          cout << "1. Car Insurance" << endl;
          cout << "2. Cell Phone" << endl;
          cout << "3. Student Loans" << endl;
          cout << "4. Rent" << endl;
          cout << "5. Others" << endl;
          
          cin >> fixed_expenses_choice;
              if (fixed_expenses_choice = 1)
              {
                 cout << "Enter Car Insurance Expense ammount" << endl;
                 cin >> car_insurance_ammount;     
                     
                     
                     fixed_expenses >> "Car Insurance: " >> car_insurance_ammount >> endl;
                    
              }
              else if (fixed_expenses_choice = 2)
              {
                   cout << "Enter Cell Phone Expenses ammount" << endl;
                   cin >> cell_phone_charges_ammount;
                   
                     
                     fixed_expenses >> "Cell Phone: " >> cell_phone_charges_ammount >> endl;
                     
              }
              else if (fixed_expenses_choice = 3)
              {
                   cout << "Enter Student Loans ammount" << endl;
                   cin >> student_loan_ammount;
                   
                          
                     fixed_expenses >> "Student Loan: " >> student_loan_ammount >> endl;
                     
              }
              else if (fixed_expenses_choice = 4)
              {
                   cout << "Enter Rent ammount" << endl;
                   cin >> rent_ammount;
                   
                     
                     fixed_expenses >> "Rent: " >> rent_ammount >> endl;
                     
              }
              else if (fixed_expenses_choice = 5)
              {
                   cout << "Enter ammount of the other expenses: " << endl;
                   cin >> others_ammount;
                   
                     
                     fixed_expenses >> "Others: " >> others_ammount >> endl;
                     
              }
      }
      else if (update_expenses_choice = 2)
      {
          cout << "You have chosen to update Variable Expenses" << endl;
          cout << "Which Variable Expense would you like to add?" << endl;
          cout << "1. Food" << endl;
          cout << "2. Books" << endl;
          cout << "5. Others" << endl;
    
          cin >> variable_expenses_choice;
              if (variable_expenses_choice = 1)
              {
                  cout << "Enter Food Expenses Ammount: " << endl;
                  cin >> food_expenses_ammount;
                  
                     
                     variable_expenses >> "Food: " >> food_expense_ammount >> endl;
                     
              }
              else if (variable_expenses_choice = 2)
              {
                   cout << "Enter Book Expenses Ammount: " << endl;
                   cin >> book_expenses_ammount;
                   
                     
                     variable_expenses >> "Books: " >> book_expenses_ammount >> endl;
                     
              }
              else if (variable_expenses_choice = 3)
              {
                   cout << "Enter ammount of other expenses: " << endl;
                   cin >> variable_others_ammount;
                   
                     
                     variable_expenses >> "Others: " >> variable_others_ammount >> endl;
                     
              }
      }
   else if (first_user_input = 3)
   {
   cout << "You have chosen to Calculate Totals" << endl;
   cout << "Please enter the number for the option you wish to view" << endl;
   cout << "1. Total Income" << endl;
   cout << "2. Total Fixed Expenses" << endl;
   cout << "3. Total Vairable Expenses" << endl;
   cout << "4. Total Expenses" << endl;
   cout << "5. Disposable Income" << endl;
   cout << "6. Entire financial report" << endl;
   
   cin >> totals_choice;
         
    system ("pause");
    return 0;
}

You're trying to swim against the (file)stream.

ifstream income;
  income.open ("Income.txt", ios::in);
  
  ifstream fixed_expenses;
  fixed_expenses.open ("Fixed Expenses.txt", ios::in);
  
  ifstream variable_expenses;
  variable_expenses.open ("Variable Expenses.txt", ios::in);

You are opening file handles of type ifstream, which are by default input handles. So, you don't need the "ios::in" mode parameters.

But, you seem to be trying to use them as output (writing) file handles. Perhaps what you really meant to do this in your declarations is:

ofstream income;
  income.open ("Income.txt" );
  
  ofstream fixed_expenses;
  fixed_expenses.open ("Fixed Expenses.txt" );
  
  ostream variable_expenses;
  variable_expenses.open ("Variable Expenses.txt" );

//  then later in the code

       income << "Paycheck: " << paycheck <<  endl;
       //etc.

ofstream handles are, by default, output handles, so "ios::out" is not necessary.

Val

Pretty much everywhere, you have
if ( var = value )
where you should have
if ( var == value )

This article has been dead for over six months. Start a new discussion instead.