Menu

C++ implemented from Java

Lektroluv
2008-10-11
2012-09-26
  • Lektroluv

    Lektroluv - 2008-10-11

    We are learning to program in Java and C++ in school. We have to make a darts game.
    I have programmed and tested it succesfully in Java. Now, I have to implement it in C++.
    I have three files in Dev-Cpp: main.cpp, Darts501.cpp and Darts501.h.
    When I compile my code I get an error: [Build Error] [main.o] Error 1.

    I'm a little bit stuck at this point.
    When I execute the program it should say: Welkom, je start met een score van 501 punten.
    (Sorry for the language difference)
    In other words, the method newGame() should be called.
    The next step would be to insert 3 throws ... then count the amount of points thrown ...

    I dont know how to write this in my main.cpp, which looks like this:

    include <cstdlib>

    include <iostream>

    using namespace std;

    int main(int argc, char *argv[])
    {

    system(&quot;PAUSE&quot;);
    return EXIT_SUCCESS;
    

    }

    ////////////////////Here is my Darts501.cpp:\\\\\\\\\\\\\\\

    include "Darts501.h"

    Darts501::Darts501() {
        newGame();
    }
    
    void Darts501::berekenScore() {
        if (aantalPunten &gt; score) {
            this-&gt;score = score;
            cout &lt;&lt; &quot;Te veel punten gegooid!&quot; &lt;&lt; endl;
        }
        else {
            this-&gt;score = score - aantalPunten;
        }
        cout &lt;&lt; &quot;Je score is &quot; &lt;&lt; score &lt;&lt; endl;
    }
    
    int Darts501::geefAantalPunten() {
        return aantalPunten;    
    }
    
    int Darts501::gooiDriePijltjes(int pijl1, int maal1, int pijl2, int maal2, int pijl3, int maal3) {
    
        worp1 = pijl1 * maal1;
        worp2 = pijl2 * maal2;
        worp3 = pijl3 * maal3;
        this-&gt;maal3 = maal3;
    
        aantalPunten = worp1 + worp2 + worp3;
        cout &lt;&lt; &quot;Je hebt &quot; &lt;&lt; aantalPunten &lt;&lt;&quot; punten gegooid&quot; &lt;&lt; endl;
        berekenScore();
        dubbelUit();
        bull();
        scoreEen();
        spelUit();
    
        return aantalPunten;
    }
    
    bool Darts501::dubbelUit() {
        if (maal3 == 2) {
            dubbelUitOk = true;
            this-&gt;score = score;
        }
        else {
            dubbelUitOk = false;
            this-&gt;score = score;
        }
        return dubbelUitOk;
    }
    
    bool Darts501::bull() {
        if (worp3 == 50) { 
            bullseye = true;
        }
        else {
            bullseye = false;
        }
        return bullseye;
    }
    
    void Darts501::scoreEen() {
        if (score == 1) {
            cout &lt;&lt; &quot;Score is gelijk aan 1 -&gt; onmogelijk dubbel uit te gooien&quot; &lt;&lt; endl;
            this-&gt;score = score + aantalPunten;
            cout &lt;&lt; &quot;Je score is terug herstelt naar &quot; &lt;&lt; score &lt;&lt; &quot; punten&quot; &lt;&lt; endl;        
        }
        else {
            this-&gt;score = score;
        }
    }
    
    bool Darts501::spelUit() {
        if (score == 0) {
            if (dubbelUitOk == true || bullseye == true) {
                goedUitgespeeld = true;
                cout &lt;&lt; &quot;Game Over&quot; &lt;&lt; endl;
            }
            else {
                cout &lt;&lt; &quot;Score is gelijk aan 0 -&gt; laatste worp was niet dubbel of bullseye&quot; &lt;&lt; endl;
                goedUitgespeeld = false;
                this-&gt;score = score + aantalPunten;
                cout &lt;&lt; &quot;Je score is terug herstelt naar &quot; &lt;&lt; score &lt;&lt; &quot; punten&quot; &lt;&lt; endl;  
            }
        }       
        else { 
                goedUitgespeeld = false;
                this-&gt;score = score;
        }
        return goedUitgespeeld;
    }
    
    void Darts501::newGame() {
        score = 501;
        cout &lt;&lt; &quot;Welkom, je start met een score van &quot; &lt;&lt; score &lt;&lt; &quot; punten&quot; &lt;&lt; endl;
    }
    

    }

    ///////////////////////////////And my Darts501.h:\\\\\\\\\\\\\\\\

    /*
    * Write a description of class Darts501 here.
    *
    /
    class Darts501
    {
    protected: int score;
    int worp1, worp2, worp3;
    int aantalPunten;
    int maal3;
    boolean goedUitgespeeld;
    boolean dubbelUitOk;
    boolean bullseye;

    public:
    /**
     * Constructor for objects of class Darts501
     */
    Darts501();
    
    /**
     * Methode berekenScore berekent de huidige score
     * 
     * @return     score    huidige score
     */
    void berekenScore();
    
    /**
     * Methode geefAantalPunten om het aantal gegooide punten te zien
     * 
     * @return  aantalPunten
     */
    int geefAantalPunten();
    
    /**
     * Methode gooiDriePijltjes om een nieuwe worp toe te voegen
     * 
     * @param   pijl1,2,3       punten van pijl1, 2, 3
     *          maal1,2,3       1 voor gewoon, 2 voor dubbel en 3 voor trippel
     * @return  aantalPunten    aantal punten per 3 worpen
     */
    int gooiDriePijltjes(int pijl1, int maal1, int pijl2, int maal2, int pijl3, int maal3);
    
    /**
     * Methode dubbelUit bepaalt of de laatste worp dubbel was
     * 
     * @return  dubbelUitOk   geeft een true terug indien de laatste worp dubbel was, zoniet false 
     */
    boolean dubbelUit();
    
    /**
     * Methode bull bepaalt of de laatste worp bullseye is
     * 
     * @return  bullseye    geeft een true terug indien de laatste worp bullseye was, zoniet false 
     */
    boolean bull();
    
    /**
     * Methode scoreEen controleert of we een score van 1 hebben
     * 
     */
    void scoreEen();
    
    /**
     * Methode spelUit controleert of we het spel correct hebben uitgespeeld
     * 
     * @return  goedUitgespeeld geeft true terug indien we het spel correct uitgespeeld hebben
     * 
     */
    boolean spelUit();
    
    /**
     * Methode om een nieuw spel te starten
     * 
     */
    void newGame();
    

    };

     
    • Lektroluv

      Lektroluv - 2008-10-12

      g++.exe: main.cpp: No such file or directory
      g++.exe: no input files

      make.exe: *** [main.o] Error 1

      Execution completed

       
    • cpns

      cpns - 2008-10-12

      > (I've translated some Dutch words to English!)

      Thanks that is helpful. Post the original as well.

      The error seems self explanatory: "UNC-paths are not supported."

      An UNC path is one like "\PCROBBIE\Public\&quot; rather han a drive letter mapped resource such as "c:\public"

      Navigate to the network share, right click it and select "Map to drive letter", then open your project from the drive letter path rather than the UNC path. You may find that you need to do a "rebuild all" or even delete "makefile.win" to force the regeneration of the file to expunge all UNC references.

      Clifford

       
    • Lektroluv

      Lektroluv - 2008-10-12

      Ok, I've adusted my code, and I can run it.

      I've changed main to:

      include <cstdlib>

      include <iostream>

      include "Darts501.h"

      using namespace std;

      int main(int argc, char *argv[])
      {

      Darts501 spel;
      spel.newGame();
      
      system(&quot;PAUSE&quot;);
      return EXIT_SUCCESS;
      

      }

      The cout problems were solved by adding using namespace std; to Darts501.cpp.

      I will now try to make it run like I want it.

      Thank you for your answers.

       
    • cpns

      cpns - 2008-10-12

      > I've copied my project to my desktop

      Yeuk! Create a folder in the root of the drive. Builds create a number of intermediate files that will now be littering your desktop. Also normally the path to the Desktop contains spaces because it is held within the "Documents and Settings" folder. Spaces in paths can cause problems with both Dev-C++ and the GNU toolchain.

      > The cout problems were solved by adding using namespace std;
      > to Darts501.cpp.

      Really!? Surely you needed to include <iostream> also? I never spotted that because I put all of your code into a single file to save me time and effort.

      > int main(int argc, char *argv[])

      If you are not using the argc/argv parametres you need not include them. Simply:

      int main()

      is fine.

       
    • cpns

      cpns - 2008-10-12

      [Build Error] [main.o] Error 1. Is not the error. That merely means the build failed. The reason for failure will have been shown earlier in the log. You need to read the "Compile Log" text form the top, not from the bottom. Read the first message not the last!

      If you are still unsure, post the text. You are explicitly asked to do this in the "PLEASE READ BEFORE POSTING A QUESTION" thread.

      Your code has a couple of simple coding errors that prevent compilation, and I imagine that is what the compile log will show:

      1) In C++ the boolean data type is just 'bool' not 'boolean'.

      2) You have a stray unmatched } at the end of Dart501.cpp. We cannot of course tell, but a decent indent style would have shown up that problem. You seem to have a mixed style - the opening brace for main() is not placed in teh same manner as your other functions. I'd prefer the style you've used for main() personally - it is easier to visually match braces because they don't vary position depending on line length.

      With regard to starting a new game, merely instantiating a Darts501 object will invoke the constructor which will start a new game:

      include <cstdlib>

      include <iostream>

      using namespace std;

      include "Darts501.h"

      int main(int argc, char *argv[])
      {

      Darts501 game ;

      system("PAUSE");
      return EXIT_SUCCESS;
      }

      The compiler may complain about the 'game' object being declared but unused. So you might remove the newGame() call from teh constructor (or remove teh constructor altogether) and simply do this:

      int main(int argc, char *argv[])
      {

      Darts501 game ;
      game.newGame() ;

      system("PAUSE");
      return EXIT_SUCCESS;
      }

      Clifford

       
    • Lektroluv

      Lektroluv - 2008-10-12

      Thank you for your respons.

      I've changed all the booleans to bool.
      Removed the } in Darts501.cpp.

      Still wont compile (even with your posted main file).

      This is form the log:
      (I've translated some Dutch words to English!)

      Compiler: Default compiler
      Building Makefile: "\PCROBBIE\Public\Robbie\School\SCH_ELO\2008-2009\OCJ\Taak\Dev-C++\Darts\Makefile.win"
      Busy excecuting make...
      make.exe -f "\PCROBBIE\Public\Robbie\School\SCH_ELO\2008-2009\OCJ\Taak\Dev-C++\Darts\Makefile.win" all
      g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

      '\PCROBBIE\Public\Robbie\School\SCH_ELO\2008-2009\OCJ\Taak\Dev-C++\Darts'
      CMD.EXE has started with the path above as active map. UNC-paths are not supported. The standardpreference is the Windows-map. This will now be used.

       
    • Lektroluv

      Lektroluv - 2008-10-12

      Ah, thank you. I've copied my project to my desktop and then it compiles with some more normal errors.

      Darts501.cpp: In member function void Darts501::berekenScore()': Darts501.cpp:10: error:cout' undeclared (first use this function)
      Darts501.cpp:10: error: (Each undeclared identifier is reported only once for each function it appears in.)
      Darts501.cpp:10: error: `endl' undeclared (first use this function)

      Darts501.cpp: In member function int Darts501::gooiDriePijltjes(int, int, int, int, int, int)': Darts501.cpp:30: error:cout' undeclared (first use this function)
      Darts501.cpp:30: error: `endl' undeclared (first use this function)

      Darts501.cpp: In member function void Darts501::scoreEen()': Darts501.cpp:64: error:cout' undeclared (first use this function)
      Darts501.cpp:64: error: endl' undeclared (first use this function) Darts501.cpp: In member functionbool Darts501::spelUit()':
      Darts501.cpp:77: error: cout' undeclared (first use this function) Darts501.cpp:77: error:endl' undeclared (first use this function)
      Darts501.cpp: In member function void Darts501::newGame()': Darts501.cpp:95: error:cout' undeclared (first use this function)
      Darts501.cpp:95: error: `endl' undeclared (first use this function)

      When I remove these cout-lines, the whole program compiles and executes. But no text in bash.

      But now, the thing I don't know how to do it.

      What do I need to type in the main to start a new game?
      It should print this line: cout << "Welkom, je start met een score van " << score << " punten" << endl;

      But then it gives the error that he doesn't know score (undeclared).
      I could say: int score = 501; but I dont think this is necessary because I declared score in Darts501.cpp.

      Can you give me a clear example?

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.