C++ Assignment

135

Comments

  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    a2jfreak wrote:
    How did you get the cin or the cout functionality? Where are they located?
    However you were able to use those is the same way you'll be able to use toupper().

    airbornflight: ans is not an object of the string class, therefore it has no .functions, it is nothing but a variable. Therefore it would be:
    [php]
    ans = toupper(ans);
    [/php]

    gotcha. I have virtually no clue when it comes to C++, I'm just going off of what I've learned from JAVA. Now JAVA I have down pat. I made a 95% on my 9 weeks AP Comp Sci Test. I'm going to pwn the AP Exam. I should take the AB exam. I think I can handle it.
  • edited March 2007
    Hehe no clue what you mean by how I got the cin/cout to work. As for the toupper thing I tried inputting it into the code but kept getting a 'Implicit declaration of function' so I know I am inputting it wrong...
  • GHoosdumGHoosdum Icrontian
    edited March 2007
    Do you have the proper header file included for use of toupper?
  • edited March 2007
    Hmm probably not as I didn't list it as like an int, tried it as a char but that didn't work.
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    I told you where to find toupper().
    I don't want to do your work for you anymore than I already have as that really won't help you. I can give you fish all day long, but the day I stop is the day you die. If I teach you how to fish you can live long after I'm gone.

    Google is a wonderful resource, so should be your instructor--that is what he's paid for. Have you taken a Programming Logic class yet? If not, you could greatly benefit from it. Also, if your school offers Perl, PHP or any other language that doesn't require things be explicitly typed, that might help you as you would only need to deal with the problem and not worry about other things like char/int/string/float/double/etc. Once you know how to handle problems in general, you can probably handle more specific problems better that require explicit typing.
  • edited March 2007
    More or less for 'toupper()' is 4 chapters ahead of where I currently am, that is why I am not too sure as to how to use it. THat is why I asked as I am not familiar with it and therefore am unsure whether i should have it in the code or not.
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    Some will agree with this statement, some with disagree.
    Personally, I find Java and C++ to be very similar. The main difference (and someone that knows Java better than I can correct me if I'm wrong) is that in Java everything is an object, whereas in C++, since it is (pretty-much) a superset of C, you can have data that has nothing explicitly associated with it (member functions, member data). Take for example the string "Hi Andy."
    in Java that would be
    [php]
    string greeting = "Hi Andy.";
    [/php]

    in C it would be
    [php]
    char* greeting = "Hi Andy.";
    [/php]

    in C++ it would be
    [php]
    string greeting = "Hi Andy.";
    [/php]

    Now, the C++ string class might allow for member functions like toUpper(), so
    [php]
    string upperGreeting = greeting.toUpper();
    [/php]
    would put "HI ANDY." in upperGreeting. Except, I don't think there is a toUpper() member function. I gave that only as an example. You could add the member function, though. There are member functions like size(), replace(), find(), substr(), so you could use greeting.size() or greeting.replace(), etc.

    However, C doesn't have objects, so greeting is just a pointer to type char. There are no member functions so there is nothing like greeting.replace().

    Since he's not using the C++ string class, he has none of the OO functionality for strings.

    In fact, he shouldn't even be using iostream.h or stdlib.h, he should be using:
    [php]
    #include <iostream>
    #include <csdtlib>
    [/php]

    And then all his cin's and cout's would be std::cin or std::cout.
    Or, he could insert:
    [php]
    using namespace std;
    [/php]
    and then he could use cin or cout without using std::.

    I'm getting deeper than I should, though.
    gotcha. I have virtually no clue when it comes to C++, I'm just going off of what I've learned from JAVA. Now JAVA I have down pat. I made a 95% on my 9 weeks AP Comp Sci Test. I'm going to pwn the AP Exam. I should take the AB exam. I think I can handle it.
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    If you're not comfortable with toupper(), then don't use it. But you will need to figure out another way to make the comparison as to whether or not ans1 and ans2 are the same (won't be difficult, just a little more code). If you've gotten this far you'll be able to figure it out.
    More or less for 'toupper()' is 4 chapters ahead of where I currently am, that is why I am not too sure as to how to use it. THat is why I asked as I am not familiar with it and therefore am unsure whether i should have it in the code or not.
  • edited March 2007
    Oh just for the record I am using C++ if you were referring to me. But yeah shall get on trying some more after I return.
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    Yes, I know you are. My post comparing C++ and Java was directed to airbornflight.
    Oh just for the record I am using C++ if you were referring to me. But yeah shall get on trying some more after I return.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    Yeh, I know they are very simillar. And yes, afaik, just about everything is an object except the primitives; but even they have wrapper classes. Though I'm a relative noob so to speak when compared to some industry pro's.

    I do know that C++ is 'lower' than java. So some stuff you are talking about I'm having to look up. Such as std::cin and that business about the difference between #include <iostream.h> and #include <iostream>. I just thought that the latter was the newer and 'right' way to do it. while the former was the legacy way to do it.
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    Close enough.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    a2jfreak wrote:
    Close enough.

    lol. I won't even pretend to know much of the advanced features in C++. I just have a good solid background in OOP with JAVA.
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    Well, it has been ages since I've coded in C++ (years, really) so I've forgotten a lot of what things are called and the exact reasoning behind why iostream.h is different than iostream. Using iostream.h pulls in the whole std namespace, whereas using iostream lets you select what you need to use, therefore allowing one to choose what is pulled in or not thereby reducing code size. Now, I say that, and that's what I think, but I could be wrong. I just did some tests using Dev-C++ to see if the exe size varied and in all tests it stayed the same so either I'm mistaken, or GCC doesn't care about scoping the way it should. Who knows. So, Basically, I can't explain it any better, I'm sure there are many C++ guys around here than can not only elaborate on what I've said, but set the record straight and correct anything that was erroneous.
  • edited March 2007
    I'll just ask the Professor on that little part or another student...

    Thanks for the help.

    Now onto the next Problem.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    oh, a2j, I hate to nitpick, but in java, String is actually a class and is declared String, not string. It's just made to look like a primitive.
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    Thanks for the clarification; I had forgotten String was not primitive. It's been quite some time (5-6 years) since I last coded any Java, and then it was only for school so I never got into the nitty gritty with Java.
    oh, a2j, I hate to nitpick, but in java, String is actually a class and is declared String, not string. It's just made to look like a primitive.
  • edited March 2007
    Alright got it to work out right, and asked a friend in class about it and it was the 'toupper()' that needed to be used. Again I thought i shouldn't use it as it wasn't part of the chapter within the problem... But I guess it is acceptable as I double checked with the teacher right before i took off to another class...

    So thank you.

    As for #3, although I know it will be using many multibranch statements... I am not too sure how to begin:

    10. Write an astrology program. The user types in a birthday, and the program responds with the sign and horoscope for that birthday. The month may be entered as a number from 1 to 12. Then enhance your program so that if the birthday is only one or two days away from an adjacent sign, the program announces that the birthday is on a "cusp" and also outputs the horoscope for the nearest adjacent sign. This program will have a long multiway branch. 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.

    Here is just what I have accumulated and inputted what I will be using...
    [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;

    do
    {
    cout >> "Enter your birthday (mm dd): \n";
    cin << mm dd;

    enum Horoscope_Signs
    {
    Aries March 21 - April 19
    Taurus April 20 - May 20
    Gemini May 21 - June 21
    Cancer June 22 - July 22
    Leo July 23 - August 22
    Virgo August 23 - Suptember 22
    Libra September 23 - October 22
    Scorpio October 23 - November 21
    Sagittariius November 22 - December 21
    Capricorn December 22 - January 10
    Aquarius January 20 - Februarry 19
    Pisces February 10 - March 20
    }

    cout << "You will be traveling to faraway places. "
    cout << "Your communication skills will benefit you greatly. "
    cout << "You will be attracting many people with your charms. "
    cout << "Your generous heart shall attract many people. "
    cout << "You will be open to many tasks, but don't overload yourself. "
    cout << "You shall do what you can to maintain all your relationships. "
    cout << "Your creative writing will get you very far. "
    cout << "Your creative insight gains you recognition. "
    cout << "People will come to you as you are a great listener. "
    cout << "You ignore what doesn't interest you. "
    cout << "You will find your destined soulmate. "
    cout << "You will have a good luck streak. "

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

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

    For the 'enum' part I am sure I will be using that but not too sure how to lay it out....
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    Your enum syntax is incorrect. And i know that's in your book.
  • edited March 2007
    Nah I know it is wrong, I am saying I know I need to use that but I am not too sure how to label out the inside of it.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited March 2007
    so are you saying that is your pseudo code?
  • edited March 2007
    That is correct it is just a pseudo code.
  • a2jfreaka2jfreak Houston, TX Member
    edited March 2007
    Never feel like you can't use a feature of a language just because the instructor has yet to cover it unless he has instructed you otherwise.
    Alright got it to work out right, and asked a friend in class about it and it was the 'toupper()' that needed to be used. Again I thought i shouldn't use it as it wasn't part of the chapter within the problem... But I guess it is acceptable as I double checked with the teacher right before i took off to another class...

    So thank you.
    As for #3, although I know it will be using many multibranch statements... I am not too sure how to begin:

    10. Write an astrology program. The user types in a birthday, and the program responds with the sign and horoscope for that birthday. The month may be entered as a number from 1 to 12. Then enhance your program so that if the birthday is only one or two days away from an adjacent sign, the program announces that the birthday is on a "cusp" and also outputs the horoscope for the nearest adjacent sign. This program will have a long multiway branch. 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.

    OK, Let's see... using enum.

    [php]
    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 - Februarry 19
    PISCES // Pisces February 10 - March 20
    }
    [/php]

    That means ARIES = 0 and PISCES = 11.
    Of course, that doesn't really help us at all right now.

    In fact, I haven't thought of a way to easily use enums to accomplish what you need. Perhaps a struct would better suit your needs, unless you have to use enum.

    Or, use an enum + struct, or enum + arrays.
    Using just an enum (unless I'm just not thinking in an obvious direction) would be quite awkward and cumbersome.

    Using a multi-way branch as you were directed, you would need nothing but if statements. No enums, no structs, no arrays.

    [php]
    if (mm == 1) {
    if (dd >= 20) {
    cout << "You are an Aquarius!" << endl;
    if (dd <= 21) {
    cout << "You are on the cusp of being a Capricorn." << endl;
    }
    }
    } else if (mm == 2) {
    if (dd <= 19) {
    cout << "You are an Aquarius!" << endl;
    if (dd >= 18) {
    cout << "You are on the cusp of being a Pisces." << endl;
    }
    }
    }
    [/php]
  • edited March 2007
    True as maybe I don't need to use the enum and the instructions do say it is multibranch so I would agreed it would be filled with if statements.

    But if I use the format that you provided, how would I label what you did for the enum? As that wouldn't be included if you advise the multibranch doesn't need them...

    EDIT: Also I can just add cout << +My horoscope after each line right? (By means of after 'You are An Aquarius'/'You are on the cusp of being a Pisces.)

    2nd EDIT: Also I can just combine the two if's of month/day like this right 'if (mm == 1 || dd >= 20)'


    Alrights: Just concentrating on the if parts.. It would be something like this right? (NOt fully done just a starting point)
    [php] if (mm == 1 || dd >= 20)
    {
    cout << "You are an Aquarius!" << endl;
    cout << "You will find your destined soulmate. " << endl;
    if (dd <= 21)
    {
    cout << "You are on the cusp of being a Capricorn." << endl;
    cout << "You ignore what doesn't interest you. " << endl;
    }
    }
    }
    else if (mm == 2) {
    if (dd <= 18) {
    cout << "You are an Aquarius!" << endl;
    cout << "You will find your destined soulmate. " << endl;
    if (dd >= 19) {
    cout << "You are on the cusp of being a Pisces." << endl;
    cout << "You will have great luck. " << endl;
    }
    }
    }
    if (mm == 2) {
    if (dd >= 19) {
    cout << "You are a Pisces!" << endl;
    cout << "You will have great luck. " << endl;
    if (dd <= 20) {
    cout << "You are on the cusp of being a Aquarius." << endl;
    cout << "You will find your destined soulmate. " << endl;
    }
    }
    } else if (mm == 3) {
    if (dd <= 20) {
    cout << "You are a Pisces!" << endl;
    cout << "You will have great luck. " << endl;
    if (dd >= 19) {
    cout << "You are on the cusp of being a Aries." << endl;
    cout << "You will find be traveling to faraway places. " << endl;
    }
    }
    }[/php]
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    Sorry if I'm coming off as a dick with this, but...

    try it. If it doesn't work, try something else. The things that I remember most are the things that I've messed up on. Whenever I ask for advice, it always seems like the person starts their response with "Oh, I had that problem, and I solved it by..."

    Nextly, it's usually easier to read code that doesn't have conditions in an if/while/etc that depend on multiple variables. You'll also often miss cases that you don't realize.

    Lastly, this is what my (very high level) pseudocode would look like for this assignment.

    1) get mm/dd input from user
    2) Check to see what month it is, and to what signs that month can correspond to (IE: January (mm = 1) can be capricorn or aquarius, depending on the day)
    3) Check the day vs the boundaries of the signs (if we know we're in month 1, and the day is the tenth (dd = 10), then you know exactly what sign the person is.
    4) Lastly, check to see if they're close to the ends of a sign
    5) Output the sign.
    6) goto 1

    In general, pseudocode has no code, only a brief outline of the conditional statements, input/output, etc.
  • edited March 2007
    I see... yeah I wasn't too sure as to the meaning of pseudo except that of a fake, but didn't know it meant like no codes or anything.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    for your first if statement, what happens if I enter October 21?
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    I see... yeah I wasn't too sure as to the meaning of pseudo except that of a fake, but didn't know it meant like no codes or anything.

    The point of pseudocode is to sketch out the flow of your program, without having to worry about syntax. You can do that with or without real code...it usually ends up being a mix of real code and meaningless code.
  • edited March 2007
    Hmm if you entered October it wouldn't matter right? As it is going by the Month and October = 11 and not 2(February) unless I am mistaken..
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    sooo...did you try it?

    [php]if (mm == 1 || dd >= 20)[/php]</SPAN></SPAN>

    what does this give for october 21?
Sign In or Register to comment.