Hi again, since I last posted I have got a much better understanding, and my program is coming along. I am making a lottery program for college, and I have to demonstrate use of functions and arrays, and I have read that it is wise to use NO global variables where possible. I have a few simple global variables as I know they will not change, but I am trying to keep everything local within functions.
My problem is, I am trying to return a 2D array from a function. This is necessary to me as I need to keep track of what line of the player's ticket a number is, up to 10 lines per ticket, as well as it's cell, 1-6, and I will later need to compare the 6 cells on each of up to 10 lines with the randomly generated numbers. I have read it is possible to persist the local 2D array in memory so it can be accessed in other functions. I only CHANGE the values from within it's own function, but I need to compare the array with the random generated balls in a separate function. I have seen a couple of examples of returning a small 1D or 2D array as the result of a function, however I cannot seem to get it to work for me. All this talk of pointers and whatnot...
below is my entire code, the problem is in the Input function, if I remove the asterisks and the return line, it functions as desired, however, as I said, I need to then get the array OUT of the function so I can use it elsewhere.
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 168 169 170 171 172 173 174
|
#include <string>
#include <iostream>
using namespace std;
const int BetLow (1); //used to make sure guesses
const int BetHigh (49); //and balls are within range
char Pound = 156; //to use pound symbol in cout
unsigned int Wallet (50); //Global wallet amount, only affected after user inputs data on all lines (removes £1 for each line) and when collecting winnings, if any
int Payout3 (10);
int Payout4 (100);
int Payout5 (2500);
int PayoutB (190000);
int Payout6 (4000000);
bool running = true;
//----------------------------------------ENTER NAME--------------------------------------------
string EnterName()
{
string Name;
cout << "Please enter your name:\n";
getline (cin, Name);
return (Name);
}
//----------------------------------------CHOOSE NUMBER OF LINES--------------------------------
int Ticket()
{
int Lines = 0; //set number of lines to default of 0 upon entering this function
do
{
cout << "\nHow many lines would you like to play with? (1-10)\n";
cin >> Lines;
if(Lines < 1 || Lines > 10) //feedback to player if they pick less than 1 or more than 10 lines
{
cout << "Must be between 1 and 10!\n\n";
}
if(Lines > Wallet && Wallet > 1) //feedback if player cannot afford this many lines
{
cout << "You do not have enough money for this many lines! You can afford up to " << Wallet << " lines!\n\n";
}
if(Lines > Wallet && Wallet == 1) //as above, correcting pluralisation for just £1
{
cout << "You do not have enough money for this many lines! You can only afford " << Wallet << " line!\n\n";
}
}
while (Lines < 1 || Lines > 10 || Lines > Wallet);
return Lines;
}
//----------------------------------------CHOOSE SIX NUMBERS PER LINE, BUBBLE SORT, AND PRINT---
int *Input(int Lines)
{
cout << "\n\nPlease choose your 6 lucky numbers: (1-49)\n";
int *Guess[Lines][6]; //sets 2D array, where Lines is defined by user, and 6 is the amount of guesses on each line
int Line = 0;
for(int n = 0; n < 6; n++) //input loop (still need to allow multiple lines, and check for duplicates!)
{
do
{
cout << n + 1 << ")\t";
cin >> *Guess[Line][n];
if (*Guess[Line][n] < BetLow)
{
cout <<"Too low, try again! (1-49)\n";
}
else if (*Guess[Line][n] > BetHigh)
{
cout << "Too high, try again! (1-49)\n";
}
}
while(*Guess[Line][n] < BetLow || *Guess[Line][n] > BetHigh);
}
cout << "You picked:\n";
for(int n = 0; n < 6; n++) //output loop, prints all data cells (need to bubble sort these per line!)
{
cout << Guess[Line][n] << "\t";
}
return Guess;
}
//----------------------------------------RANDOM NUMBER GENERATOR-------------------------------
void Generate()
{
}
//----------------------------------------COMPARE RANDOM BALLS WITH GUESSES PER LINE------------
int Compare()
{
}
//----------------------------------------PAYOUT------------------------------------------------
void Payout()
{
}
//----------------------------------------MAIN (LOOP FROM NUMBER OF LINES TO PAYOUT)------------
int main ()
{
string Name = EnterName();
//start game by asking for player's name
cout << "\nWelcome, " << Name << "!\t\tYou have " << Pound << Wallet << " in your wallet to play with!\n\n";
//------------------------------------MAIN LOOP---------------------------------------------
do
{
int Lines = Ticket();
Input(Lines);
Wallet = Wallet - Lines; //subtract £1 for each line played
cout << "You have " << Pound << Wallet << " left in your wallet.";
//Generate();
//Compare();
//Payout();
//end game when run out of money
/*if(Wallet <= 0)
{
running = false;
}*/
running = false;
}
while(running==true);
cout << "\n\n\nYou ran out of money. Thanks for playing!\n";
return 0;
}
|
Thanks in advance, hopefully this is a simple fix, I know I am
almost there, but I don't understand all this pointers nonsense, I just want the return value of the function to BE the array that is generated within it.
Thanks again!
(the error refers to line 100 in case you cannot find it at first glance)