formatting for line with "Game Results: " backwards

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include<ctime> //FOR RAND AND SRAND
#include<cstdlib> //FOR TIME FUNCTION
#include<iomanip>
#include<cmath>
using namespace std;
const int hundo = 100;
const int width = 15;
bool flip;
bool coinFlip();
char ask_play();
char ask_play_again();
int main()
{
    srand((unsigned)time(0));
    int count;
    int heads;
    int tails;
    heads = 0;
    tails = 0;
    cout<<"Welcome to the Coin Flip Game! \n\n";
    cout<<"Here's a 100-Flip Example (T = Tails, H = Heads): \n";
    count = 1;

    while(count <= hundo)
    {
        if(count%34==0)cout<<"\n";
        cout<<" ";
        coinFlip();
        if(flip == true)heads++;
        else
            tails++;
        count++;
    }
    cout<<"\nThere were "<<heads<<" heads and "<<tails<<" tails.\n";
    char play = ask_play();
    float prev_balance=0;
    float game_cost=5.50;
    float new_balance = 0;
    float win;
    float winning;
    cout << fixed << setprecision(2)<<showpoint;
    do {

       if (play == 'y' || play == 'Y') {
            cout << endl;
            cout << setw(width) << left << "Game Results: ";
            cout << setw(width) << right;
            win=0;
            do{
                coinFlip();
                win++;
            }while (flip == false);

            
            cout << endl;
            winning = pow(2, (win - 2));
            new_balance = prev_balance + winning - game_cost;
            cout << setw(width) << left << "Prev. Balance:" << setw(width) << right << prev_balance << "\n";
            cout << setw(width) << left << "Game cost:" << setw(width) << right << game_cost << "\n";
            cout << setw(width) << left << "Winnings:" << setw(width) << right << winning << "\n";
            cout << setw(width) << left << "New Balance: " << setw(width) << right << new_balance << "\n\n";
            prev_balance = new_balance;
            play = ask_play_again();
        }
    }while(play== 'y' || play == 'Y');
    if(prev_balance >= 0)
    cout<<"Thanks for playing! Congratulations on your winnings of "<<prev_balance<<".\n";
    else if(prev_balance == 0)
        cout<<"Thanks for playing! You broke even.\n";
    else if(prev_balance <=0)
    {
        prev_balance = prev_balance * -1;
        cout<<"Thanks for playing! You lost and owe "<<prev_balance<<".\n";
    }
    return 0;
}
bool coinFlip()
{
    int random = (rand()%2)+1;
    if (random == 1)
        flip = true;
    else if(random == 2)
        flip = false;
    if (flip == true)
        cout << "H";  //DISPLAY HEADS IF 1
    else if (flip == false)
        cout << "T";  //DISPLAY TAILS IF 2
    return flip; //END FLIP FUNCTION
}
char ask_play()
{
    char choice = '-';
    do{
        cout<<"\nEach game costs $5.50. We'll flip a coin until the first Head appears, on the nth flip.\n";
        cout<<"Your winnings will be $2^(n-2). Would you like to play a game? (enter Y or y for yes, N or n for no): ";
        cin>>choice;

        if(choice != 'y' && choice != 'Y' && choice != 'n' && choice!= 'N')
            cout<<"Invalid input.  Please try again.  Enter yes or no ('Y' 'y' 'N' or 'n')"<<endl;

    }while(choice != 'y' && choice != 'Y' && choice != 'n' && choice!= 'N');
    return choice;
}
char ask_play_again()
{
    char choice = '-';
    do{
        cout<<"Would you like to play another game for $5.50?";
        cin>>choice;

        if(choice != 'y' && choice != 'Y' && choice != 'n' && choice!= 'N')
            cout<<"Invalid input.  Please try again.  Enter yes or no ('Y' 'y' 'N' or 'n')"<<endl;

    }while(choice != 'y' && choice != 'Y' && choice != 'n' && choice!= 'N');
    return choice;
}
Last edited on
I would like to subtract win (an int value) from width in line 48 to correct formatting issues however it is not properly declared until after the loop runs through line 56.
Last edited on
Since you're not using the return result of coinFlip, and that result is a global anyway, why not make it return something useful.
1
2
3
4
5
6
7
8
char coinFlip()
{
    int random = (rand()%2)+1;
    if (random == 1)
        return 'H';
    else if(random == 2)
        return 'T'
}


Then
1
2
3
4
5
6
string result = "";
char toss;
do{
  toss = coinFlip();
  result += toss;
}while (toss == 'T');

You can do all that before you cout anything at all.
Then you can use result.length() to help you figure out what you need to do with your width variable.
Topic archived. No new replies allowed.