hello, i want to get some guidance from the reader who read my post. I'm doing a game called deal or no deal using c++ and i can't think of other way to print the box's number instead of printing the value inside. what i want to do is, user input the number of the box he/she select and print the value inside. what i can think of is use pass by reference by calling the function that store the value inside the box once the user input the number. but it came out error. i wonder that is my way of using pass by reference is wrong? and my i know how to calculate the banker's offer? i had browsed through google on how to calculate the offer, but i still don't understand the formula.

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

void generate(double amount_in_box[], int RandIndex, double array[])
{
	
	srand( time(NULL) );
	double amount_in_box[26]={.01,1,5,10,25,50,75,100,200,300,400,500,750,1000,5000,10000,25000,50000,75000,100000,200000,300000,400000,500000,750000,1000000};
	for(int i=0; i<26; i++)
	{
		RandIndex=rand() % 26;
		array[i]==amount_in_box[RandIndex];
	}	
}	

void showbox(int box[])
{
	 box[26]={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};
	
	for( int i=0; i<26; i++)
	{
			cout<<" "<<"|"<<box[i]<<"|";
	}
	
}
void GetValue(double& array[],int input)
{
	
	cout<<"Please choose a number:"<<endl;
	cin>>input;
	
	if( input==1)// testing purpose
	{
		cout<<array[1]<<endl;
	}
	else
	{
		cout<<"bad input"<<endl;
	}
	
} 
int main()
{
	double amount_in_box[26];double array[];int box[26],
	int RandIndex,input;
	generate(amount_in_box[],RandIndex);
	showbox( box[]);
	GetValue(array[],input);
	
}

Recommended Answers

All 23 Replies

There are lot of mistakes in your code. First of all, use vectors instead of arrays. Arrays are evil, and can cause you a lot of headaches, when they go wrong.

I am sorry, but almost all of your functions are wrong. :( You are trying to pass your arrays as value, not reference, therefore the generate, and all the other functions wont work at all! Plus arrays are not dynamic, they cannot be resized on the fly. Use vectors, or pointers(preferably vectors).

And why are you using double arrays to store integers? The modulo operator, will only give you back integers. Use int, double is for floating point numbers, it is for precision.

Back to the functions, what you need to do is pass your arrays as reference, so that you can manipulate them in the main. - Remember everything you pass as value, is only just a copy of the original variable.

Here is a small code, that might help you:

#include <iostream>

using namespace std;

void showAll ( int*, int );
void changeThemToZeros ( int*, int );

int main ()
{
	int myArray[] = { 1, 2, 3, 4 };
 
	showAll ( myArray, 4 ); // &myArray[0] - we are giving the address of the array here

	changeThemToZeros ( myArray, 4 );

	showAll ( myArray, 4 );

	return 0;
}

void showAll ( int *ptr, int n )
{
	for ( int i = 0; i < n; ++i, ++ptr )
		
		cout << *ptr << ' ';
		
	cout << endl;
}

void changeThemToZeros ( int *ptr, int n )
{
	for ( int i = 0; i < n; ++i, ++ptr )
		
		*ptr = 0;
}

Don't get confused by the asterisk in the showAll function. - It's only purpose is to dereference the pointer, so it will give back the actual value, not some memory address.

One more thing you can only declare arrays like this:

int array[] = { 1,2 };

is, because the number of values are given, so the compiler will know, the size of that array will be 2. That is not something you can change later, the size of the array has to be given, when you declare it.

Ok... I've gone through ur program.. Few suggestions :
1) There are errors in your main program.
The three errors are :
a) You are using 'int main' and there is no return statement
b) While declaring the array 'array' you havent specified array size(left [] blank)
c) After int box[26], you've used a comma, use a semicolon

Tip : While accepting an array as a parameter in a function, it is always best to do it using a pointer.
Eg: void showbox(int *box) is better than void showbox(int box[])
Reason : Turbo C++ gives an error while using the second method and i think you are using Turbo C++ Ver 3.0. Also, the function is called as usual.
To call function : showbox(box) and not showbox(box[])

These are a few noticeable errors/suggestions.. try incorporating these and see if ur program works.. If not, i've already written the source code for the entire program and i'll give it to u, but only once u've tried and reached somewhere and yes, ofcourse if u need it anymore

I dont think u should ask him to use vectors as that is a concept after arrays and unless you cant master the simpler ones, vectors are gonna be tougher for him... In my opinion, the doubt is pretty basic which makes very clear that he needs more practice with the basics... If you can, read 'passing arrays to functions using pointers' as that would be enough at this level to solve your problem. Try simpler stuff and become confident in the basics, only then learn more complicated things.. Coz if u dont... ur gonna keep screwing up !!

commented: I completely agree about vectors. Too many people believe vectors are the greatest thing since the wheel. But arrays are taught first because they are more universal. +17

hello, um, may i know what's the meaning of "double amount_in_box[26] shadows a parameter? um, the reason i declare as double is because the value inside the box contain decimal, when i type double RandIndex as the declaration for the RandIndex, it gives error when i compile, but when i typed int, it works, so i'm a little not sure here. i used MinGW and notepad++ for programming

You are using the same name for the array, and for the parameter you are trying to pass to the function.
Also, I've just noticed another mistake, at line 14. - You are assigning the value not comparing it, right?

array[i]==amount_in_box[RandIndex];

type array [ value ] <- in the index operator the value must be integer, you cannot use float, or double for that purpose.

hello, um, you meant, the data type must be the same?

hello adityatandon,

may i know why you put the %5?

int j=0;
for( int i=0; i<26; i++)
	{
		cout<<" "<<"|"<<box[i]<<"|";
	              ++j;
              if(j%5==)
                 cout<<"\n";
}

Yea, thats so that your output can be formatted as u asked

|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|

Thats the output u want! Am i right ?
I have added if(j%5==0)cout<<"\n";
This shows that as soon as j is divisible by 5, it goes to the next line
Basically for formatting of your output !

hello, may i know what is the meaning of invalid type double[26][double] for array subscript?

i've tried to put all the thing inside the main, once correct, i will implement it in function, but when i run the program, it print "invalid type double[26][double] for array subscript". i wonder why we can't do this?

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

int main()
{
	int box[26]={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};
	double boxvalue[26]={.01,1,5,10,25,50,75,100,200,300,400,500,750,1000,5000,10000,25000,50000,75000,100000,200000,300000,400000,500000,750000,1000000};
	double cboxvalue[26];
	double RandIndex; int input;// the RandIndex cannot be declared as double.Why?
		
	srand( time(NULL) );
	for( int j=0; j<26; j++)
	{
		RandIndex=rand()%26;
		cboxvalue[j]=boxvalue[RandIndex];
		
	}
	
	
	for( int i=0; i<26; i++)
	{
			cout<<" "<<"|"<<box[i]<<"|";
	}
	cout<<endl;
	
	cout<<"Please enter a number:"<<endl;
	cin>>input;
	
	if( input==1)
	{
		cout<<cboxvalue[1];
	}
	else
	{
		cout<<"Bad input"<<endl;
	}
}

randindex cannot be declared as double because of two reasons :
1) You are using rand to generate a random number and store it in randindex. As rand() function only generates integers as random numbers randindex cant be double.
2) This is a more probable reason.. In line 17 you are saying the following :

cboxvalue[j]=boxvalue[RandIndex];

As the array of boxvalue only has integer numbers, i.e boxvalue[1] exists but boxvalue[1.12] does not exist !

Try declaring randindex as int... Your program should work great then !

hello adityatandon,

i see, thank you for explaining the %5! XD

Program Worked ?

there are a few things i need to do: print all the value in deal or no deal, calculate the banker's offer, user's selection, error handling. i want to made sure the value generated for each boxes is not the same, means there's no same number generated on two or more boxes, so i have to do checking for this, i wanted to use array searching, but the main things is that i don't even know what's the value in the boxes, so the condition will be a problem, i wanted to use:

if (cbox[j]==.....)

the condition i not sure how to write, but i do know we use "&&" in the condition...

The problem is RandIndex's type is double, you can't use floating point numbers, for indexing elements in an array.

double RandIndex;
.
.
cboxvalue[j]=boxvalue[RandIndex];

The index has nothing to do with type of the array.

#include <iostream>

using std::cout;
using std::endl;

struct MyStruct
{
	int a;
	char c;
};

int main ()
{
	double DoubleArray[3] = { 2.13, 3.14, 5.2 };

	cout << DoubleArray[2] << endl;

	int IntArray[3] = { 1, 2, 3 };

	cout << IntArray[1] << endl;

	char CharArray[3] = { 'a', 'b', 'c' };

	cout << CharArray[0] << endl;

	

	MyStruct CustomTypeArray[3]; // declared, not initialiezed

	long LongIntArray[7];

	return 0;
}

You see, I am using 'int' in the [] operator to index the elements of the arrays, regardless what kind of type they are holding.

hello,

i wanted to do error checking to ensure that there's no repeated value in the boxes. so i thought of using array searching to do it, this is what i have in mind:

int main()
{
	int box[26]={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};
	double boxvalue[26]={.01,1,5,10,25,50,75,100,200,300,400,500,750,1000,5000,10000,25000,50000,75000,100000,200000,300000,400000,500000,750000,1000000};
	double cboxvalue[26];
	int RandIndex; int input;
		
	srand( time(NULL) );
	for( int j=0; j<26; j++)
	{
	   RandIndex=rand()%26;
           cboxvalue[j]=boxvalue[RandIndex];

           do{
                   
              if( cboxvalue[1]==1||cboxvalue[1]==5||cboxvalue[1]==75||.....)//checking
               {
                  RandIndex=rand()%26;
		  cboxvalue[j]=boxvalue[RandIndex];
                }while( cboxvalue[1]==true)//not sure here


		
	     }

i thought of another way is that, to do this:

if(cboxvalue[1]==cboxvalue[2]&&cboxvalue[1]==cboxvalue[3]&&.....)
{
  
    RandIndex=rand()%26;
    cboxvalue[1]=boxvalue[RandIndex];
}
else if ( cboxvalue[2]==cboxvalue[1]....)
{
    RandIndex=rand()%26;
    cboxvalue[2]=boxvalue[RandIndex];
}

i think i'll need loop for this, but i not sure it need to loop how many times, or i should use do while to do it?

You are on the right track. :) About your loop, when you cannot determinate(actually no one can) when the right value will be assigned to your array, use while.

Your if statement is somewhat ugly, think about what you want to do.

1, You want to generate different elements for you array.
2, Therefore you need to check the previously generated elements in it.

Your array is empty at the moment.

[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]

After you generated at least two elements, you want go back in your array, and check for duplicates. If the element(currently generated) already exists somewhere, you need to regenerate it again, until it will be the only one.

[5] [8] [ ] [ ] [ ] [ ] [ ] [ ] [ ]

[5] [8] [11] [22] [ ] [ ] [ ] [ ] [ ] . . .

And so on, until your array is full. You will need nested loops to do that.

You are on the right track as LRRR said.. As already pointed out earlier by me and then again by LRRR, reason randindex wasnt double was because u wer calling elements of an array! Coming to checking for doubles, u are going correct but instead of so many ifs and elses, use the following :

for(int i=0;i<26;++i)
         { for( int j=0;j<26;++j)
                { if(cboxvalue[i]==cboxvalue[j])
                     cboxvalue[j]=0;
                 }
          }

What the above loop is doing, is comparing all elements and if any element is repeated, it just converts it to 0 and hence u can later search for the 0's and replace them with numbers that are not 0's and are not repeated !

but arent both i and j have the same value? because both start from zero and we're looking at the same array,

for(int i=0;i<26;++i)
         { for( int j=0;j<26;++j)
                { if(cboxvalue[i]==cboxvalue[j])

You are right, that nested loop would not work. At some point all the element would be compared to the element itself. What you need to do is change the inner loop's starting point to 'int j = i + 1;' so only the elements after index 'i' would be compared. Think of i as an anchor point you slowly keep pushing forward as you advance in the array, and j is the running index to find duplicates.

A simple code to demonstrate it:

#include <iostream>

int main ()
{
	int array[] = { 3, 1, 4, 3, 8, 5, 6, 7, 8, 9, 10, 7, 3, 10, 9 }; // 15


	for ( int i = 0; i < 15; ++i )

		for ( int j = i + 1; j < 15; ++j )
			
			if ( array[j] == array[i] )
				
				array[j] = 0;


	for ( int i = 0; i < 15; ++i )

		std::cout << array[i] << ' ';

	std::cout << std::endl;

	return 0;
}

hello, why i got the output all 0 when i do this:

for (int i=0; i<20; i++)
	{
		for (int j=i+1;j<20; j++)
		{
			if (cboxvalue[j]==cboxvalue[i])
			{
				cboxvalue[j]=0;
			}
		}
	}
	for ( int i=0; i <20; i++)
		{
			if ( cboxvalue[i]=0)
			{
				RandIndex=rand()% 20;
				cboxvalue[i]=boxvalue[RandIndex];
			}
		}

i thought we use search array to do it? aren't way of doing search array is using loop and if else statement?

hello, how to set color using c++? i've searched through the google and i still can't get it, they use void setcolor() and some kind of function inside, which i don't understand....

hello,

for the checking part i used if else statement, because i couldn't understand the loop, i want to ask a few things here, if i declare array[20] means my value supposed from 0 to 19 right? but why when generate the number and print it, there's a value 0, whereas in my array, i don't have 0 in there.this is my array that store the value:

int boxvalue[20]={1,5,50,100,200,500,1000,2000,5000,20000,30000,50000,100000,120000,150000,200000,220000,250000,300000};

when the game started, user must select a box and in the game, if user input no deal, user have to select certain number of boxes available right? so, how do we do this? we implement loop for this right?

then each round the number of boxes that user need to choose will be decreasing am i right? so for the round part i think i want to implement function,but how about number of boxes can be select during that round? should i implement different loop or use back the same and reset the number of boxes to be chosen?this is the loop to eliminate the boxes, and i want to set the box selected to be 'X' or something so that user cannot choose that particular box again. but it seems like i failed. i tried to assign 'X' to box[num] then print the whole box again, it still appeared the same, and i tried to change the text color of the box selected to black, it failed as well, so i got no idea what can i do anymore. and i wanted to maintain the sign in each round, how to implement it?

char sign='X';

for( int i=5; i>=1; i--)
  {
	cout<<"Please choose a box to eliminate"<<endl;
	cin>>num;
	cout<<cboxvalue[num]<<endl;	
	box[num]=sign;
	displaybox(box);
}

i also did something like this:

void dealornodeal(int deal)
{
	cout<<" 1. DEAL  2.NO DEAL"<<endl;
	cin>>deal;
	do
		{
			if ( deal==1)
			{ 
				cout<<"boxaathand"<<endl;
			}
			else if ( deal==2)
			{
				// not yet implement
			}
			else
			{
				cout<<"ERROR! Please input 1 OR 2!"<<endl;//if user enter input other than 1 or 2, output this sentence
				cout<<"Please enter again!"<<endl;
				cin>>deal;
			}
		}while ( deal !=1 && deal !=2);

}

is it possible to call another function for the not yet implement part?

In your post far above, look at your if statement. You are not comparing, you are assigning(single equal sign)!

if ( cboxvalue[i]=0) // use == instead of =

I think the easiest way would be to eliminate a box, is to put a special value in it, so you can identify the boxes which have been already ruled out. For example -1, or 0, I am not familiar with the rules of the game so it is up to you. When the player select a box, which contains your special value, you force him/her to pick another one, until it is valid. Also I would make a variable to store the number of the valid boxes, and decrease it each time when the user picked out one box, so I accidentally would not end keep asking the player to pick another one over and over again, when there aren't any left.(Infinite loop.) Also don't forget to initialize your variables.

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.