C++ Assignment

124

Comments

  • edited March 2007
    Well not sure how to try it as the code isn't completed yet so I have many errors still. Just need a little more confirmation as to whether the example I posted is correct so I can continue/ finish up the rest and then post up what I have. Along with if I don't use the enum statement, how I would then include the Signs.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    You should never write all your code and then compile. You should be writing small pieces that let you compile to make sure that things are working like you expect.

    So, the first thing to do would just have code that reads in the mm/dd, and says "you entered [mm/dd]".
    Then, you'd get it working for the first sign in january.
    Then, the second january sign
    Then, so it will tell you if you're on the cusp.
    Then, you can model the rest of your code on that first part that you know works.

    Otherwise, you end up with a huge amount of bugs that you have no idea where the real problem is.
  • edited March 2007
    True and I can see I already did my intro part wrong...
    [php] cout >> "Enter your birthday (mm/dd): \n";
    cin << mm/dd;[/php]

    Seems the errors I get are:
    c:\docume~1\chrisl~1\desktop\cs2\ch.3\questi~2.cpp:22: no match for `_IO_ostream_withassign & >> const char[31]'
    c:\docume~1\chrisl~1\desktop\cs2\ch.3\questi~2.cpp:23: no match for `_IO_istream_withassign & << int'

    [php] int mm, dd, ans;[/php]

    That is what I have...
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    What's he's trying to point out and still have you be the one to realize the problem is (speaking English)
    If the answer is the 1st month or greater than or equal to the 20th day.
    Understand? Do you see the logic flaw in that?
    I won't tell you, but I think that is a big hint.

    As for asking for the input.
    [php]
    cout << "Enter your birth month (1 - 12)" << endl;
    cin >> mm;
    cout << "Enter your day of birth" << endl;
    cin >> dd;
    [/php]

    Otherwise, if you want the input to be in mm/dd format, you will have to parse strings, which is probably a bit more advanced than you could handle at this time.
  • edited March 2007
    Nah I understand now, I was thinking of && rather than ||, but would the && work or still no? But I do get it after finding my mistake there.

    EDIT: Got it to work but then when I have it looped to do again it just exits.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    Nah I understand now, I was thinking of && rather than ||, but would the && work or still no? But I do get it after finding my mistake there.

    EDIT: Got it to work but then when I have it looped to do again it just exists.

    your problem is on line 50, plus or minus 50 lines. That's about all I can help you with without seeing your code.
  • edited March 2007
    Alrights it worked for a while but then now it won't execute...

    [php]/*
    __________________________________________________________________
    Programmer : Christopher La
    Project : Ch. 3.3
    Class : CS2
    Due : 3/15/07
    Description : Write an astrology program. Make up a horoscope
    for each sign. Your program should include a loop
    that lets the user repeat this calculation until
    the user says she or he is done.
    ___________________________________________________________________
    */
    #include
    #include

    int main()
    {
    int mm, dd, ans;

    do
    {
    cout << "Enter your birth month (1 - 12): ";
    cin >> mm;
    cout << "Enter your day of birth: ";
    cin >> dd;

    enum Signs
    {
    ARIES, // Aries March 21 - April 19
    TAURUS, // Taurus April 20 - May 20
    GEMINI, // Gemini May 21 - June 21
    CANCER, // Cancer June 22 - July 22
    LEO, // Leo July 23 - August 22
    VIRGO, // Virgo August 23 - Suptember 22
    LIBRA, // Libra September 23 - October 22
    SCORPIO, // Scorpio October 23 - November 21
    SAGITTARIUS, // Sagittariius November 22 - December 21
    CAPRICORN, // Capricorn December 22 - January 10
    AQUARIUS, // Aquarius January 20 - February 18
    PISCES // Pisces February 19 - March 20
    };

    if (mm == 1){
    if (dd >= 20){
    cout << "You are an Aquarius! You will find your destined soulmate.";
    if (dd <= 21){
    cout << "You are on the cusp of being a Capricorn. You ignore what
    /n doesn't interest you.";
    }
    }
    }
    else if (mm == 2){
    if (dd <= 18){
    cout << "You are an Aquarius! You will find your destined soulmate.";
    if (dd >= 19){
    cout << "You are on the cusp of being a Pisces. You will have great
    /n luck. ";
    }
    }
    }
    if (mm == 2){
    if (dd >= 19){
    cout << "You are a Pisces! You will have great luck.";
    if (dd <= 20){
    cout << "You are on the cusp of being a Aquarius. You will find your
    /n destined soulmate.";
    }
    }
    }
    else if (mm == 3){
    if (dd <= 20){
    cout << "You are a Pisces! You will have great luck.";
    if (dd >= 19){
    cout << "You are on the cusp of being a Aries. You will find be
    /n traveling to faraway places.";
    }
    }
    }

    cout << "/nTry again? (y or n): ";
    cin >> ans;
    }
    while (ans == 'Y' || ans == 'y');

    system("PAUSE");
    return 0;
    }[/php]EDIT Nvm, was missing a few semicolons, working now
    </stdlib.h></iostream.h>
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    if you're not using the enum, why is it in there? I'd say that's your major problem.

    Also, keep better track of your indents/curly braces in your multi if-block places. You'll see if you're missing a open/close based on the indents, and then you won't have to track them down at the end.
  • edited March 2007
    I could remove the enum and the program would still be the same right?

    EDIT:

    [php]/*
    __________________________________________________________________
    Programmer : Christopher La
    Project : Ch. 3.3
    Class : CS2
    Due : 3/15/07
    Description : Write an astrology program. Make up a horoscope
    for each sign. Your program should include a loop
    that lets the user repeat this calculation until
    the user says she or he is done.
    ___________________________________________________________________
    */
    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int mm, dd, ans;

    do
    {
    cout << "Enter your birth month (1 - 12): ";
    cin >> mm;
    cout << "Enter your day of birth: ";
    cin >> dd;

    if (mm == 1)
    {
    if (dd >= 20)
    {
    cout << "You are an Aquarius! You will find your destined soulmate."
    << endl;
    if (dd <= 21)
    {
    cout << "You are on the cusp of being a Capricorn. You ignore what
    doesn't interest you." << endl;
    }
    }
    }
    else if (mm == 2)
    {
    if (dd <= 18)
    {
    cout << "You are an Aquarius! You will find your destined soulmate."
    << endl;
    if (dd >= 19)
    {
    cout << "You are on the cusp of being a Pisces. You will have great
    luck. " << endl;
    }
    }
    }
    if (mm == 2)
    {
    if (dd >= 19)
    {
    cout << "You are a Pisces! You will have great luck." << endl;
    if (dd <= 20)
    {
    cout << "You are on the cusp of being a Aquarius. You will find your
    destined soulmate." << endl;
    }
    }
    }
    else if (mm == 3)
    {
    if (dd <= 20)
    {
    cout << "You are a Pisces! You will have great luck." << endl;
    if (dd >= 19)
    {
    cout << "You are on the cusp of being a Aries. You will find be
    traveling to faraway places." << endl;
    }
    }
    }

    cout << "\nTry again? (y or n): ";
    cin >> ans;
    }
    while (ans == 'Y' || ans == 'y');

    system("PAUSE");
    return 0;
    }[/php]

    Is that what you mean by the braces...?
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    I'm really big on the self-learning method of teaching. Try it and find out.
    If you're not referencing it anywhere, why would removing it break something?
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    After every open brace, you should increase your indent by a tab.
    After every close brace, you should decrease your indent by a tab.

    If you don't end up back at the left margin at the end of your code, you know you missed something somewhere.
  • edited March 2007
    ooo yeah i can see how that makes the code a lot easier to read, will post a new code after I get back from lunch, thanks again.

    Alrights finished..

    [php]/*
    __________________________________________________________________
    Programmer : Christopher La
    Project : Ch. 3.3
    Class : CS2
    Due : 3/15/07
    Description : Write an astrology program. Make up a horoscope
    for each sign. Your program should include a loop
    that lets the user repeat this calculation until
    the user says she or he is done.
    ___________________________________________________________________
    */
    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int mm, dd, ans;

    do
    {
    cout << "Enter your birth month (1 - 12): ";
    cin >> mm;
    cout << "Enter your day of birth: ";
    cin >> dd;

    if (mm == 1)
    {
    if (dd >= 20)
    {
    cout << "You are an Aquarius! You will find your destined soulmate."
    << endl;
    if (dd <= 21)
    {
    cout << "You are on the cusp of being a Capricorn. You ignore what
    doesn't interest you." << endl;
    }
    }
    }
    else if (mm == 2)
    {
    if (dd <= 18)
    {
    cout << "You are an Aquarius! You will find your destined soulmate."
    << endl;
    if (dd >= 19)
    {
    cout << "You are on the cusp of being a Pisces. You will have great
    luck. " << endl;
    }
    }
    }
    if (mm == 2)
    {
    if (dd >= 19)
    {
    cout << "You are a Pisces! You will have great luck." << endl;
    if (dd <= 20)
    {
    cout << "You are on the cusp of being a Aquarius. You will find your
    destined soulmate." << endl;
    }
    }
    }
    else if (mm == 3)
    {
    if (dd <= 20)
    {
    cout << "You are a Pisces! You will have great luck." << endl;
    if (dd >= 19)
    {
    cout << "You are on the cusp of being a Aries. You will find be
    traveling to faraway places." << endl;
    }
    }
    }
    if (mm == 3)
    {
    if (dd >= 21)
    {
    cout << "You are a Aries! You will find be traveling to faraway places."
    << endl;
    if (dd <= 20)
    {
    cout << "You are on the cusp of being a Pisces. You will have great
    luck." << endl;
    }
    }
    }
    else if (mm == 4)
    {
    if (dd <= 19)
    {
    cout << "You are a Aries! You will find be traveling to faraway places."
    << endl;
    if (dd >= 18)
    {
    cout << "You are on the cusp of being a Tauros. Your communication
    skills will benefit you greatly." << endl;
    }
    }
    }
    if (mm == 4)
    {
    if (dd >= 20)
    {
    cout << "You are a Tauros! Your communication skills will benefit you
    greatly." << endl;
    if (dd <= 21)
    {
    cout << "You are on the cusp of being a Aries! You will find be traveling
    to faraway places." << endl;
    }
    }
    }
    else if (mm == 5)
    {
    if (dd <= 20)
    {
    cout << "You are a Tauros! Your communication skills will benefit you
    greatly." << endl;
    if (dd >= 19)
    {
    cout << "You are on the cusp of being a Gemini. You will be attracting
    many people with your charms." << endl;
    }
    }
    }
    if (mm == 5)
    {
    if (dd >= 21)
    {
    cout << "You are a Gemini! You will be attracting many people with your
    charms." << endl;
    if (dd <= 22)
    {
    cout << "You are on the cusp of being a Aries! You will find be traveling
    to faraway places." << endl;
    }
    }
    }
    else if (mm == 6)
    {
    if (dd <= 21)
    {
    cout << "You are a Gemini! You will be attracting many people with your
    charms." << endl;
    if (dd >= 21)
    {
    cout << "You are on the cusp of being a Cancer. Your generous heart shall
    attract many people." << endl;
    }
    }
    }
    if (mm == 6)
    {
    if (dd >= 22)
    {
    cout << "You are a Cancer! Your generous heart shall attract many people."
    << endl;
    if (dd <= 23)
    {
    cout << "You are on the cusp of being a Gemini! You will be attracting
    many people with your charms." << endl;
    }
    }
    }
    else if (mm == 7)
    {
    if (dd <= 22)
    {
    cout << "You are a Cancer! Your generous heart shall attract many people."
    << endl;
    if (dd >= 21)
    {
    cout << "You are on the cusp of being a Leo. You will be open to many
    tasks, but don't overload yourself." << endl;
    }
    }
    }
    if (mm == 7)
    {
    if (dd >= 23)
    {
    cout << "You are a Leo! You will be open to many tasks, but don't overload
    yourself." << endl;
    if (dd <= 24)
    {
    cout << "You are on the cusp of being a Cancer. Your generous heart
    shall attract many people." << endl;
    }
    }
    }
    else if (mm == 8)
    {
    if (dd <= 22)
    {
    cout << "You are a Leo! You will be open to many tasks, but don't overload
    yourself." << endl;
    if (dd >= 21)
    {
    cout << "You are on the cusp of being a Virgo. You shall do what you can
    to maintain all your relationships." << endl;
    }
    }
    }
    if (mm == 8)
    {
    if (dd >= 23)
    {
    cout << "You are a Virgo! You shall do what you can to maintain all your
    relationships." << endl;
    if (dd <= 24)
    {
    cout << "You are on the cusp of being a Leo. You will be open to many
    tasks, but don't overload yourself." << endl;
    }
    }
    }
    else if (mm == 9)
    {
    if (dd <= 22)
    {
    cout << "You are a Virgo! You shall do what you can to maintain all your
    relationships." << endl;
    if (dd >= 21)
    {
    cout << "You are on the cusp of being a Libra. Your creative writing will
    get you very far." << endl;
    }
    }
    }
    if (mm == 9)
    {
    if (dd >= 23)
    {
    cout << "You are a Libra! Your creative writing will get you very far."
    << endl;
    if (dd <= 24)
    {
    cout << "You are on the cusp of being a Virgo. You shall do what you can
    to maintain all your relationships." << endl;
    }
    }
    }
    else if (mm == 10)
    {
    if (dd <= 22)
    {
    cout << "You are a Libra! Your creative writing will get you very far."
    << endl;
    if (dd >= 21)
    {
    cout << "You are on the cusp of being a Scorpio. Your creative insight
    gains you recognition." << endl;
    }
    }
    }
    if (mm == 10)
    {
    if (dd >= 23)
    {
    cout << "You are a Scorpio! Your creative insight gains you recognition."
    << endl;
    if (dd <= 24)
    {
    cout << "You are on the cusp of being a Libra. Your creative writing will
    get you very far." << endl;
    }
    }
    }
    else if (mm == 11)
    {
    if (dd <= 21)
    {
    cout << "You are a Scorpio! Your creative insight gains you recognition."
    << endl;
    if (dd >= 20)
    {
    cout << "You are on the cusp of being a Sagittarius. People will come to
    you as you are a great listener." << endl;
    }
    }
    }
    if (mm == 11)
    {
    if (dd >= 22)
    {
    cout << "You are a Sagittarius! People will come to you as you are a great
    listener." << endl;
    if (dd <= 23)
    {
    cout << "You are on the cusp of being a Scorpio. Your creative insight
    gains you recognition." << endl;
    }
    }
    }
    else if (mm == 12)
    {
    if (dd <= 21)
    {
    cout << "You are a Sagittarius! People will come to you as you are a great
    listener." << endl;
    if (dd >= 20)
    {
    cout << "You are on the cusp of being a Capricorn. You ignore what
    doesn't interest you." << endl;
    }
    }
    }
    if (mm == 12)
    {
    if (dd >= 22)
    {
    cout << "You are a Capricorn! You ignore what doesn't interest you."
    << endl;
    if (dd <= 23)
    {
    cout << "You are on the cusp of being a Sagittarius. People will come to
    you as you are a great listener." << endl;
    }
    }
    }
    else if (mm == 1)
    {
    if (dd <= 10)
    {
    cout << "You are a Capricorn! You ignore what doesn't interest you."
    << endl;
    if (dd >= 9)
    {
    cout << "You are on the cusp of being a Aquarius. You will find your
    destined soulmate." << endl;
    }
    }
    }

    cout << "\nTry again? (y or n): ";
    cin >> ans;
    }while (ans == 'Y' || ans == 'y');

    system("PAUSE");
    return 0;
    }[/php]

    That is a LONG code... >< but still one more problem.. Loop.. as I can't get it to loop... Not sure what i am missing.
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    I changed your dates as they weren't correct. I used the Tropical dates from Wikipedia

    This isn't complete. It has logic holes in it, but I can't do your work for you, can I? But this will show you another way of approaching the problem.

    Also, as for Shwaip saying if you do not reference something it won't break anything to remove it. He is correct, though read his statement carefully. He didn't say remove it if you don't reference. I like to leave everything I've coded in until I'm done. I just comment it out and put notes in so I can remember why I initially had it or why I commented it out. So, if you're not sure if you want to keep something or not, keep it and just comment it out and make notes. When all is done, if you still don't need it, go through and remove it.

    [php]
    #include <cstdlib>
    #include <iostream>

    using namespace std;
    /*** SIGNS AND DATES ***
    AQUARIUS // Aquarius January 21 - February 19
    PISCES // Pisces February 20 - March 20
    ARIES // Aries March 21 - April 19
    TAURUS // Taurus April 20 - May 21
    GEMINI // Gemini May 22 - June 21
    CANCER // Cancer June 22 - July 23
    LEO // Leo July 24 - August 23
    VIRGO // Virgo August 24 - Suptember 23
    LIBRA // Libra September 24 - October 23
    SCORPIO // Scorpio October 24 - November 22
    SAGITTARIUS // Sagittariius November 23 - December 21
    CAPRICORN // Capricorn December 22 - January 20
    *** SIGNS AND DATES ***/

    int day_begin[] = { 21, 20, 21, 20, 22, 22, 24, 24, 24, 24, 23, 22 };
    int day_end[] = { 20, 19, 20, 19, 21, 21, 23, 23, 23, 23, 22, 21 };
    char* Signs[] = {
    "Aquarius: You will find your destined soulmate.",
    "Pisces: You will have a good luck streak.",
    "Aries: You will be traveling to faraway places.",
    "Taurus: Your communication skills will benefit you greatly.",
    "Gemini: You will be attracting many people with your charms.",
    "Cancer: Your generous heart shall attract many people.",
    "Leo: You will be open to many tasks, but don't overload yourself.",
    "Virgo: You shall do what you can to maintain all your relationships.",
    "Libra: Your creative writing will get you very far.",
    "Scorpio: Your creative insight gains you recognition.",
    "Saggittarius: People will come to you as you are a great listener.",
    "Capricorn: You ignore what doesn't interest you.",
    };

    int main(int argc, char *argv[])
    {
    int month, day;
    char repeat;

    do {
    cout << "Enter the Month you were born (1 - 12): ";
    cin >> month;
    cout << endl;
    cout << "Enter the Day you were born: ";
    cin >> day;
    cout << endl;

    if (month >= 1 && month <= 12) {
    if (day >= 1 && day <= 31) {
    if (month % 2 == 0) {
    if (day > 30 || (month == 2 && day > 28)) {
    cout << "Incorrect Day of month, please try again." << endl;
    cout << "Would you like to enter another birthday? (Y/N)" << endl;
    cin >> repeat;
    continue;
    }
    }
    if (day >= day_begin[month - 1]) {
    cout << Signs[month - 1] << endl;
    if ((day - 1) < day_begin[month - 1]) {
    if (month == 1) {
    cout << "You are on the cusp of being an " << Signs[12] << endl;
    } else {
    cout << "You are on the cusp of being an " << Signs[month - 2];
    }
    }
    } else {
    if (month == 1) {
    cout << Signs[12];
    if ((day + 1) >= day_begin[month - 1]) {
    cout << "You are on the cusp of being an " << Signs[month - 1] << endl;
    }
    } else {
    cout << Signs[month - 2];
    if ((day + 1) >= day_begin[month - 1]) {
    cout << "You are on the cusp of being an " << Signs[month - 1] << endl;
    }
    }
    }
    } else {
    cout << "Incorrect Day of month, please try again." << endl;
    cout << "Would you like to enter another birthday? (Y/N)" << endl;
    cin >> repeat;
    continue;
    }
    } else {
    cout << "Incorrect Month, please try again." << endl;
    cout << "Would you like to enter another birthday? (Y/N)" << endl;
    cin >> repeat;
    continue;
    }

    cout << "Would you like to enter another birthday? (Y/N)" << endl;
    cin >> repeat;
    } while (repeat == 'Y' || repeat == 'y');

    return EXIT_SUCCESS;
    }
    [/php]
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    Here's another way to approach the problem (which is very similar) with one logic fix thrown in. Neither this nor the previous post are fully functional, so you can't use these for your assignment (my intent, as I'm not wanting to do your work, just trying to give you code to learn from as you seem to be having difficulty finding solutions by just reading text in your book and looking at the ridiculous examples that are normally given, which is understandable).

    [php]
    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int day_begin[] = { 22, 21, 20, 21, 20, 22, 22, 24, 24, 24, 24, 23, 22 };
    char* Signs[] = {
    "Capricorn: You ignore what doesn't interest you.",
    "Aquarius: You will find your destined soulmate.",
    "Pisces: You will have a good luck streak.",
    "Aries: You will be traveling to faraway places.",
    "Taurus: Your communication skills will benefit you greatly.",
    "Gemini: You will be attracting many people with your charms.",
    "Cancer: Your generous heart shall attract many people.",
    "Leo: You will be open to many tasks, but don't overload yourself.",
    "Virgo: You shall do what you can to maintain all your relationships.",
    "Libra: Your creative writing will get you very far.",
    "Scorpio: Your creative insight gains you recognition.",
    "Saggittarius: People will come to you as you are a great listener.",
    "Capricorn: You ignore what doesn't interest you.",
    };

    int main(int argc, char *argv[])
    {
    int month, day;
    char repeat;

    do {
    do {
    cout << "Enter the Month you were born (1 - 12): ";
    cin >> month;
    cout << endl;
    } while (month >= 1 && month <= 12);

    do {
    cout << "Enter the Day you were born: ";
    cin >> day;
    cout << endl;
    } while (day >= 1 && day <= 31);

    if (month % 2 == 0) {
    if (day > 30 || (month == 2 && day > 28)) {
    cout << "Incorrect Month/Day combination, please try again." << endl;
    cout << "Would you like to enter another birthday? (Y/N)" << endl;
    cin >> repeat;
    continue;
    }
    }
    if (day >= day_begin[month]) {
    cout << Signs[month] << endl;
    if ((day - 1) < day_begin[month]) {
    cout << "You are on the cusp of being an " << Signs[month - 1] << endl;
    }
    } else {
    cout << Signs[month - 1];
    if (day <= (day_begin[month - 1] + 1)) {
    cout << "You are on the cusp of being an " << Signs[month - 1] << endl;
    }
    }
    cout << "Would you like to enter another birthday? (Y/N)" << endl;
    cin >> repeat;
    } while (repeat == 'Y' || repeat == 'y');

    return EXIT_SUCCESS;
    }
    [/php]
  • edited March 2007
    Thank you, shall go over it and look it over as I get home.

    BUt even though the long code I had, there is a way to make it loop correct?

    Also that alternative code, i just tested it and it seems it is looping it too much.
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    I said it wasn't fully functional. I made it that way so that you can learn and not just use the code to complete the assignment
  • edited March 2007
    Nah, I knew it wasn't completed, but thought it would run.
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    Well, it does run, just not the way as is expected. :D
  • edited March 2007
    Bah, I'll turn it in the way I have it and have him correct it and then see if he can help me out after that...

    Thanks for all the alternatives though.

    Now trying to work on the next layout for the next problem before posting.
  • edited March 2007
    Alright question 9: 9. Interest on a loan is paid on a declining balance, and hence a loan with an interest rate of, say, 14% can cost significantly less than 14% of the balance. Write a program that takes a loan amount and interest rate as input and then outputs the monthly payments and balance of the loan until the loan is paid off. Assume that the monthly payments are one-twentieth of the original loan amount, and that any amount in excess of the interest is credited toward decreasing the balance due. Thus, on a loan of $20,000, the payments would be $1,000 a month. If the interest rate is 10%, then each month the interest is one-twelfth of 10% of the remaining balance. The first month, (10% of $20,000)/12 or $166.67, would be paid in interest, and the remaining $833.33 would decrease the balance to $19,166.67. The following month the interest would be (10% of $19,166.67)/12, and so forth. Also have the program output the total interest paid over the life of the loan.
    <o>:p> </o>:p>
    Finally, determine what simple annualized percentage of the original loan balance was paid in interest. For example, if $1,000 was paid in interest on a $10,000 loan and it took two years to pay off, then the annualized interest is $500, which is 5% of the $10,000 loan amount. Your program should allow the user to repeat this calculation as often as desired.

    And I am slowly starting off.

    [php]/*
    __________________________________________________________________
    Programmer : Christopher La
    Project : Ch. 3.9
    Class : CS2
    Due : 3/22/07
    Description : Write a program that takes a loan amount and
    interest rate as input and then outputs the
    monthly payments and balance of the loan until the
    loan is paid off.
    ___________________________________________________________________
    */
    #include
    #include

    int main()
    {
    int loan, ans;
    double interest, payment, paid_in_interest;

    do
    {
    cout << "Enter loan (rounded to whole dollars): $";
    cin >> loan;

    cout << "Enter interest rate: ";
    cin >> interest;
    payment = (loan * (0.05));

    cout << "It will take a monthly payment of $ " << payment << " to pay off\n"
    << "the loan." << endl;
    paid_in_interest = ((interest/100) * loan) / 12;

    cout << "Paid interest of $ " << paid_in_interest << " for first month.";

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "\nTry again? (y or n): ";
    cin >> ans;

    }while (ans == 'Y' || ans == 'y');

    system("PAUSE");
    return 0;
    }[/php]As you can see I am struggling in the 'paid_in_interest' as it is giving me 3 numers to the right of the Decimal.

    Inputted numbers are 20000 and interest rate of 10.</stdlib.h></iostream.h>
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    I can't see which header files you're including, but one should be iomanip.
    Also, setprecision() is used as such, where x is the float/double variable you're wanting to print.
    [php]
    cout << fixed << setprecision(2) << x << endl;
    [/php]

    I'm not saying the way you're using cout.setf() is wrong, but it's in the wrong place, as you're setting the format parameters after you're printing paid_in_interest.
  • edited March 2007
    Yeah I did try with the iomanip but then I left the cout.setf and precision as is so that might have been the problem.

    But then using that given line and replacing x with 'paid_in_interest' now it is saying that 'fixed' is undeclared.. would I have to input that as an int as well?

    [php]#include
    #include
    #include

    int main()
    {
    int loan, ans, fixed;
    double interest, payment, paid_in_interest;

    do
    {
    cout << "Enter loan (rounded to whole dollars): $";
    cin >> loan;

    cout << "Enter interest rate: ";
    cin >> interest;
    payment = (loan * (0.05));

    cout << "It will take a monthly payment of $ " << payment << " to pay off\n"
    << "the loan." << endl;
    paid_in_interest = ((interest/100) * loan) / 12;

    cout << "Paid interest of $ " << fixed << setprecision(2)
    << paid_in_interest << " for first month." << endl;


    cout << "\nTry again? (y or n): ";
    cin >> ans;

    }while (ans == 'Y' || ans == 'y');

    system("PAUSE");
    return 0;
    }[/php]I thought maybe I coudl do it that way...

    EDIT: Oh.. yeah I was trying to follow the example in the book and it was formatted that way.
    </iomanip.h></stdlib.h></iostream.h>
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    you also need to put
    [php]
    using namespace std;
    [/php]

    above int main()
  • edited March 2007
    Bah, definitely still doing something wrong ><,

    [php]/*
    __________________________________________________________________
    Programmer : Christopher La
    Project : Ch. 3.9
    Class : CS2
    Due : 3/22/07
    Description : Write a program that takes a loan amount and
    interest rate as input and then outputs the
    monthly payments and balance of the loan until the
    loan is paid off.
    ___________________________________________________________________
    */
    #include <iostream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    using namespace std;

    int main()
    {
    int loan, ans, fixed;
    double interest, payment, paid_in_interest;

    do
    {
    cout << "Enter loan (rounded to whole dollars): $";
    cin >> loan;

    cout << "Enter interest rate: ";
    cin >> interest;
    payment = (loan * (0.05));

    cout << "It will take a monthly payment of $ " << payment << " to pay off\n"
    << "the loan." << endl;
    paid_in_interest = ((interest/100) * loan) / 12;

    cout << "Paid interest of $ " << paid_in_interest << " for first month."
    << endl;

    cout << fixed << setprecision(2) << paid_in_interest << endl;

    cout << "\nTry again? (y or n): ";
    cin >> ans;

    }while (ans == 'Y' || ans == 'y');

    system("PAUSE");
    return 0;
    }[/php]
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    You're using:
    iostream.h, which should be just iostream
    stdlib.h, which should be cstdlib
    iomanip.h, which should be iomanip
  • edited March 2007
    Oh for C++ I need to add the .h at the end or they don't work.
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    Actually, it's C++ that took the .h away, but if you don't want to use the NON .h version, then fine, but take out the "using namespace std;" then.

    iostream.h (automatically pulls in the ios classes and puts them in the global namespace)
    iostream (pulls in the ios classes and puts them in the std namespace)
    iomanip.h (puts everything in the global namespace)
    iomanip (puts everything in the std namespace)
    stdlib.h (puts everything in the global namespace)

    So on and so forth.
  • edited March 2007
    Bah internet went bleh on me...

    Well the version of C++ I use requires the addition of .h

    But anywho, not too sure how to continue, / how to start the rest of the problem ><
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    Perhaps you could be a bit more specific as "not too sure how to continue" doesn't give much. Continue from where? The code you last posted, or where you are now? Not sure on what, the math or the C++?

    And unless you're using some odd-ball C++ compiler with strange libraries, then removing .h should not give you any problems, as long as "using namespace std;" is above main(). But, hey, whatever floats your boat.
  • edited March 2007
    Oh woops I am sorry, emmm I meant to continue on after the math part, "The first month, (10% of $20,000)/12 or $166.67, would be paid in interest, and the remaining $833.33 would decrease the balance to $19,166.67. The following month the interest would be (10% of $19,166.67)/12, and so forth. Also have the program output the total interest paid over the life of the loan."

    Not to sure how I would go on with that.... as the main math part I'll try to just ask a teacher on that part as the set precision thing isn't working for me.
Sign In or Register to comment.