I have trouble saving my numbers. I used an array for my saving feature. As you play the game, the computer asks you if you want to save a number. you say yes and you pick which number(s) you wish to save. My problem is, if i save 2 numbers the first time and then re roll my die, and then choose to save 2 other numbers, the program somehow says that I have saved 5 numbers instead of 4. Can anyone help me?? I need this help so i can pass my c++ programming I class.

// This is my header
#include <iostream> 
#include <string>
#include <algorithm>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <dos.h>
using namespace std;
void gotoxy(int xpos, int ypos)
{
  COORD scrn;
  HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
  scrn.X = xpos; scrn.Y = ypos;
  SetConsoleCursorPosition(hOuput,scrn);
}
 

#define _WIN32_WINNT 0x0500

bool SetWindow(int Width, int Height);
bool SetWindow(int Width, int Height)
{
	_COORD coord;
	coord.X = Width;
	coord.Y = Height;

	_SMALL_RECT Rect;
	Rect.Top = 0;
	Rect.Left = 0;
	Rect.Bottom = Height - 1;
	Rect.Right = Width - 1;

	// Get handle of the standard output
	HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);
	if (Handle == NULL)
	{
		cout<<"Failure in getting the handle\n"<<GetLastError();
		return FALSE;
	}

	// Set screen buffer size to that specified in coord
	if(!SetConsoleScreenBufferSize(Handle, coord))
	{
		cout<<"Failure in setting buffer size\n"<<GetLastError();
		return FALSE;
	}

	// Set the window size to that specified in Rect
	if(!SetConsoleWindowInfo(Handle, TRUE, &Rect))
	{
		cout<<"Failure in setting window size\n"<<GetLastError();
		return FALSE;
	}

	return TRUE;
}



// This is my main
#include "Constants_and_Re_Sizing_Board.h"
char numbersave = 'y';
char rollAgain = 'y';
char savenum = 'y';
const int LOW = 1, HIGH = 6;
int Hold[5]; // Array that the user can save his number too
int die[5]; // Random numbers array
int choice;

void Welcome_Screen()
{
 cout << "                 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ " << endl;
 cout << endl;     
 cout << "                            Yahtzee Scorecard Edition " << endl;
 cout << endl;
 cout << "                   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ " << endl;
 cout << endl; 
 cout << endl;
 cout << endl;
 cout << "Made by Zvjezdan23" << endl;
 cout << endl;
 cout << endl;
 system("pause");
}
void Board()
{
 string name, letter;
 int randomnumber = rand();
 system("cls");
 cout << "=====================================================================" << endl;
 cout << endl;
 cout << "                Player Scorecard" << endl;
 cout << endl;
 cout << "=====================================================================" << endl;
 cout << endl;
 gotoxy(18,5);
 cout << "Type in your name: ";
 getline(cin,name);
 cout << endl;
 cout << "Enter d to roll the die: ";
 cin >> letter;
 cout << endl;
 cout << "_____________________________________________________________________" << endl;
 cout << endl;
}


int fifth_case_roll_5_Die() // Randomizes 5 numbers from 1 - 6
{
    die[0] = rand() % (HIGH - LOW + 1) + LOW; 
    die[1] = rand() % (HIGH - LOW + 1) + LOW; 
    die[2] = rand() % (HIGH - LOW + 1) + LOW; 
    die[3] = rand() % (HIGH - LOW + 1) + LOW; 
    die[4] = rand() % (HIGH - LOW + 1) + LOW; 
	cout << endl;
	cout << "_____________________________________________________________________" << endl;
	cout << "You rolled a: " << die[0] << endl;
	cout << "You rolled a: " << die[1] << endl;
	cout << "You rolled a: " << die[2] << endl;
	cout << "You rolled a: " << die[3] << endl;
	cout << "You rolled a: " << die[4] << endl;
	cout << endl;
	cout << "_____________________________________________________________________" << endl;
	cout << endl;
	return 0;
}
int fourth_case_roll_4_Die() // Randomizes 4 numbers from 1 - 6
{
    die[0] = rand() % (HIGH - LOW + 1) + LOW; 
    die[1] = rand() % (HIGH - LOW + 1) + LOW; 
    die[2] = rand() % (HIGH - LOW + 1) + LOW; 
    die[3] = rand() % (HIGH - LOW + 1) + LOW; 
	cout << endl;
	cout << "_____________________________________________________________________" << endl;
	cout << endl;
	cout << "You rolled a: " << die[0] << endl;
	cout << "You rolled a: " << die[1] << endl;
	cout << "You rolled a: " << die[2] << endl;
	cout << "You rolled a: " << die[3] << endl;
	cout << endl;
	cout << "_____________________________________________________________________" << endl;
	cout << endl;
	return 0;
}
int third_case_roll_3_Die() // Randomizes 3 numbers from 1 - 6
{
    die[0] = rand() % (HIGH - LOW + 1) + LOW; 
    die[1] = rand() % (HIGH - LOW + 1) + LOW; 
    die[2] = rand() % (HIGH - LOW + 1) + LOW; 
	cout << endl;
	cout << "_____________________________________________________________________" << endl;
	cout << endl;
	cout << "You rolled a: " << die[0] << endl;
	cout << "You rolled a: " << die[1] << endl;
	cout << "You rolled a: " << die[2] << endl;
	cout << endl;
	cout << "_____________________________________________________________________" << endl;
	cout << endl;
	return 0;
}
int second_case_roll_2_Die() // Randomizes 2 numbers from 1 - 6
{
    die[0] = rand() % (HIGH - LOW + 1) + LOW; 
    die[1] = rand() % (HIGH - LOW + 1) + LOW; 
	cout << endl;
	cout << "_____________________________________________________________________" << endl;
	cout << endl;
	cout << "You rolled a: " << die[0] << endl;
	cout << "You rolled a: " << die[1] << endl;
	cout << endl;
	cout << "_____________________________________________________________________" << endl;
	cout << endl;
 return 0;
}
int first_case_roll_1_Die() // Randomizes a random number from 1 - 6
{
	die[0] = rand() % (HIGH - LOW + 1) + LOW;
	cout << endl;
	cout << "_____________________________________________________________________" << endl;
	cout << "You rolled a: " << die[0] << endl;
	cout << endl;
	cout << "_____________________________________________________________________" << endl;
	cout << endl;
	return 0;
}


void Roll_Again_With_Features()
{
	// Your first set of random numbers when you start Yahtzee

    die[0] = rand() % (HIGH - LOW + 1) + LOW; 
    die[1] = rand() % (HIGH - LOW + 1) + LOW; 
    die[2] = rand() % (HIGH - LOW + 1) + LOW; 
    die[3] = rand() % (HIGH - LOW + 1) + LOW; 
    die[4] = rand() % (HIGH - LOW + 1) + LOW; 
	cout << endl;
	cout << "You rolled a: " << die[0] << endl;
	cout << "You rolled a: " << die[1] << endl;
	cout << "You rolled a: " << die[2] << endl;
	cout << "You rolled a: " << die[3] << endl;
	cout << "You rolled a: " << die[4] << endl;
	cout << "_____________________________________________________________________" << endl;
	cout << endl;
	 
	// Loop where the user can choose to save a number or choose how many die he or she wants to re roll
	do
	 {
		 cout << "Roll Again?? (y/n): ";
		 cin >> rollAgain;
		 cout << endl;
 
		 if (rollAgain != 'y')
		 {
			cout << "Do you want to save any numbers? (y/n): ";
			cin >> savenum;
			if (savenum != 'y')
			{
				cout << endl;
				cout << "End of Turn" << endl;
			}
			else if (savenum == 'y')
			{
				cout << "What number do you want to save? (1-6): ";
				cin >> Hold[0]; // 1st number the user chooses to save
				cout << endl;
				cout << "Do you want to save any other number? (y/n): ";
				cin >> numbersave;
				if (numbersave != 'y')
				{
				cout << endl;
				}
				else if (numbersave == 'y')
				 {
					 cout << "What number do you want to save? (1-6): ";
					 cin >> Hold[1]; // 2nd number the user chooses to save
					 cout << endl;
				 }
				 cout << "Save another number? (y/n): ";
				 cin >> savenum;
				 if (savenum != 'y')
				  {
					 cout << endl;
				  }
				 else if (savenum == 'y')
				 {
					 cout << "What number do you want to save? (1-6): ";
					 cin >> Hold[2]; // 3rd number the user chooses to save
					 cout << endl;
					 cout << "Do you want to save another? (y/n): ";
					 cin >> numbersave;
					 if (numbersave != 'y')
					  {
					  cout << endl;
					  }
					  else if (numbersave == 'y')
						{
							cout << "What number do you want to save? (1-6): ";
							cin >> Hold[3]; // 4th number the user chooses to save
							cout << endl;
						}
				 }
				cout << "Save another number? (y/n): ";
				cin >> savenum;
				if (savenum != 'y')
				{
					cout << "Your Die are: " << Hold[0] << " , " << Hold[1] << " , " << Hold[2] << " , " << Hold[3] << endl; // Outputs what numbers you have
				}
				 else if (savenum == 'y')
				 {
				 cout << "What number do you want to save? (1-6): ";
				 cin >> Hold[4];
				 cout << endl;
				 cout << "Your Die are: " << Hold[0] << " , " << Hold[1] << " , " << Hold[2] << " , " << Hold[3] << " , " << Hold[4] << endl; // Outputs the numbers you chose to save
				 }
				}
			}
			 else if (rollAgain == 'y')
			{
			cout << "Do you want to save any numbers before you re roll? (y/n): ";
			cin >> savenum;
			if (savenum != 'y')
			 {
				cout << "How many die do you want to re roll? ";
				cin >> choice;
				switch (choice)
				 {
					case 1: 
						 first_case_roll_1_Die();
						 break;
					case 2: 
						 second_case_roll_2_Die();
						 break;
					case 3:
						 third_case_roll_3_Die();
						 break;
					case 4:
						 fourth_case_roll_4_Die();
						 break;
					case 5:
						 fifth_case_roll_5_Die();
						 break;
				 }
   }
   else if (savenum == 'y')
   {
    cout << "What number do you want to save? (1-6): ";
    cin >> Hold[0]; // 1st number the user chooses to save
    cout << endl;
    cout << "Do you want to save any other number? (y/n): ";
    cin >> numbersave;
    if (numbersave != 'y')
    {
		cout << "How many die do you want to re roll? ";
		cin >> choice;
		switch (choice)
		{
		 case 1: 
			  first_case_roll_1_Die();
			  break;
		 case 2: 
			  second_case_roll_2_Die();
			  break;
		 case 3:
			  third_case_roll_3_Die();
			  break;
		 case 4:
			  fourth_case_roll_4_Die();
			  break;
		 case 5:
			  fifth_case_roll_5_Die();
			  break;
		 }
    }
    else if (numbersave == 'y')
    {
     cout << "What number do you want to save? (1-6): ";
     cin >> Hold[1]; // 2nd number the user chooses to save
     cout << endl;
    }
    cout << "Save another number?? (y/n): ";
    cin >> numbersave;
    if (numbersave != 'y')
    {
     cout << "How many die do you want to re roll? ";
     cin >> choice;
     switch (choice)
     {
     	  case 1: 
			  first_case_roll_1_Die();
			  break;
		 case 2: 
			  second_case_roll_2_Die();
			  break;
		 case 3:
			  third_case_roll_3_Die();
			  break;
		 case 4:
			  fourth_case_roll_4_Die();
			  break;
		 case 5:
			  fifth_case_roll_5_Die();
			  break;
     }
    }
    else if (numbersave == 'y')
    {
     cout << "What number do you want to save? (1-6): ";
     cin >> Hold[2]; // 3rd number the user chooses to save
     cout << endl;
    }
    cout << "Do you want to save any other number? (y/n): ";
    cin >> numbersave;
    if (numbersave != 'y')
    {
     cout << "How many die do you want to re roll? ";
     cin >> choice;
     switch (choice)
     {
     	  case 1: 
			  first_case_roll_1_Die();
			  break;
		 case 2: 
			  second_case_roll_2_Die();
			  break;
		 case 3:
			  third_case_roll_3_Die();
			  break;
		 case 4:
			  fourth_case_roll_4_Die();
			  break;
		 case 5:
			  fifth_case_roll_5_Die();
			  break;
     }
    }
    else if (numbersave == 'y')
    {
     cout << "What number do you want to save? (1-6): ";
     cin >> Hold[3]; // 4th number the user chooses to save
     cout << endl;
    }
    cout << "Save another number?? (y/n): ";
    cin >> numbersave;
    if (numbersave != 'y')
    {
     cout << "How many die do you want to re roll? ";
     cin >> choice;
     switch (choice)
     {
     	  case 1: 
			  first_case_roll_1_Die();
			  break;
		 case 2: 
			  second_case_roll_2_Die();
			  break;
		 case 3:
			  third_case_roll_3_Die();
			  break;
		 case 4:
			  fourth_case_roll_4_Die();
			  break;
		 case 5:
			  fifth_case_roll_5_Die();
			  break;
     }
    }
    else if (numbersave == 'y')
    {
     cout << "What number do you want to save? (1-6): ";
     cin >> Hold[4]; // 5th number the user chooses to save
     cout << endl;
     cout << "Your Die are: " << Hold[0] << " , " << Hold[1] << " , " << Hold[2] << " , " << Hold[3] << " , " << Hold[4] << endl; // Outputs the numbers the user chose to save
    }
   }
   }
 } while (rollAgain == 'y');
 cout << endl;
}
int main()
{
 srand(time(0));

 SetWindow(100,50);

 Welcome_Screen();
 Board();
 Roll_Again_With_Features();
 system("pause");
 return 0;
}

Recommended Answers

All 2 Replies

Would you mind explaining how you 'save' numbers? It's so much faster than trying to understand 452 lines of code.

SOrry I can not read you whole code next time please just post the problematic code.

What I think your problem is that you declare an array and then you either add an extra integer somewhere. Please post your segment of code!
Hope this helps...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.