0

I am writing a program that is like a guessing die and card game. It simulates the user rolling a die, and based on what they toll a card is generated that is equivalent to the amount of points they received in that round based. For example, rolling 1 has a card that says the user caught a big fish, and earned 20 points. The problem that I have is incrementing points and keeping a running total. The program only displays the die number rolled, as opposed to how many points were earned that round. It also does not print the correct final total. I have been tinkering with it for a few days, which leads me to believe I am making a simple mistake. Please help. My code is pasted below.

    #include<iostream>
#include<cstdlib>

class Dice{
    private: 
        int dicevalue;
    public:
        int rollDice(){
         int ran = rand() %6 +1;
        dicevalue = ran;
         return dicevalue;
           }
};

class Points{

    public:
        int getpoints(int dicevalue){
            switch(dicevalue){
                case 1:
                    printf("\nYou won %d points\n",dicevalue);
                    return 1;
                    break;
                case 2:
                    printf("\nYou won %d points\n",dicevalue);
                    return 2;
                    break;
                case 3:
                    printf("\nYou won %d points\n",dicevalue);
                    return 3;
                    break;
                case 4:
                    printf("\nYou won %d points\n",dicevalue);
                    return 4;
                    break;
                case 5:
                    printf("\nYou won %d points\n",dicevalue);
                    return 5;
                    break;
                case 6:
                    printf("\nYou won %d points\n",dicevalue);
                    return 6;
                    break;
            }

            return -1;
        }
};

class Game{
    private:
        int total=0;
        Points points;
        Dice dice;

    public:
        int playgame(){
            int con;
            do{
            int dicevalue = dice.rollDice();
            int points_to_add=points.getpoints(dicevalue);
            total = total + points_to_add;

            printf("\nif you want one more term press 1 or 0 : ");
            scanf("%d",&con);   
        }while(con==1);

        return total;
        }


};

int main(){
    Game game;
    printf("\ntotal points are %d",game.playgame());
    return 0;
}
Tiffany
  • 17
  • 4
  • Updated code ( posted wrong snippet) – Tiffany Oct 21 '15 at 15:42
  • I would remove the `switch` statement in `getpoints` and replace with `if` statement: `if (dice_value < 7) return dice_value; else return -1;`. – Thomas Matthews Oct 21 '15 at 15:42
  • BTW, you don't need a class to hold functions, functions can be free-standing. I suggest moving the `getpoints` function into the `game` class. – Thomas Matthews Oct 21 '15 at 15:47
  • 1
    To me it looks like you have abused classes, dice and points classes are unnecessary and makes the code ugly and cluttered. – user5159806 Oct 21 '15 at 15:48
  • 1
    When you used the debugger, what statement is causing the issue? If you don't know how to use a debugger, this is an excellent opportunity. – Thomas Matthews Oct 21 '15 at 15:48
  • Thanks everyone! I'm working things out. Will post updated code shortly. Also, I think all the classes are overkill as well, but this is how my professor wants it for this assignment. – Tiffany Oct 21 '15 at 17:32

2 Answers2

0

The strange behaviour you experience is related to your use of the old C functions printf and scanf.

In particular, consider these two lines here:

        printf("\nif you want one more term press 1 or 0 : ");
        scanf("%d",&con);   

The output of printf is buffered, i.e. there is no guarantee that it's immediately written to the console or wherever the program output is directed. Indeed, when scanf prompts the user for input, the output may still not have been written, which obviously results in poor user experience! The user enters something and only then sees the line which should have been visible all along...

You could use fflush(stdout) as a workaround to force flushing of the buffer. But why should you? If you use C++ I/O with std::cout and std::cin, then you do not encounter this problem, because reading from std::cin automatically flushes the output buffer[*]:

        std::cout << "\nif you want one more term press 1 or 0 : ";
        std::cin >> con;

This is a wonderful example of how using standard C++ mechanisms automatically prevents you from dealing with low-level problems which a beginner should not even be aware of.

I believe all other errors you are describing were just a wrong interpretation on your part of inconsistent program output, because the program works perfectly for me if I replace all printf/scanf calls with std::cout/std::cin.


[*] The technical background for this feature to work is that std::cin is said to be tied to std::cout.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
-1

Add

using namespace std;

at the beginning of your code so that we can compile it without errors.

It seems to me that your code does exactly what it is supposed to.

In the function getpoints(), you always return the same value as dicevalue, (eg case 1: ... return 1;) So it is equivalent to

  int getpoints(int dicevalue){
      if (dicevalue < 7) {
        printf("\nYou won %d points\n",dicevalue);
        return dicevalue;
      }
      else return -1;
  }

I changed it so that the number of points would be dicevalue + 10 and it worked fine:

  int getpoints(int dicevalue){
      if (dicevalue < 7) {
        int numberPoints = dicevalue+10;
        printf("\nYou won %d points\n",numberPoints);
        return numberPoints;
      }
      else return -1;
  } 

And at the end, I always get "total point are " the sum of the points I earned in the rounds.

You should seed your random numbers using time, put this at the beginning of the main function:

srand(time(NULL));

As it is, I get the same serie of numbers whenever I launch the program.

Hope this helps.

Dese
  • 338
  • 1
  • 12
  • And "using namespace std;" is bad practice apparently: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Dese Oct 21 '15 at 16:12
  • so you could instead write std::printf and std::scanf (which is what didn't compile for me), which seems to solve the problem that you had. Of course, std::cout and std::cin are better. And I maintain what I said about seeding your random numbers. – Dese Oct 22 '15 at 11:08