I have this problem. I don't know how to prompt user to input for the size of array in a class

#include <iostream>
using namespace std;

class ArrayClass1
   {
      private: 
      int *array , size ;

      public:
      void prompt(void) ;  // asks for size of desired array
                           // and allocates it, then fills it
                           // with random numbers
      void print(void)  ;  // prints out the array
                           // Uses sprintf() formatting to keep all 
                           // the numbers nicely aligned vertically.
      void createFile( char * filename) ; // opens disk file and writes array to it
      void sortArray(void) ; // sorts array using simple Bubble Sort.
   } ;

int main ()
 {
   int array1; // first array to add

   std::cout << "Enter the size of array: "; // prompt user for data
   std::cin >> array1; // read first integer from user into array1

   
return 0;// indicate that program ended successfully

} // end function main

My friend recommend me this but how exactly I can put this just to prompt user to input the size...???

cout << "size of array is " << myArray.getSize() << " and the subscript is now " << myArray.getSubscript() << endl;

I have this problem. I don't know how to prompt user to input for the size of array in a class

#include <iostream>
using namespace std;

class ArrayClass1
   {
      private: 
      int *array , size ;

      public:
      void prompt(void) ;  // asks for size of desired array
                           // and allocates it, then fills it
                           // with random numbers
      void print(void)  ;  // prints out the array
                           // Uses sprintf() formatting to keep all 
                           // the numbers nicely aligned vertically.
      void createFile( char * filename) ; // opens disk file and writes array to it
      void sortArray(void) ; // sorts array using simple Bubble Sort.
   } ;

int main ()
 {
   int array1; // first array to add

   std::cout << "Enter the size of array: "; // prompt user for data
   std::cin >> array1; // read first integer from user into array1

   
return 0;// indicate that program ended successfully

} // end function main

My friend recommend me this but how exactly I can put this just to prompt user to input the size...???

cout << "size of array is " << myArray.getSize() << " and the subscript is now " << myArray.getSubscript() << endl;

int main ()
 {
   int array1; // first array to add

   std::cout << "Enter the size of array: "; // prompt user for data
   std::cin >> array1; // read first integer from user into array1

   
return 0;// indicate that program ended successfully

} // end function main

I would rename the variable in red. If it is an integer, calling it array1 is going to be confusing. Call it arraySize or something. Regarding the code in green, again, it is a bit confusing. array1 is either an array or an integer. From the prompt and the first comment, it looks like you want it to be an integer. The second comment suggests that it is an element of an array, but no array has been declared.

From your class specifications, it appears that this code should not be in main, but rather in the function called prompt rather than main where you have it currently. You need to declare an object of type ArrayClass1 , then call the prompt function.

int main ()
{
     ArrayClass1 anArray;
     anArray.prompt();
     // other code.
     return 0;
}


void ArrayClass1::prompt ()
{
     // prompt user for array size, put into size data member
     array = new int[size];
     for (int i = 0; i < size; i++)
     {
          // code to fill array with random integers
     }
}

Thank for the information but I still get several complier errors. I might put it wrong...

/*------------------------------------------------------------------
* Assignment 5: Classes, array processing.
*Worth: 50 points.
*Made by: Samuel Georgeo (max11) 	       			       

Write a C++ program which creates a class object which
allocates and fills an array with random numbers
prints array contents
sorts array contents using Bubble Sort (code provided)
prints sorted array contents
saves the unsorted array to a text file on disk
saves the sorted array to a text file on disk with the suffix ".srt"
appended to the filename.
-------------------------------------------------------------------*/

#include <iostream>
using namespace std;

class ArrayClass1
   {
      private: 
      int *array , size ;

      public:
      
	  int ArrayClass1 anArray;
      int anArray.prompt();

	  void prompt(void) ;  // asks for size of desired array
      void ArrayClass1::prompt ()// and allocates it, then fills it
                           // with random numbers
      void print(void)  ;  // prints out the array
                           // Uses sprintf() formatting to keep all 
                           // the numbers nicely aligned vertically.
      void createFile( char * filename) ; // opens disk file and writes array to it
      void sortArray(void) ; // sorts array using simple Bubble Sort.
   } ;

int main ()
 {
   int arraySize; // first array to add

   array = new int[size];
     for (int i = 0; i < size; i++)
     
  {

{
/*------------------------------------------------------------------
* Assignment 5: Classes, array processing.
*Worth: 50 points.
*Made by: Samuel Georgeo (max11) 	       			       

Write a C++ program which creates a class object which
allocates and fills an array with random numbers
prints array contents
sorts array contents using Bubble Sort (code provided)
prints sorted array contents
saves the unsorted array to a text file on disk
saves the sorted array to a text file on disk with the suffix ".srt"
appended to the filename.
-------------------------------------------------------------------*/

#include <iostream>
using namespace std;

class ArrayClass1
   {
      private: 
      int *array , size ;

      public:
      
	  int ArrayClass1 anArray;
      int anArray.prompt();

	  void prompt(void) ;  // asks for size of desired array
      void ArrayClass1::prompt ()// and allocates it, then fills it
                           // with random numbers
      void print(void)  ;  // prints out the array
                           // Uses sprintf() formatting to keep all 
                           // the numbers nicely aligned vertically.
      void createFile( char * filename) ; // opens disk file and writes array to it
      void sortArray(void) ; // sorts array using simple Bubble Sort.
   } ;

int main ()
 {
   int arraySize; // first array to add

   array = new int[size];
     for (int i = 0; i < size; i++)
     
  {

{

Lines 39 - 48 : You have three starting brackets, but no ending brackets. Is this the whole function? You must have an ending bracket for every starting bracket. You also want to have return 0; at the end of main.

Line 43 - array and size are data members of the ArrayClass1 class. You need to create an object of type ArrayClass1 in order to use those functions and variables. size and array are also private, so you can't access them directly unless you are accessing them from a member function of ArrayClass1, which main is not

Line 27 - this line doesn't make sense. It needs to be a data member or a member function declaration. It looks like you intend it to be a command, but it is not inside any function, so you can't do that.

Line 30 - this is a class implementation function. Don't put it inside lines 19 - 37 (the class declarations). Put it afterwards. ArrayClass1:: signifies that it's an implementation function of ArrayClass1, so don't put it in the class member function declarations. Just put:

void prompt()

in there, which you already have on line 29, though it's like this in yours:

void prompt(void)

I don't think it makes a difference, but I leave off the void inside the parenthese in cases like that.

See my prior post:

int main ()
{
     ArrayClass1 anArray;
     anArray.prompt();
     // other code.
     return 0;
}


void ArrayClass1::prompt ()
{
     // prompt user for array size, put into size data member
     array = new int[size];
     for (int i = 0; i < size; i++)
     {
          // code to fill array with random integers
     }
}

Line 3 creates a new object of type ArrayClass1.
Line 4 calls a member function of ArrayClass1, using the object created in line 3. This is similar to line 27 in the code you posted in your code above. The difference is that, one, this time, the call is within a function, and two, there is no int .

Line 13 uses size and array without declaring them. That's okay because it's in a member function. If it wasn't, you couldn't do that.

This article has been dead for over six months. Start a new discussion instead.