I am tring to set an array with its values to zero within a constructor, but I can't get it to work. I have created an array with the size of 5 in the private of my class. I want the values of my array to be set at zero. Can anybody help me out with this??

#include <iostream>
#include <cstdlib>

using namespace std;

class NumberFun {

  public:

   NumberFun( ); // parameterless default constructor

			
   void squarecube();			
   void history();   

  private:    

   int cube[5]; // Array for squarecube

};

int main() {

    NumberFun mynum;
    int selection;
    
    cout << "Welcome to Number Fun\n\n"; 
    
    while ( selection != 0 ) {
    cout << "********** Main Menu **********\n\n";      
    cout << "Please make a selection" << endl << endl;
    cout << "    * 0 - quit" << endl;
    cout << "    * 1 - TabFive" << endl;
    cout << "    * 2 - SquareCubes" << endl;
    cout << "    * 3 - CalorieCounter" << endl;
    cout << "    * 4 - Show historical output" << endl << endl;
    cin >> selection;
 
    switch (selection){
           case 0:
                break;
           case 1:
                mynum.tabfive( );
                break;
           case 2:
                mynum.squarecube( );
                break;
           case 3:
                mynum.caloriecounter( );
                break;
           case 4:
                mynum.history( );
                break;
           default:
                   cout << "Invalid. Select 0-4.\n\n";
                   }
 
    
}
    
    cout << "\nThank You for using Number Fun!!\n\n";

  system("pause"); 
  return 0;
}

NumberFun::NumberFun() {  // default constructor
    numin = 0;
    ate = 0;
    cube[5];

}

void NumberFun::squarecube() {

// Heading

   cout << "\n**********  SquareCube  **********\n\n";

// Prompt user to enter 5 different integers

   cout << "Enter 1st Integer : " ;
   cin >> cube[0];
   cout << "Enter 2nd Integer : " ;
   cin >> cube[1];
   cout << "Enter 3rd Integer : " ;
   cin >> cube[2];      
   cout << "Enter 4th Integer : " ;
   cin >> cube[3];
   cout << "Enter 5th Integer : " ;
   cin >> cube[4];
         
// Prints a table whose first column has the five integers entered, second column corresponding 
// squares of the first column and third the cubes. Seperated by tabs.

   cout << "\nInt\tSqr\tCube\n" ;
   cout << cube[0] << "\t" << cube[0]*cube[0] << "\t" << cube[0]*cube[0]*cube[0] << endl;
   cout << cube[1] << "\t" << cube[1]*cube[1] << "\t" << cube[1]*cube[1]*cube[1] << endl;
   cout << cube[2] << "\t" << cube[2]*cube[2] << "\t" << cube[2]*cube[2]*cube[2] << endl;
   cout << cube[3] << "\t" << cube[3]*cube[3] << "\t" << cube[3]*cube[3]*cube[3] << endl;
   cout << cube[4] << "\t" << cube[4]*cube[4] << "\t" << cube[4]*cube[4]*cube[4] << endl << endl; 
   
   system("pause");
   
   cout << endl << endl;   

}
void NumberFun::history() {

// Heading

   cout << "\n**********  History  **********\n\n";
   
// Display the History from TabFive

   cout << "---TabFive" << endl << endl;
   cout << "   The number entered is " << numin << endl << endl;
   cout << "---SquareCube" << endl << endl;
   cout << "   The Five Numbers entered are " << cube[0] << " " << cube[1] 
        << " " << cube[2] << " " << cube[3] << " " << cube[4] << endl << endl;
   cout << "---CalorieCounter" << endl << endl;
   cout << "   The number of cookies the user entered is " << ate << endl << endl;
   
   system("pause");
   
   cout << endl << endl;     


}

The line cube[5]; is just a number. However, since the array has only five elements and you are trying to access the sixth, you'll get a bounds error (or worse).

Use a for loop to set each element of the array to zero.

Hope this helps.

Normally I would just do this to set the array to zero.

cube[5] = {0};

But this dosen't work within the constructor. I basically want all values of the array to be initally to zero and then later be able to be changed when the program is run.

Duoas already told you how to do it -- create a for-next loop and set each element to 0

Where do I put the for-next loop?? Within the constructor??

Where do I put the for-next loop?? Within the constructor??

Well Duuuah! of course you put it in the contructor -- afterall that was your original question wasn't it.

delete line 70 of your original post (I added line numbers to make it easier to refer to it). Then put a for-next loop there.

ok thanks, I will try this. I am new to constructors, so forgive me if I ask stupid questions:P

I was able to solve this. I see why I was having trouble with this. Thanks for the help!!
How do I set this post to solved?? oops nevermind I see it.

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.