new one

This is a discussion on new one within the C++ Programming forums, part of the General Programming Boards category; I am so new at function I am having a great deal of trouble I can write the program and ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    61

    Cool new one

    I am so new at function I am having a great deal of trouble I can write the program and it comes out exactly as it is supposed to so how can I turn this into a function

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cmath>
    
    using namespace std;
     
    
    
    int main( )
    {
    
    const float yearly_int_rate = 0.053;
    const float quarterly_int_rate = (0.053/4.0);
    double balance;
    double new_balance;
    double interest_earned;
    int quarter;
    double first_qt_interest;
    double second_qt_interest;
    double third_qt_interest;
    double fourth_qt_interest;
    double first_qt_balance;
    double second_qt_balance;
    double third_qt_balance;
    double fourth_qt_balance;
    
    interest_earned = 0;
    new_balance = 0;
    balance = 0;
    quarter = 0;
    first_qt_interest = 0;
    second_qt_interest = 0;
    third_qt_interest = 0;
    fourth_qt_interest = 0;
    first_qt_balance = 0;
    second_qt_balance = 0;
    third_qt_balance = 0;
    fourth_qt_balance = 0;
    
    
    cout << "Enter the account balance and press the Enter key ./n " << endl;
    cin >> balance; 
    
    first_qt_interest = balance * quarterly_int_rate;
    first_qt_balance = balance + first_qt_interest;
    second_qt_interest = first_qt_balance * quarterly_int_rate;
    second_qt_balance = first_qt_balance + second_qt_interest;
    third_qt_interest = second_qt_balance * quarterly_int_rate;
    third_qt_balance = second_qt_balance + third_qt_interest;
    fourth_qt_interest = third_qt_balance * quarterly_int_rate;
    fourth_qt_balance = third_qt_balance + fourth_qt_interest;
    
    
    cout << fixed << showpoint;
    cout << setprecision(2);
     
    cout << "Quarter      "  << "Balance     " << "Interest Earned   " << 
         "New Balance    " << endl;
    cout << setw(4) << "1" << setw(9) << "$" << balance << setw(9) << "$" 
         << first_qt_interest << setw(10) << "$" << first_qt_balance << endl;
    cout << setw(4) << "2" << setw(9) << "$" << first_qt_balance << setw (9) << "$" 
         << second_qt_interest << setw(10) << "$" << second_qt_balance << endl;
    cout << setw(4) << "3" << setw(9) << "$" << second_qt_balance << setw(9) << "$" 
         << third_qt_interest << setw(10) << "$" << third_qt_balance << endl;
    cout << setw(4) << "4" << setw(9) << "$" <<third_qt_balance << setw(9)<< "$" 
         << fourth_qt_interest << setw(10) << "$" << fourth_qt_balance << endl;
    
    
    		system("pause");
    	
    	return 0;
    }
    Last edited by Salem; 06-12-2007 at 01:43 PM. Reason: [CODE] at the beginning, [/CODE] at the end.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code tags, man.... Code tags. Put them around the code.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    And use punctuation.
    It. Works.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    I did put the code tags

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    I thought "answers" weregiven not smart remarks to intimidate people I am doing my own homework for whomever keeps tagging things as such---if someone cannot help say so do not put smart .......... remarks

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    First of all, you screwed up the code tags, so you didn't do it right.

    Secondly, as I pointed out already in the other topic, the picture I have is part of a signature. Do you think so highly of yourself that you think everything is directed at you? lol...

    Edit: Then again, maybe it just hits a nerve and it should be directed at you.
    Last edited by MacGyver; 06-12-2007 at 02:04 PM.

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    Ok. You have too many variables.

    If you want to "make it a function" you have to define what you want this function to do.
    In math, functions have inputs and some known format of output. In programming, there doesn't have to be any inputs, and the function might DO something but not EVALUATE to anything. Still, the concept is similar. For this program, I'd make some function that takes a starting balance,a quarterly rate, and a number representing which quarter to evaluate to, and then "returns" the adjusted balance.

    Ok. It "returns" the new balance. What kind of data is that? Double. The function is a double. Let's call it qtr_calc:
    Code:
    double qtr_calc/*.....*/;
    Now let's make a function. What inputs (arguments) will it accept? I said before: a starting balance, a quarterly rate, and a quarter. The starting balance will be of the same type as the other balances, thus a double. The rates you make floats (although your constants were doubles), but I'll use a double instead. The quarter could be any number, but since this interest is compunded quarterly, saying the 1.7th quarter will yield the same result as the 1st quarter. So let's use an int.
    Code:
    double qtr_calc(double start_bal, double qtr_rate, int last_qtr);
    Hey, look! A logical funciton prototype. Now for the code.
    Code:
    double qtr_calc(double start_bal, double qtr_rate, int last_qtr)
    {
       qtr_rate += 1.0;  //might not need this, depending on usage.
    
       if(last_qtr == 0 && qtr_rate == 0.0)  return start_bal;  // prevent pow(0,0)
    
       else if(last_qtr > 4)  std::cout << "Quarterly earnings calculated beyond one year." << std::endl;
    
       return std::pow(qtr_rate,last_qtr) * start_bal;
    }
    Then you could:
    Code:
    //. . .  somewhere else, maybe in main() . . . 
    double principle, rate = .053/4.0;
    std::cin >> principle;
    std::cout << "After the third quarter, the balance is $" << std::fixed << std::setprecision(2) << qtr_calc(principle, rate, 3) << std::endl;
    //Note: principle and rate are unchanged by the function.
    And I think that oughta do it.
    Last edited by CodeMonkey; 06-12-2007 at 02:38 PM. Reason: Removing second to last line. Unnecessary.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > I did put the code tags
    But you didn't do it right, which you would have seen had you pressed "go advanced" and then "preview post".

    > Last edited by Salem : Today at 08:43 PM. Reason: [CODE] at the beginning, [/CODE] at the end.
    You have to do more than simply put in [code][/code] at random points in your post just to make the post checker let your post through.

    Oh, and while we're at it, "new one" isn't a useful topic title.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    With the code above from codemonkey (thanks) how can I use this and make it output in table form the first, second, third, and fourth quarter totals

  10. #10
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40

    basic approach

    "newone",

    Your code doesn't seem to really need C++.
    As a first start just implement in C first and then wrap the functions and data at the end.
    aka 'keep it simple' and 'do it in steps'

    You mentioned online course...can you give more particulars?
    Also, what IDE/Brand o'C++ are you using?

    FYI - http://www.cplusplus.com/doc/tutorial/

    Also, below is a standard layout. When code becomes long then the top part is broken-off and place in a header file.

    Code:
    // example: class constructor
    #include <iostream>
    
    //using namespace std;
    using std::cout;
    using std::end1;
    
    namespace anyname
    {                    // the most outer layer of brackets?
    class CRectangle {
        int width, height;
      public:
        CRectangle (int,int);
        int area () {return (width*height);}
    };
    
    CRectangle::CRectangle (int a, int b) {
      width = a;
      height = b;
    }
    
    int main () {
      CRectangle rect (3,4);
      CRectangle rectb (5,6);
      cout << "rect area: " << rect.area() << endl;
      cout << "rectb area: " << rectb.area() << endl;
      return 0;
    }
    } // end of namespace

  11. #11
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    Quote Originally Posted by BJtoVisualcC++ View Post
    With the code above from codemonkey (thanks) how can I use this and make it output in table form the first, second, third, and fourth quarter totals
    I mean no offense when I say this, but if you understood the code you would not have that question.

    Still, no question is a stupid one. Think of qtr_calc(arg,arg,arg) as a value. It's like a variable (and is one, temporarily). So, if you had what I wrote:
    Code:
    double principle, rate = .053/4.0;
    std::cin >> principle;
    std::cout << "After the third quarter, the balance is $" << std::fixed << std::setprecision(2) << qtr_calc(principle, rate, 3) << std::endl;
    How would you expect to print (cout) the other values? You could do this
    Code:
    std::cout << "After the second quarter, the balance is $" << qtr_calc(principle, rate, 2) << std::endl;
    for each remaining quarter. You'll excuse me for being sparse, but I want to make sure that you're not just copying code.

    Because you're working with a function, you can afford to calculate to an arbitrary amount of quarters if you like. Consider this:
    Code:
    double principle, rate = .053/4.0;
    int count;
    std::cout << "What's the original?" << std::endl;
    std::cin >> principle;
    std::cout << "The annual rate is " << (rate*4.0*100.0) << "% compounded quarterly. To which quarter would you like to calculate? (3 being third quarter, 6 being the second quarter of the second year): ";
    std::cin >> count;
    
    std::cout << std::fixed << std::setprecision(2);  //just some formatting stuff, so dollar amounts are displayed rather than huuuuge decimals.
    for(int x = 1; x <= count; ++x)
    {
       std::cout << "Balance after quarter " << x << ": $" << qtr_calc(principle, rate, x) << std::endl;
    }
    Simple stuff. Without running the code, what do you think the output will be?

    PS: Just make sure you get your procedural concepts down ("Do this, then do this") before you wander into the abstractions of OOP.
    Last edited by CodeMonkey; 06-12-2007 at 06:19 PM. Reason: clarity
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  12. #12
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    I tried to reply yesterday but apparently trouble I do not know the output because I have not learned what std:: means

  13. #13
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40

    std and workspace

    "std" is the namespace for the standard library

    http://www.parashift.com/c++-faq-lit....html#faq-27.5

    instead of
    Code:
    namespace std;
    cin << "text" << endl;
    use
    Code:
    // namespace std
    using std::cout;
    using std::endl;
    
    cout << "text" << endl;
    There are other ways but this is how I do it as a beginner.
    Further up ... the tutorial in a previous reply has a namespace example. Your compiler may complicate things more or less.
    Last edited by Dave++; 06-14-2007 at 07:37 PM. Reason: dumb mistake

  14. #14
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    That is, std:: doesn't specify what to do but where to look. If you don't care where cout is (though the computer does), then you can ignore it as you read.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

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