Coin Class Issue. Unable to get correct read out.

Mar 23, 2017 at 3:45am
I am having an issue with my program. The object is for two coins to be flipped simultaneously and to have the user guess beforehand whether the coins will be equal (even) or if the will be different (odd). I have tried several different ways of writing the code and am stuck. Before I tried to move the function getPrediction() out of the do while loop, the program would run twice and quit. Now that I have moved it out of the do while loop I keep getting a ::toss is not specified error. I am trying to get it where I do not alter the class file (coin.hpp) or the coin.cpp (implementation file). I am trying to figure out how to structure main so that this is possible. Thank you for any help you can give me. I appreciate it.







#include <iostream>
#include <random>
#include <string>
#include <ctime>

#include "coin.hpp"

using namespace std;

// Input and verify character from list of characers
char inputOption(string prompt, string options, bool lower=true);
string getPrediction();


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

// Display title
cout << "Toss Two Coins Simultaneously" << endl;
cout << "-----------------------------" << endl;

// Set random number generator seed
srand(time(0));

// Create instance of Coin class
Coin coin1;
Coin coin2;
int timesCorrect = 0;
int result;
string guess;

getPrediction();
do {
coin1.tossCoin();
coin2.tossCoin();


cout << "Coin #1 side up is " << coin1.getSideUp() << ". Coin #2 side up is" << coin2.getSideUp() << "." << endl;
cout << "You predicted " << getPrediction() << "Your prediction was " << guess << endl;
cout << "Out of " << coin1.getTossCount() << " predictions, " << timesCorrect << " have been correct." << endl;
}
while (toss != 'q');
return 0;
}

string getPrediction() {
char toss;
string prediction;
toss = inputOption("Toss the coin. Predict Odd, Even or Quit (O/E/Q)? ", "oOeEqQ");
if (toss == 'e'){
prediction = 'Even';

}
else {
prediction = 'Odd';
}
return prediction, toss;
}


// Output prompt and clear buffer
void outputPrompt(string prompt) {
cout << prompt;
cin.clear();
cin.sync();
}

// Input and verify character option
char inputOption(string prompt, string options, bool lower) {
outputPrompt(prompt);
char result = cin.get();
while(options.find_first_of(result) == string::npos) {
cout << "Please re-enter. ";
outputPrompt(prompt);
result = cin.get();
}
return lower == true ? tolower(result) : result;
}


Here is my coin.cpp file:

/* Coin class implementation */

#include "coin.hpp"
#include <random>

// Toss the coin and update member variables
void Coin::tossCoin() {
if(rand() % 2) {
sideUp="Heads";
headsCount++;
}
else {
sideUp="Tails";
tailsCount++;
}
}

and my coin.hpp file with the class definition:

#ifndef COIN_H
#define COIN_H

#include <string>

using namespace std;

/* Coin class declaration */

class Coin {
private:
int headsCount;
int tailsCount;
string sideUp;
public:
// Constructor
Coin() {
headsCount = 0;
tailsCount = 0;
sideUp = "";
}
// Getters
int getHeadsCount() {
return headsCount;
}
int getTailsCount() {
return tailsCount;
}
int getTossCount() {
return headsCount + tailsCount;
}
string getSideUp() {
return sideUp;
}
// Toss the coin and update member variables
void tossCoin();
};
#endif
Mar 23, 2017 at 4:39am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <random>
#include <string>
#include <ctime>

#include "coin.hpp"

using namespace std;

// Input and verify character from list of characers
char inputOption(string prompt, string options, bool lower=true);
string getPrediction();


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

// Display title
cout << "Toss Two Coins Simultaneously" << endl;
cout << "-----------------------------" << endl;

// Set random number generator seed
srand(time(0));

// Create instance of Coin class
Coin coin1;
Coin coin2;
int timesCorrect = 0;
int result;
string guess;

getPrediction();
do {	
coin1.tossCoin();	
coin2.tossCoin();


cout << "Coin #1 side up is " << coin1.getSideUp() << ". Coin #2 side up is" << coin2.getSideUp() << "." << endl;
cout << "You predicted " << getPrediction() << "Your prediction was " << guess << endl;
cout << "Out of " << coin1.getTossCount() << " predictions, " << timesCorrect << " have been correct." << endl;
}
while (toss != 'q');
return 0;
}

string getPrediction() {
char toss;
string prediction;
toss = inputOption("Toss the coin. Predict Odd, Even or Quit (O/E/Q)? ", "oOeEqQ");
if (toss == 'e'){
prediction = 'Even';

}
else {
prediction = 'Odd';
}
return prediction, toss;
}


// Output prompt and clear buffer
void outputPrompt(string prompt) {
cout << prompt;
cin.clear();
cin.sync();
}

// Input and verify character option
char inputOption(string prompt, string options, bool lower) {
outputPrompt(prompt);
char result = cin.get();
while(options.find_first_of(result) == string::npos) {
cout << "Please re-enter. ";
outputPrompt(prompt);
result = cin.get();
}
return lower == true ? tolower(result) : result;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "coin.hpp"
#include <random>

// Toss the coin and update member variables
void Coin::tossCoin() {
if(rand() % 2) {
sideUp="Heads";	
headsCount++;
}
else {
sideUp="Tails"; 
tailsCount++;
}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef COIN_H
#define COIN_H

#include <string>

using namespace std;

/* Coin class declaration */

class Coin {
private:
int headsCount;
int tailsCount;
string sideUp;
public:
// Constructor
Coin() {
headsCount = 0;
tailsCount = 0;
sideUp = "";
}
// Getters
int getHeadsCount() {
return headsCount;
}
int getTailsCount() {
return tailsCount;
}
int getTossCount() {
return headsCount + tailsCount;
}
string getSideUp() {
return sideUp;
}
// Toss the coin and update member variables
void tossCoin(); 
};
#endif 


getPrediction() should be in the loop but why not use a while loop instead of a do while loop?


Last edited on Mar 23, 2017 at 4:42am
Mar 25, 2017 at 10:48pm
I am not sure. I am fairly new to C++. Thank you. Would it work better with a while loop?
Mar 25, 2017 at 11:12pm
Okay so I put getPrediction in the loop but the loop exits after one time? I am not seeing what I am doing wrong.
Mar 26, 2017 at 12:46pm
modelling heads and tails as two numbers 1 and 2, the following program captures correct and incorrect guesses for two coins showing heads and tails simultaneously:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <random>
#include <chrono>

int main()
{
    bool fQuit = false;
    int correctChoice{}, wrongChoice{}, runs{};

    while (!fQuit)
    {
        std::cout << "Select 1 to guess, 2 to quit \n";
        int choice{};
        std::cin >> choice;//do input validation here
        switch(choice)
        {
            case 1:
            {
                std::cout << "Select 1 for both coins same, 2 for different \n";
                int choice{};
                std::cin >> choice;//do input validation here

                auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
                std::default_random_engine dre(seed);//engine
                std::uniform_int_distribution<int> di(1, 2);//distribution

                auto a = di(dre); auto b = di(dre);//generates numbers 1 and 2 randomly

                if (((choice == 1) && (a == b)) || ((choice == 2) && (a != b)))
                //correct guess: when user choooses same && both coins are same ...
                //... or when user chooses different && both coins are not same
                {
                    std::cout << "Correct guess \n";
                    ++correctChoice;
                }
                else
                {
                    std::cout << "Wrong guess \n";
                    ++wrongChoice;
                }
                ++runs;
            }
            break;
            case 2:

                std::cout << "You played " << runs << " times \n";
                std::cout << "Correct choices " << correctChoice;
                std::cout << "\nWrong choices " << wrongChoice;
                std::cout << "Goodbye"
                fQuit = true;
            break;
            default:
                
                std::cout << "Wrong choice, try again \n";
                break;
        }
    }
}
Mar 26, 2017 at 1:20pm
closed account (48T7M4Gy)
Here's another way to demonstrate how you can structure the coin toss with a simple array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <random>
#include <string>

int main()
{
    std::string outcome[]{"HH", "TH", "TT", "HT"}; // <---
    
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<> dis(0,3);
    
    int guess = 0;
    int toss = 0;
    
    for (int n=0; n<10; ++n){
        std::cout << "Enter your guess < 0 = even, 1 = odd > ";
        std::cin >> guess;
        
        toss = dis(gen);
        
        std::cout << outcome[toss] << ' ';
        if(toss % 2 == guess)
            std::cout << "You win\n";
        else
            std::cout << "You lose\n";
        
    }
    
    std::cout << '\n';
    
    return 0;
}


http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
Last edited on Mar 26, 2017 at 1:21pm
Mar 26, 2017 at 11:07pm
Thank you very much I appreciate the assistance.
Topic archived. No new replies allowed.