Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ assignment

  1. May 21, 2007 #1
    Hi Everyone I have a object oriented C++ assignment which is due this week and I have no idea as to how start it.................

    This program prints a motion verb ( fly, run, swim, crawl, walk, or roll ), waits for a second,
    then prints the name of an entity and repeats the verb (for example: fly ..1s.. pigeon fly!.. ).
    The player has ½ second to type ‘y’ for yes or ‘n’ for no. (case insensitive)

    - If the answer is correct, the player scores, if incorrect, or if there was no answer within the ½ second, the
    player looses a point. When correct, all the motions the entity can perform are printed.

    - When wrong, Ka..BOOM! is printed instead.

    The default number of questions per player is 20, but the number can be changed by passing a different
    number on the command line when starting the program.

    At the end of the run the player’s score (and the score of previous players if there were any) is printed, the
    question: “Another player (Y/N): “ is asked. The game exits if ‘n’ is typed.

    The output should look something like this:-

    Player 1 starting.
    You must answer with the letter ‘y’ for YES, or ‘n’ for NO.
    Press [Enter] when ready to start:

    1 – fly pigeon fly!.. y
    - I walk - I fly
    2 – swim Wheelbarrow swim.. n
    - I roll
    3 – crawl ball crawl!..
    4 – fly plane fly!.. y
    - I roll - I fly.
    5 – run boat run!.. n
    - I roll.
    6 – walk engine walk!..
    7 – roll stone roll!.. y
    - I roll.
    . . .
    16- crawl time crawl!.. y
    - I crawl - I fly.
    17- swim goose swim!.. n
    18- run lizard run!.. y
    - I run - I crawl.
    19- run nose run.. y
    - I run
    20- fly goose fly!.. y
    - fly - I walk – I run – I swim.

    Player 1 *** score: 16 ***

    Well done! Start another player (Y or N)? y

    Player 2 starting
    You must answer with the letter ‘y’ for YES, or ‘n’ for NO.
    Press [Enter] when ready to start:

    1 – crawl giraffe crawl!.. y

    I am not good at all in coding so i tried and made an overview of what the main driver should look like

    check here wherther the player is old or new------>Connector

    where a = value of yes or no, taken by cin>>
    if the vaue is yes....
    you are back in main >>>here

    now start the for...while/Do...while[(condition for 1/2 min)& this shld take care of the initial 30 seconds also]
    call function2(loop for 20 questions)
    else if right answer add a point
    Also call a function to print what the object does (for ball, it will be I ROLL)

    keep adding ( + or -)points in a variable

    End for loop of questions

    display final score
    ask to replay ------------>connector

    But as said above I have no idea on the coding part of the whole program and would like some help here please

    Also apart from this can anyone suggest a good compiler, I use Visual Basic C++ if thats good

    Thanks in advance:cool:
  2. jcsd
  3. May 21, 2007 #2


    User Avatar

    man if this was in VB, it would take 2 mins
  4. May 21, 2007 #3


    User Avatar
    Science Advisor
    Homework Helper

    If you were doing it in a non-OO language, one obvious way to go would be encode the definitions of what objects can do what actions in a 2-D array, and the names of the objects and actions in two 1-D arrays. Apart from the data, the program would then be about 20 lines long (including comments).

    But in C++, you can make it far more complcated than that by using templates to create collections of classes with multiple inheritance, or whatever... :rolleyes:
  5. May 22, 2007 #4
    yup C would have been easier, I got friends who are doing Java too..................... but C++ is one language in which i cant get the codes, it just freaks me out. but I am still strying, working on the code and will post it when i haven made some(or any) progress at all.

    Anyways thanks guys for replying still if any1 can will help a lot:rolleyes:
  6. May 22, 2007 #5
    If you have borland turbo cpp 3, then you could use dos.h which has a delay(time in ms) function. However, I dont know what the corresponding function would be in ansi cpp or whatever.. im still getting used to it...

    Anyway, you could create a class which stores the question, and its answer, use the delay function (or one like it), and test the input... sorry, im not much help...
  7. May 22, 2007 #6
    Srry i use Microsoft visual studio 8...................... hehehehehe guess i am too lost to remember that too, anyhow anybody who can code the game program in c++ still find it tough to understand
  8. May 24, 2007 #7
    so anybody has anything to tell me???? i dont have much time left with other things in hand..........................
  9. May 27, 2007 #8
    here is something new


    Code: ( cpp )

    #ifndef MOTION_H
    #define MOTION_H

    #include <iostream>
    #include <string>

    using namespace std;

    class Motion
    friend ostream &operator << (ostream &out, const Motion &m);

    virtual ~Motion(){};
    virtual string *toString() const = 0;
    virtual bool contains(const string *key) const = 0;




    Code: ( cpp )

    #include <iostream>
    #include <sstream>
    #include <string>
    #include "Motion.h"

    ostream &operator << (ostream &out, const Motion &m)

    out << "- I " << m.toString();

    return out;


    Code: ( cpp )

    #ifndef FLYMOTION_H
    #define FLYMOTION_H

    #include <iostream>
    #include <string>

    using namespace std;

    #include "Motion.h"

    class FlyMotion : public Motion

    static FlyMotion* instance();
    virtual string *toString() const;
    virtual bool contains(const string *key) const;

    FlyMotion(const FlyMotion&);

    static FlyMotion *inst;
    // return &inst;




    Code: ( cpp )

    #include <iostream>
    #include <sstream>
    #include <string>
    #include "FlyMotion.h"

    using namespace std;

    FlyMotion* FlyMotion::inst = 0;

    inst = 0;

    FlyMotion* FlyMotion::instance()

    if(inst == 0)
    inst = new FlyMotion;

    return inst;


    string* FlyMotion::toString() const
    ostringstream outStr;

    outStr << Motion::toString() << "fly " << flush;

    return outStr.str();


    After compiling FlyMotion.cc this is what i get

    Flymotion.cc: In member function 'virtual std::string* FlyMotion::toString() const'
    line30: cannot convert 'std::basic_string<char, std::char_traits<char> std::allocator<char> >' to 'std::string*' in return
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?