问题
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;
}
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/33263523/how-to-execute-correct-running-total-using-class-objects