okay, Here is what I want to know. How can I store floating point number n an array and have the user enter a number into the array from the keyboard, then be able to print out the number the user enter . Can you give me an example of that?

Followup To
Question -
Hi David I'm still having problem with my program. I decided to send you the program and see if you can tell me what I'm doing wrong and tell me some of the bugs. My biggest problem is I'm not sure how to store an array of number and to retrieve the content of the array. Here it is:

using namespace std;

class Array
 float *number;
 int count;
 char reply;
 char choice;
 void displayMenu(); // Function prototype
 double Average();
 int store_num();
 int retrieve_num();
 int sum();
 int lowest_value();
 void contents();
 void quit();
    delete number;//reclaim storage


Array::Array(int size = 5)
  number = new float[size];
  char choice = 0;
  int count = 0;
  char reply = 0;
  cout <<"Enter a floating point number:  ";
  cin >>number[count++];
// Check if another input is required
  cout<< "Do you want to enter another number?(y/n)\n";
  cin >> reply;
  }while(count < 5 && tolower(reply) == 'y');

  // Indicate when array is full
  if(count == 5)
     cout<< endl <<"Maximum number has been reach. "<<endl;

 int main()
 Array num();



void Array::displayMenu()
  cout<<endl<<" Enter your choice:   \n";
  cout<<"\n Menu\n ";
  cout<<"A) Store a number in any element of the array \n";
  cout<<"B) Retrieve a number from any element of the array \n";
  cout<<"C) Return the highest value stored in the array \n";
  cout<<"D) Return the lowest value stored in the array \n";
   cout<<"E) Return the average of all the numbers in the array \n";
  cout<<"F) Output the contents of the array\n";
  cout<<"G) Quit the program  \n";

  cin>> choice;

  if(choice  == 'A' &&   'a')

     if(choice == 'B' && 'b')
  if(choice == 'C' && 'c')
  if(choice == 'D' && 'd')
  if(choice == 'E' && 'e')
  if(choice == 'F' && 'f')
  if(choice == 'G' && 'g')


double Array::Average()
  double average = 0.0;
  for(int i = 0; i < count; i++)
     average +=number[i];
  average /= count;
  cout<< endl;
     return average ;
void Array::contents()
for (int i = 0; i< number[5]; i++)
cout<< number[i];

int Array::sum()

  int i,n;
  int total(0);
  for(i=0; i< n; i++) total+= number[i];

  return total;

int Array::retrieve_num()
  int i, n, target;
  for(i=0; i<n; i++)
     if(number[i] == target)// key comparison
        return i;
Followup To
Question -
I'm trying to create an array of floating point in a class. The constructor should accept an interger argument and dynamically allocate the array to hold that many number. The problem I'm having is I'm not sure how to access the number in the array. I'm doing this object orient. could you give me an example of how this will work
Answer -
stacy , Thank you for your question.

Arrays are accessed by their address, either using pointer addition or using bracket notation.

Example using bracket notation:

class A
long * pArray;

// Constructor
A(int Length)
pArray = new long[Length]; // Allocate storage
} // End of constructor

// Destructor
delete pArray; // Reclaim storage
} // End of destructor

// Function to access an element
// of the array 'Array'
long Test(int Index)
return pArray[Index];
} // End of function Test
}; // End of class A

David Spector

Answer -
I'm not sure what is giving you trouble. I believe I answered your question, showing you code that creates, references, and frees a dynamic array.

I regret that I am unable to take the time to study your source code due to the volume of questions received. If you can create a shorter example, a tiny test case, or a more specific question, I will be pleased to help you.


stacy, This code has not been tested:

using namespace std;

int main()
double   n = 1.8;
double   Numbers[5];

// Store 'n' into an array
Numbers[2] = n;

// Input 'n' from user and store in the array
cout << "Enter a floating point number: ";
cin >> n;
Numbers[2] = n;
} // main


All Answers

Answers by Expert:

Ask Experts


David Spector


Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.


I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 About.com. All rights reserved.