#include <iostream>
#include <fstream>
#include <string>
#include<ctime> //for rand and srand
#include<cstdlib> //for time function
#include<iomanip> //Formatting
#include<cmath> //Pow function
#include<vector> // to display Game results properly
double populateArray(double* arr, int);
double probability[]={};
usingnamespace std;
double main2();
int sides =0;
double totalweight =0;
void rollTime(int x, double y ,double *arr);
int main()
{
int switch2=0;
int switch3 =0;
int choice=0;
do {
cout << "Choose an option:\n\n";
cout << "Option: 1: Enter sides and weights for a die \n";
cout << "Option: 2: Specify output file \n";
cout << "Option: 3: Simulate rolls \n";
cout << "Option: 4: Exit\n\n";
cout << "Your choice: ";
cin >> choice;
if(choice == 1)
{
main2();
switch2=1;
}
elseif(choice == 2)
{
string filename;
ofstream outputFile;//steam output file
cout<<"What is the name of the file would you like to output to?";
// /Users/toddi/CLionProjects/untitled1/save.txt
cin >> filename;
switch3=1;
}
elseif(choice == 3 && switch2==0 || switch3==0)
{
cout<<"You must select options 1 and 2 first; cancelling… \n";
}
elseif(choice == 3 && switch2==1 && switch3 ==1)
{
cout<<"Simulate rolls\n";
cout<<sides<<"\n";
cout<<totalweight<<"\n";
double* arr = newdouble[sides];
rollTime( sides, totalweight, arr);
}
}while(choice!=4);
return 0;
}
double main2()
{
srand((unsigned)time(0));//seed random
cout<<endl;
cout<<"Enter the number of sides ( >= 2 ): ";
cin>>sides;
double* arr = newdouble[sides];
populateArray(arr,sides);
//for ( int i = 0; i < sides; i++)
//{
//cout <<"The weight of side "<< i +1 <<" is : "<<*(arr + i)<<endl;
//}
for ( int i = 0; i < sides; i++)
{
totalweight = totalweight + *(arr + i);
}
// cout <<"The total weight of sides "<< totalweight <<endl;
// outputFile.open(filename);//open outputfile
// outputFile.close();
// cout << "\nData written to"<< filename <<"\n";
return *arr;
}
double populateArray(double* arr,int x)
{
for ( int i = 0; i < x; i++)
{
cout << "Enter the weight for Side "<<i+1<<" ( > 0 ): ";
cin>>*( arr + i );
//shifts the address
}
return *arr;
}
void rollTime(int x, double y, double *z)
{
vector <double> boundary4;
//cout<< "You have "<< x<<" sides.\n";
//cout<<"Your total weight is "<<y<<"."<<endl;
for( int i = 0; i < x; i++)
{
double prob = (*(z+i)/y)*100;
//cout<<"The probability of landing on side "<< i +1 << " is "<<prob<<endl;
probability[i]=prob;
}
//for( int i = 0; i < x; i++)
//{
// cout<<"The probability of landing on side "<< i +1 << " is "<<probability[i]<<endl;
// }
double boundary1 =0;
for( int i = 0; i < x; i++)
{
boundary1 += probability[i] ;
// cout<<"The upper boundary for landing on side "<< i +1 << " is "<<boundary1<<endl;
boundary4.push_back(boundary1);
}
// for( int i = 0; i < x; i++)
// {
//cout<<"The upper boundary for landing on side "<< i +1 << " is "<<boundary4[i]<<endl;
// }
// double random2 = (rand()%100)+.01;
//cout<<"You rolled a for your first try "<<setprecision(4)<<" "<< random2<<" lets find out where that falls.\n";
// int result = 0;
// for ( int i = 0; i < x; i++)
// {
// if (boundary4[i] >= random2)
// {
// result = i+1;
// break;
// }
// }
// cout<<"you fell on side "<<result<<endl;
cout<<"how many times would you like to roll with me?"<<endl;
int rollwithme =0;
cin>>rollwithme;
cout<<"you want to roll "<<rollwithme<<" times. Lets go.\n";
int result3[x];
for (int i = 0; i <= x; ++i)
{
result3[i]=0;
}
for ( int i = 0; i < rollwithme; i++)
{
double random3 = (rand()%100)+.01;
for ( int i = 0; i < x; i++)
{
if (boundary4[i] >= random3)
{
result3[i] = result3[i] + 1;
break;
}
}
}
for ( int i = 0; i < x; i++)
{
cout<<"you rolled side "<<i+1<<" "<<result3[i]<<" times with an expected probability of "<<probability[i]<<"\n";
}
}
at line 58 the function will not take the array that we already declared with the other function and it is saying it is undeclared when i bring it out of the scope of the other function.
I'd recommend you start by eliminating all of those global variables, pass the variables to and from the functions that need them.
Next statically allocated arrays must use compile time constants for their sizes and the size must be greater than 0.
Lastly for now, be very important, use meaningful variable and function names and avoid single letter variable names except when used as loop index values. Single letter variable names combined with global variables make the program difficult to debug because the program logic is being obscured.
I've managed to workaround this solution with vectors which is probably a much more difficult way of accomplishing this project. I can't post the full solution here. However I did PM you jib. Thanks for your advice.