Problem passing values

This is a discussion on Problem passing values within the C++ Programming forums, part of the General Programming Boards category; I apologize if this is a dumb problem, but I really need help. I have most of my program worked ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    Problem passing values

    I apologize if this is a dumb problem, but I really need help.

    I have most of my program worked out, but I am having a hard time getting it to do what I want it to do. The idea is that in main, I call a function that opens an external file, allowing the user to enter the name, then passes the file to another function which is supposed to create and save data; it has to write columns, then call a function to calculate some info (this function then calls two other functions which each calculate a separate part of data this is to be printed to the file), then call a program to write the data to a file. I was doing OK until I got to the write part. I have worked on this all morning until I can't see straight, and I am totally lost now. The program compiles, but other than the header and column names, nothing is printed to the output file. Here is what I have put together so far, can someone please help me understand where I am going wrong and how to fix this?
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cmath>
    #include <cstdlib>     
    #include <fstream>     
    #include <iostream>
    
    using namespace std;
    
    int openOutputFile(ofstream& o);
    void writeHeaderLinesToOutputFile(ofstream& o);
    int createAndSaveLoanData(ofstream& o);
    double calculateOneLineOfLoanResults(double r, double m);
    double calculatemonthlyPayment(double r, double m);
    double calculateTotalPayment(double& mPmt, double m);
    int writeLineOfResultsToOutputFile(ofstream& o, double r, double m, double& mp, double& tp);
    
    int main()
    {
       ofstream outFile;    
        
       
       openOutputFile(outFile);
       
        
        
        
        cin.get();
        return 0;
    }
    int openOutputFile(ofstream& outFile)
    {
        char outputName[50];
        
        cout << "Please type the name of the output file, including the extension: ";
        cin >> outputName;
        
        outFile.open(outputName);
            if (outFile.fail())
            {
                cerr << "Could not open " << outputName << "!" << endl;
                return EXIT_FAILURE;
            }
         createAndSaveLoanData(outFile);  
      
        return 0;
    }
    int createAndSaveLoanData(ofstream& outFile)
    {
        double monthlyPmt, total;
        writeHeaderLinesToOutputFile(outFile);
        for (double r = .04; r < .0725 ; r + .0025)
        {
            for (double m = 240; m < 370; m + 60)
            {
                calculateOneLineOfLoanResults(r, m);
                writeLineOfResultsToOutputFile(outFile, r, m, monthlyPmt, total);                       
            }
        }    
                
        return 0;   
    }
    void writeHeaderLinesToOutputFile(ofstream& outFile)
    {    
              outFile << "Loan Amount: $1000" << '\n' <<'\n'; 
              outFile << left << setw(12) << "Interest" << left << setw(12) << "Duration" << left << setw(12)  << "Monthly"; 
              outFile << left << setw(12) << "Total" << '\n';
              outFile << left << setw(12)  << "rate" << left << setw(12) << "(years)" << left << setw(12) << "payment";
              outFile << left << setw(12) << "payment" << '\n';
              outFile.close();
    }
    
    
    double calculateOneLineOfLoanResults(double rate, double months)
    {
           double monthlyPmt, total;
           
           monthlyPmt = calculatemonthlyPayment(rate, months);
           total = calculateTotalPayment(monthlyPmt, months);
                 
    }
    
    double calculatemonthlyPayment(double rate, double months)
    {
           double  ratem, expm, monthlyPmt;
           double loan = 1000;
           double e_to_the_m;
                         
           ratem = rate / 1200;
           expm = (1.0 + ratem);
           e_to_the_m = pow(expm, months);            
           monthlyPmt = (ratem * e_to_the_m * loan) / e_to_the_m - 1.0;
           
           return monthlyPmt;
    }
    
    double calculateTotalPayment(double& monthlyPmt, double months)
    {
           double total;
           
           total = monthlyPmt * months;
           
           return total;
    }
           
    int writeLineOfResultsToOutputFile(ofstream& outFile, double rate, double months, double& monthlyPmt, double& total )
    {    
              
              outFile << left << setw(12) << rate << left << setw(12) << (months / 12) << left << setw(12)  << monthlyPmt; 
              outFile << left << setw(12) << total << '\n';
              outFile.close();
              
              return 0;
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    this is an infinite loop
    Code:
            for (double m = 240; m < 370; m + 60)
    try
    Code:
            for (double m = 240; m < 370; m += 60)
    But then I wouldn't use doubles in for loops
    Kurt

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Of course the header is the only thing that gets written, you close the file immediately after writing the header.
    Code:
    void writeHeaderLinesToOutputFile(ofstream& outFile)
    {    
        outFile << "Loan Amount: $1000" << '\n' <<'\n'; 
        outFile << left << setw(12) << "Interest" << left << setw(12) << "Duration" << left << setw(12)  << "Monthly"; 
        outFile << left << setw(12) << "Total" << '\n';
        outFile << left << setw(12)  << "rate" << left << setw(12) << "(years)" << left << setw(12) << "payment";
        outFile << left << setw(12) << "payment" << '\n';
        outFile.close();
    }
    
    int writeLineOfResultsToOutputFile(ofstream& outFile, double rate, double months, double& monthlyPmt, double& total )
    {    
              
        outFile << left << setw(12) << rate << left << setw(12) << (months / 12) << left << setw(12)  << monthlyPmt; 
        outFile << left << setw(12) << total << '\n';
        outFile.close();
              
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    after briefly going through your code ive noticed one problem. in function createAndSaveLoanData, you have a line of code that essentially does nothing:
    Code:
    int createAndSaveLoanData(ofstream& outFile)
    {
        double monthlyPmt, total;
        writeHeaderLinesToOutputFile(outFile);
        for (double r = .04; r < .0725 ; r + .0025)
        {
            for (double m = 240; m < 370; m + 60)
            {
                calculateOneLineOfLoanResults(r, m);
                writeLineOfResultsToOutputFile(outFile, r, m, monthlyPmt, total);                       
            }
        }    
                
        return 0;   
    }
    the line in bold is what i am referring to. you call the function calculateOneLineOfLoanResults and pass two parameters, then in that function you do some calculations but nothing is done with the resulting value. you have it declared to return a double but arent returning anything. i assume you want to return an answer and store it in the calling function createAndSaveLoanData.

    hope it helps

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Quote Originally Posted by nadroj View Post
    after briefly going through your code ive noticed one problem. in function createAndSaveLoanData, you have a line of code that essentially does nothing:
    Code:
    int createAndSaveLoanData(ofstream& outFile)
    {
        double monthlyPmt, total;
        writeHeaderLinesToOutputFile(outFile);
        for (double r = .04; r < .0725 ; r + .0025)
        {
            for (double m = 240; m < 370; m + 60)
            {
                calculateOneLineOfLoanResults(r, m);
                writeLineOfResultsToOutputFile(outFile, r, m, monthlyPmt, total);                       
            }
        }    
                
        return 0;   
    }
    the line in bold is what i am referring to. you call the function calculateOneLineOfLoanResults and pass two parameters, then in that function you do some calculations but nothing is done with the resulting value. you have it declared to return a double but arent returning anything. i assume you want to return an answer and store it in the calling function createAndSaveLoanData.

    hope it helps
    I do want to return an answer but the problem is that I actually have two answers that I need to pass to the calling function. calculateOneLineOfLoanResults calls two other functions, one which calculates what the monthly payment will be, one which calculates what the total payment is going to be. I need both of those values to be passed to writeLineOfResultsToOutputFile, which has to print the rate, the duration, the monthly payment and the total payment to a file. I made some modifications to my code, as per the other posts, but now I have another problem; now the output file is not getting created at all.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cmath>
    #include <cstdlib>     
    #include <fstream>     
    #include <iostream>
    
    using namespace std;
    
    int openOutputFile(ofstream& o);
    void writeHeaderLinesToOutputFile(ofstream& o);
    int createAndSaveLoanData(ofstream& o);
    double calculateOneLineOfLoanResults(double r, double m);
    double calculatemonthlyPayment(double r, double m);
    double calculateTotalPayment(double& mPmt, double m);
    int writeLineOfResultsToOutputFile(ofstream& o, double r, double m, double& mp, double& tp);
    
    int main()
    {
       ofstream outFile;  
       
       openOutputFile(outFile);
       
        cin.get();
        return 0;
    }
    int openOutputFile(ofstream& outFile)
    {
        char outputName[50];
        
        cout << "Please type the name of the output file, including the extension: ";
        cin >> outputName;
        
        outFile.open(outputName);
            if (outFile.fail())
            {
                cerr << "Could not open " << outputName << "!" << endl;
                return EXIT_FAILURE;
            }
         createAndSaveLoanData(outFile);  
      
        return 0;
    }
    int createAndSaveLoanData(ofstream& outFile)
    {
        double monthlyPmt, total;
        writeHeaderLinesToOutputFile(outFile);
        for (double r = .04; r < .0725 ; r = r + .0025)
        {
            for (double m = 240; m < 370; m = m + 60)
            {
                calculateOneLineOfLoanResults(r, m);
                writeLineOfResultsToOutputFile(outFile, r, m, monthlyPmt, total);                       
            }
        }    
                
        return 0;   
    }
    void writeHeaderLinesToOutputFile(ofstream& outFile)
    {    
              outFile << "Loan Amount: $1000" << '\n' <<'\n'; 
              outFile << left << setw(12) << "Interest" << left << setw(12) << "Duration" << left << setw(12)  << "Monthly"; 
              outFile << left << setw(12) << "Total" << '\n';
              outFile << left << setw(12)  << "rate" << left << setw(12) << "(years)" << left << setw(12) << "payment";
              outFile << left << setw(12) << "payment" << '\n';
              
    }
    
    
    double calculateOneLineOfLoanResults(double rate, double months)
    {
           double monthlyPmt, total;
           
           monthlyPmt = calculatemonthlyPayment(rate, months);
           total = calculateTotalPayment(monthlyPmt, months);
    }
    
    double calculatemonthlyPayment(double rate, double months)
    {
           double  ratem, expm, monthlyPmt;
           double loan = 1000;
           double e_to_the_m;
                         
           ratem = rate / 1200;
           expm = (1.0 + ratem);
           e_to_the_m = pow(expm, months);            
           monthlyPmt = (ratem * e_to_the_m * loan) / e_to_the_m - 1.0;
           
           return monthlyPmt;
    }
    
    double calculateTotalPayment(double& monthlyPmt, double months)
    {
           double total;
           total = monthlyPmt * months;
           
           return total;
    }
           
    int writeLineOfResultsToOutputFile(ofstream& outFile, double rate, double months, double& monthlyPmt, double& total )
    {    
              
              outFile << left << setw(12) << rate << left << setw(12) << (months / 12) << left << setw(12)  << monthlyPmt; 
              outFile << left << setw(12) << total << '\n';
              outFile.close();
              
              return 0;
    }

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    how can you even compile it to see the output if you dont even have a return for the function that is declared to return something? i thought that was an error, or is it implicit?

    edit:
    regarding the calculateOneLineOfLoanResults, i dont know what you want to return. if you need JUST 'total', then return that. if you need BOTH 'monthlyPmt' and 'total', then change the function to be:
    Code:
    double calculateOneLineOfLoanResults(double, double , double&, double&)
    then in 'createAndSaveLoanData' create two more local variables, which are passed as the 3rd and 4th parameters to calculateOneLineOfLoanResults, and store your calculations in there, which modify the variables in createAndSaveLoanData.
    Last edited by nadroj; 09-07-2007 at 11:10 AM.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Quote Originally Posted by nadroj View Post
    how can you even compile it to see the output if you dont even have a return for the function that is declared to return something? i thought that was an error, or is it implicit?
    I think maybe my compiler inserted a return statement.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok. but who knows what returned double it gives you? (well i dont). the point is, you should fix that first, as well as the other errors mentioned by members above, before continuing.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    OK, I think I have fixed the issues but now the file is not being created.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cmath>
    #include <cstdlib>     
    #include <fstream>     
    #include <iostream>
    
    using namespace std;
    
    int openOutputFile(ofstream& o);
    void writeHeaderLinesToOutputFile(ofstream& o);
    int createAndSaveLoanData(ofstream& o);
    void calculateOneLineOfLoanResults(double r, double m, double& mp, double& tp);
    double calculatemonthlyPayment(double r, double m);
    double calculateTotalPayment(double& mPmt, double m);
    int writeLineOfResultsToOutputFile(ofstream& o, double r, double m, double& mp, double& tp);
    
    int main()
    {
       ofstream outFile;  
       
       openOutputFile(outFile);
       
        cin.get();
        return 0;
    }
    int openOutputFile(ofstream& outFile)
    {
        char outputName[50];
        
        cout << "Please type the name of the output file, including the extension: ";
        cin >> outputName;
        
        outFile.open(outputName);
            if (outFile.fail())
            {
                cerr << "Could not open " << outputName << "!" << endl;
                return EXIT_FAILURE;
            }
         createAndSaveLoanData(outFile);  
      
        return 0;
    }
    int createAndSaveLoanData(ofstream& outFile)
    {
        double monthlyPmt, total;
        writeHeaderLinesToOutputFile(outFile);
        for (double r = .04; r < .0725 ; r = r + .0025)
        {
            for (double m = 240; m < 370; m = m + 60)
            {
                calculateOneLineOfLoanResults(r, m, monthlyPmt, total);
                writeLineOfResultsToOutputFile(outFile, r, m, monthlyPmt, total);                       
            }
        }    
                
        return 0;   
    }
    void writeHeaderLinesToOutputFile(ofstream& outFile)
    {    
              outFile << "Loan Amount: $1000" << '\n' <<'\n'; 
              outFile << left << setw(12) << "Interest" << left << setw(12) << "Duration" << left << setw(12)  << "Monthly"; 
              outFile << left << setw(12) << "Total" << '\n';
              outFile << left << setw(12)  << "rate" << left << setw(12) << "(years)" << left << setw(12) << "payment";
              outFile << left << setw(12) << "payment" << '\n';
              
    }
    
    
    void calculateOneLineOfLoanResults(double rate, double months , double& monthlyPmt, double& total)
    {
            
           monthlyPmt = calculatemonthlyPayment(rate, months);
           total = calculateTotalPayment(monthlyPmt, months);
    }
    
    double calculatemonthlyPayment(double rate, double months)
    {
           double  ratem, expm, monthlyPmt;
           double loan = 1000;
           double e_to_the_m;
                         
           ratem = rate / 1200;
           expm = (1.0 + ratem);
           e_to_the_m = pow(expm, months);            
           monthlyPmt = (ratem * e_to_the_m * loan) / e_to_the_m - 1.0;
           
           return monthlyPmt;
    }
    
    double calculateTotalPayment(double& monthlyPmt, double months)
    {
           double total;
           total = monthlyPmt * months;
           
           return total;
    }
           
    int writeLineOfResultsToOutputFile(ofstream& outFile, double rate, double months, double& monthlyPmt, double& total )
    {    
              
              outFile << left << setw(12) << rate << left << setw(12) << (months / 12) << left << setw(12)  << monthlyPmt; 
              outFile << left << setw(12) << total << '\n';
              outFile.close();
              
              return 0;
    }

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    You're still doing it...
    Code:
    int writeLineOfResultsToOutputFile(ofstream& outFile, double rate, double months, double& monthlyPmt, double& total )
    {    
              
        outFile << left << setw(12) << rate << left << setw(12) << (months / 12) << left << setw(12)  << monthlyPmt; 
        outFile << left << setw(12) << total << '\n';
        outFile.close();
              
        return 0;
    }
    ... after the first time you call the function in the loop, the file closes and subsequent lines of output would not be present in the file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The behaviour of compilers in the face of missing return values differs. The C++ standard says it is undefined behaviour if a function with a return type other than void drops off the end of the function (i.e. doesn't execute a return statement first). It also says that a return statement with an expression that is not compatible with the return type (in particular a return; where the type is not void) is an error.

    The underlying problem is this: suppose you have a function with a complicated return type (say, a class type that requires complicated and valid constructor arguments). This function has various valid control paths. One or more of them throw an exception instead of returning. Then you don't want to still have to write the return statement, since you probably don't have valid parameters for the constructor. By not caring about a return statement, C++ takes the simplest way of allowing this.
    There are two alternatives. One is to simply require a return statement, no matter what. That's stupid.
    The other is to require the compiler to check whether there is a way in which the function can end without executing a return statement. This requires the compiler to have at least very basic flow analysis. Java and C# do this. Their language specifications specify exactly under which circumstances a return statement is required.
    The problem is, the thing falls short. Java specifies that the only way of aborting early is by using a throw. This does not account for:
    System.exit(), which doesn't return.
    Non-local throws, i.e. a function which always throws.
    In both cases, you must have a return statement anyway.

    In C++, on the other hand, code that apparently has no return statement is allowed to compile - the language trusts the programmer to know what he's doing. Compilers that do have sufficient flow analysis can emit warnings when they're sure that the programmer made a mistake.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    thanks everyone for all the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with sentinel values
    By Golfduffer in forum C Programming
    Replies: 3
    Last Post: 08-30-2007, 01:19 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  4. passing values
    By laasunde in forum C Programming
    Replies: 3
    Last Post: 11-22-2002, 02:50 PM
  5. Replies: 2
    Last Post: 11-22-2001, 11:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21