Programming project hints on wrapping integer

Hello all. I am new to the forum. I joined specifically to ask this question, as I stumbled upon here while searching for answers.

I am working on a project for my beginning programming class (this is my first semester) and I am about 80% complete. Something in the instructions has me confused, however and I cannot figure out how to go about this, even using arrays. I am apparently missing that "aha!" moment. If any of you can help me, I would greatly appreciate it. the portion of the problem I am stuck on is this:

"...you will add $7.50 to their bank. Treat one higher than 10 as 1 and 1 lower than 1 as 10. In other words, you wrap the number around if you go one higher than the max or 1 lower than the min. "


The only thing that I can think of is to set up 4 variable (not counting the actual roll) as "RollMax", "RollMin", "RollMaxOne", "RollMinOne",, setting the values to wrap, but I KNOW that is not the actual solution. It's a gambling/chance/dice program, and it's coming along nicely but for this one part...
javascript:PostPreview()
Can anyone help?

(I have my "rand()" seeded off time and running fine)

[Edited the title for clarity]
Last edited on
Sounds like the number is modulus 10.
I used mod 10 in my rand call

1
2
3
4
5
6
int rollDice()
{
   int result = ((rand()%10) +1);
     
   return result;
}


and my game function calls that. The problem I ma having is applying that to user input. If I use modulus, and the user gives me a single digit number, (such as 4), won't it return a "0", as that is the number after the decimal?

Here is my code:

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Paul Wiklund
CSIS 111B 07 May 2011
Project "A Simple Game of Chance"

Write a program that asks the user to guess the next roll of one ten sided die.
*/

#include <iostream>
#include <ctime>//include ctime for srand, rand and time.
#include <iomanip>

using namespace std;

int rollDice();
int game(double UserBankRoll);
double UserRolling(double UserBankRoll);
double UserBankRoll = 20.00;//The player will start out with a $20 bankroll.


int main()
//Your main function should only have srand and a Game function. 
{
   srand((unsigned)time(0));//You should seed the random number generator using the time function.
   game(UserBankRoll);
   return 0;
}
int game(double UserBankRoll)
/*Your program should at least have a Game function and you may create any other functions you need*/
{
   UserBankRoll;
   char UserAnswer;

   if (UserBankRoll >= 1.00)
   {
      cout << "\nWould you like to roll some dice? Press Yes or No." << endl;
      cin >> UserAnswer;

      while (UserAnswer == 'Y' || UserAnswer == 'y')
      /*when you ask the user if he wants to play again you should only play again if the user enters
      a y or Y.*/
      {
         UserRolling(UserBankRoll);
      }

      if (UserAnswer == 'N' || UserAnswer == 'n')
      {  
         cout << "\nThanks for playing. Please come back again soon!" << endl;
         cout << "\nYour total winnings this session were: $" << fixed << setprecision(2) << UserBankRoll - 20.00 << endl;
         /*Print the total winnings or loss at the end of the game.*/
         exit(0);
      }
      else if (UserAnswer != 'Y' && UserAnswer != 'y' && UserAnswer != 'N' && UserAnswer != 'n')
      {
       cout << "\nThat was not a valid statement" << endl;
       game(UserBankRoll);
      }
   }

   else
   {
      cout << "\nYou have insufficient funds to complete this transaction." << endl;
      cout << "Thanks for playing. Please come back again soon!" << endl;
      cout << "\nYour total winnings this session were: $" << fixed << setprecision(2) << UserBankRoll - 20.00 << endl;
      /*Print the total winnings or loss at the end of the game.*/
      main();
   }

   return 0;
}

double UserRolling(double UserBankRoll)
{
   signed int MaxUserVal=10;
   signed int MinUserVal=1;
   signed int ActualUserVal=0;
   signed int result=rollDice();

   if (UserBankRoll >= 01.00 )
   {
      cout << "\nYour bank is $" << fixed << setprecision(2) << UserBankRoll << endl;
      cout << "It costs $1.00 to guess the next roll of a 10 sided die. If you \n";
      cout << "guess correctly, I will pay you $7.50. If you miss the number by \n";
      cout << "one I will pay you $2.00. Enter your guess (1-10): " ;
      cin >> ActualUserVal;  

      cout << "\n";
      
      if (ActualUserVal < MinUserVal || ActualUserVal > MaxUserVal)
      /*When you ask the user for his guess the program should not accept any answer that is 
      outside the 1-10range.*/
      {
         cout << "That was not a valid number.\nThe die cannot roll " << ActualUserVal << "\n";
         UserRolling(UserBankRoll);  
      }

      else
      {

         while (result == 10 && ActualUserVal == 1)
         /*Treat one higher than 10 as 1 and one lower than 1 as 10. In other words you wrap the 
         number around if you go one higher than the max or one lower than the min. */
         {
            cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
            cout << "You won $2.00."<< endl;
            UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
            game(UserBankRoll);
         }

         while (result == 1 && ActualUserVal == 10)
         {
            cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
            cout << "You won $2.00."<< endl;
            UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
            game(UserBankRoll);
         }
         
         if (result == ActualUserVal)
         /*Each time they guess correctly, you add $7.50 to their bank.*/
         {
               cout << "\nYou won $7.50! The die rolled a " << result << endl;
               UserBankRoll = (UserBankRoll-1)+07.50;//Each guess costs $1.
               game(UserBankRoll);
         }

         else if (result == ActualUserVal +1)/*If the user’s guess is one number higher or lower than 
                                             the random number, you add $2 to the bankroll.*/
         { 
            cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
            cout << "You won $2.00."<< endl;
            UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
            game(UserBankRoll);
         }

         else if (result == ActualUserVal -1)
         /*If the user’s guess is one number higher or lower than the random number, you add $2 to the 
         bankroll.*/
         {
            cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
            cout << "You won $2.00."<< endl;
            UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
            game(UserBankRoll);
         }

         else 
         {
            cout << "\nSorry! You were too far off. You predicted " << ActualUserVal  <<  " and you rolled " << result << "." <<endl;
            cout << "You lost $1.00."<< endl;
            UserBankRoll = (UserBankRoll-1);//Each guess costs $1.
            game(UserBankRoll);
         }
      }
   }
    
   return UserBankRoll;
}

int rollDice()
{
   int result = ((rand()%10) +1);
     
   return result;
}

Last edited on
I think you need to post more of the description. Ther phrase you've posted doesn't have enough context for me.
Sure. I can do that. I only posted the snippet because I don't want the entire project done for me. As it is, I have worked a lot of the bugs out of the code already. The only question I am having is integer wrapping. Here is the assignment:


CSIS 111B Programming Project 1

A Simple Game of Chance (250 points)

Write a program that asks the user to guess the next roll of one ten sided die. Each guess costs $1. If they guess correctly, they get $7.50. The player will start out with a $20 bankroll. Each time he (or she) guesses correctly or incorrectly you will subtract $1 from their bankroll. Each time they guess correctly, you add $7.50 to their bank. If the user’s guess is one number higher or lower than the random number, you add $2 to the bankroll. Treat one higher than 10 as 1 and one lower than 1 as 10. In other words you wrap the number around if you go one higher than the max or one lower than the min. Print the total winnings or loss at the end of the game.

Your main function should only have srand and a Game function. Your program should at least have a Game function and you may create any other functions you need. When you ask the user for his guess the program should not accept any answer that is outside the 1-10 range. Likewise when you ask the user if he wants to play again you should only play again if the user enters a y or Y.

Hint: Since the program uses random numbers, include ctime for srand, rand and time. You should seed the random number generator using the time function.

Your program output should look something like this:

Your bank is $20

It costs $1.00 to guess the next roll of a ten sided die. If you

guess correctly I will pay you $7.50. If you miss the number by

one I will pay you $2.00. Enter your guess (1-10): 10



Sorry, you guessed wrong - the die rolled a 5 - you lost $1.00.

Play again? (y/n) y


...



And here is my updated code so far:

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

/*
Paul Wiklund
CSIS 111B 07 May 2011
Project "A Simple Game of Chance"

Write a program that asks the user to guess the next roll of one ten sided die.
*/

#include <iostream>
#include <ctime>//include ctime for srand, rand and time.
#include <iomanip>

using namespace std;

int rollDice();
int game(double UserBankRoll);
double UserRolling(double UserBankRoll);
double UserBankRoll = 20.00;//The player will start out with a $20 bankroll.


int main()
//Your main function should only have srand and a Game function. 
{
   srand((unsigned)time(0));//You should seed the random number generator using the time function.
   game(UserBankRoll);
   return 0;
}
int game(double UserBankRoll)
/*Your program should at least have a Game function and you may create any other functions you need*/
{
   UserBankRoll;
   char UserAnswer;

   if (UserBankRoll >= 1.00)
   {
      cout << "\nWould you like to roll some dice? Press Yes or No." << endl;
      cin >> UserAnswer;

      while (UserAnswer == 'Y' || UserAnswer == 'y')
      /*when you ask the user if he wants to play again you should only play again if the user enters
      a y or Y.*/
      {
         UserRolling(UserBankRoll);
      }

      if (UserAnswer == 'N' || UserAnswer == 'n')
      {  
         cout << "\nThanks for playing. Please come back again soon!" << endl;
         cout << "\nYour total winnings this session were: $" << fixed << setprecision(2) << UserBankRoll - 20.00 << endl;
         /*Print the total winnings or loss at the end of the game.*/
         exit(0);
      }
      else if (UserAnswer != 'Y' && UserAnswer != 'y' && UserAnswer != 'N' && UserAnswer != 'n')
      {
       cout << "\nThat was not a valid statement" << endl;
       game(UserBankRoll);
      }
   }

   else
   {
      cout << "\nYou have insufficient funds to complete this transaction." << endl;
      cout << "Thanks for playing. Please come back again soon!" << endl;
      cout << "\nYour total winnings this session were: $" << fixed << setprecision(2) << UserBankRoll - 20.00 << endl;
      /*Print the total winnings or loss at the end of the game.*/
      main();
   }

   return 0;
}

double UserRolling(double UserBankRoll)
{
   signed int MaxUserVal=10;
   signed int MinUserVal=1;
   signed int ActualUserVal=0;
   signed int result=rollDice();

   if (UserBankRoll >= 01.00 )
   {
      cout << "\nYour bank is $" << fixed << setprecision(2) << UserBankRoll << endl;
      cout << "It costs $1.00 to guess the next roll of a 10 sided die. If you \n";
      cout << "guess correctly, I will pay you $7.50. If you miss the number by \n";
      cout << "one I will pay you $2.00. Enter your guess (1-10): " ;
      cin >> ActualUserVal;  

      cout << "\n";
      
      if (ActualUserVal < MinUserVal || ActualUserVal > MaxUserVal)
      /*When you ask the user for his guess the program should not accept any answer that is 
      outside the 1-10range.*/
      {
         cout << "That was not a valid number.\nThe die cannot roll " << ActualUserVal << "\n";
         UserRolling(UserBankRoll);  
      }

      else
      {

         while (result == 10 && ActualUserVal == 1)
         /*Treat one higher than 10 as 1 and one lower than 1 as 10. In other words you wrap the 
         number around if you go one higher than the max or one lower than the min. */
         {
            cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
            cout << "You won $2.00."<< endl;
            UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
            game(UserBankRoll);
         }

         while (result == 1 && ActualUserVal == 10)
         {
            cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
            cout << "You won $2.00."<< endl;
            UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
            game(UserBankRoll);
         }
         
         if (result == ActualUserVal)
         /*Each time they guess correctly, you add $7.50 to their bank.*/
         {
               cout << "\nYou won $7.50! The die rolled a " << result << endl;
               UserBankRoll = (UserBankRoll-1)+07.50;//Each guess costs $1.
               game(UserBankRoll);
         }

         else if (result == ActualUserVal +1)/*If the user’s guess is one number higher or lower than 
                                             the random number, you add $2 to the bankroll.*/
         { 
            cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
            cout << "You won $2.00."<< endl;
            UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
            game(UserBankRoll);
         }

         else if (result == ActualUserVal -1)
         /*If the user’s guess is one number higher or lower than the random number, you add $2 to the 
         bankroll.*/
         {
            cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
            cout << "You won $2.00."<< endl;
            UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
            game(UserBankRoll);
         }

         else 
         {
            cout << "\nSorry! You were too far off. You predicted " << ActualUserVal  <<  " and you rolled " << result << "." <<endl;
            cout << "You lost $1.00."<< endl;
            UserBankRoll = (UserBankRoll-1);//Each guess costs $1.
            game(UserBankRoll);
         }
      }
   }
    
   return UserBankRoll;
}

int rollDice()
{
   int result = ((rand()%10) +1);
     
   return result;
}




EDIT:

The problem that I am having is that my console display is capable of reading

"Sorry! You were too far off. You predicted a 1 and you rolled a 10.
You lost $1.00."

I can't have that, but I can't find a solution anywhere (though I know there is one or it wouldn't be part of the assignment)
Last edited on
If the user’s guess is one number higher or lower than the random number, you add $2 to the bankroll. Treat one higher than 10 as 1 and one lower than 1 as 10.
With respect to the user guess being within 1 of the dice value, yt's just saying that the next one up from 10 is 1 and the next one down from 1 is 10.
Last edited on
kbw: Thanks, I understand that portion in theory, I am just at a loss as how to implement it in code. The rest of my code is pretty straightforward. I believe I have met all of the other assignment criteria on my own, I am just stuck at the very part that you mentioned.

I did my best at that part with lines 97-111 of the above code. I just can't make the 10 wrap to 1 or the 1 wrap to 10
What about using relational opderators in a 'while' statement?

i.e.:
1
2
3
4
5
6
7
while (result == 10 && ActualUserVal == 1)
{
  cout << "\nYou were close! The die rolled a " << result << " and you guessed" << ActualUserVal << "." <<endl;
   cout << "You won $2.00."<< endl;
   UserBankRoll = (UserBankRoll-1)+2.0;
   game(UserBankRoll);
}


I think that might be the ticket. If I have solved this thing then I am going to mark the query as solved.
That worked. Conditionals with a &&relational operator seem to have done the trick.

I am removing the bulk of my code because it is a project that is worth 1/4 the final grade, and I don't want to carry others through the class. I will re post the code after the project is graded. Than k you KBW for your assistance.
Sorry about taking so long to get back to you.

You solution inadvertently uses recursion

Consider this little app, it does your partial match.
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
#include <iostream>
#include <stdlib.h>

int correct(int x, int max)
{
	if (x == 0)
		return max;

	if (x > max)
		return (x%max);

	return x;
}

bool compare(int result, int x, int max)
{
	if (result == x)
		return true;
	if (result == correct(x + 1, max))
		return true;
	if (result == correct(x - 1, max))
		return true;
	return false;
}

int main()
{
	int x;
	std::cout << "Please enter a number (1 - 10): ";
	std::cin >> x;

	// Display exact and +/-1 matches
	const int max = 10;
	for (int i = 1; i <= max; ++i)
		std::cout << i << " " << x << " " << compare(i, x, max) << std::endl;
	return 0;
}
KBW:

Thank you.

so recursion is where my functions call themselves? ( I had to look that up lol)

Some of the code you posted wasn't within the scope of my class, so there are certain options you used that I didn't know were available...I can't wait to get deeper into this, I really overloaded myself this semester.
My code has been reposted as the projects were submitted yesterday. Thank you KBW for your help with this.
Topic archived. No new replies allowed.