C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
 

Code Snippets

  

C++ Source Code



Recording using pointers

Records a number using a pointer

Submitted By: DeeViLiSh
Actions:
Rating:
Views: 1,601

Language: C++

Last Modified: August 29, 2006

Snippet


  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int main ()
  6. {
  7.         int i,n;
  8.         int * p;
  9.  
  10.         cout << "How many numbers would you like to type? ";//The max limit of the table
  11.         cin >> i;
  12.  
  13.         p = new int[i]; //Sets the table limit
  14.  
  15.         if (p == 0)
  16.                 cout << "Error: memory could not be allocated";
  17.  
  18.         else
  19.         {
  20.                 for (n=0; n<i; n++) //Records numbers
  21.                 {
  22.                         cout << "Enter number: ";
  23.                         cin >> p[n];
  24.                 }
  25.         cout << "You have entered: "; //Prints out numbers
  26.  
  27.         for (n=0; n<i; n++)
  28.                 cout << p[n] << ", ";
  29.  
  30.         delete[] p;
  31.         }
  32.  
  33.   system("PAUSE");
  34.   return 0;
  35.  
  36. }
  37.  

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.