Declaring variables

This is a discussion on Declaring variables within the C++ Programming forums, part of the General Programming Boards category; I want to write a program that gets an integer from an user eg. 5 The five should tell the ...

  1. #1
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25

    Question Declaring variables

    I want to write a program that gets an integer from an user eg. 5
    The five should tell the program that 5 variables should be created. How would i approach this problem? must one use arrays? any input will be appreciated

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Code:
    cin>>i;
    int *pi;
    pi = new int [i];

  3. #3
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25
    Thanks for the reply, but im a bit thick and rather new to c++. Can you give a quick explaination of whats going on??
    `Who are YOU?' said the Caterpillar.
    This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Code:
    int i;
    cin>>i; //get input from user
    int *pi;//define a pointer to int
    pi = new int [i]; //new allocates memory. In this case, 
                             //new allocates memory for i objects of type int.
                             //and pi points to the first elements of the allocated space.
    This new expression allocates i objects of type int from the free store and returns the address of that object. We use that address to initialize the pointer pi.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Last edited by Antigloss; 06-20-2005 at 04:30 AM.

  5. #5
    Registered User fuchu's Avatar
    Join Date
    Jun 2005
    Posts
    8
    Does he know what pointers are yet?

  6. #6
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25
    I have a basic idea of pointers. Does it work the same as in c?
    I wasnt to bad in c, but that was about 2 yrs ago.
    `Who are YOU?' said the Caterpillar.
    This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.

  7. #7
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Quote Originally Posted by Marlon
    I have a basic idea of pointers. Does it work the same as in c?
    I guess so

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring variables in int main()?
    By Programmer_P in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 02:21 AM
  2. dynamically declaring variables
    By smartalco in forum C++ Programming
    Replies: 7
    Last Post: 01-02-2007, 02:02 PM
  3. question about declaring global variables
    By Dag_ in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2005, 06:03 AM
  4. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM
  5. Declaring Variables Using Variables
    By Mr. Squirrel in forum Windows Programming
    Replies: 5
    Last Post: 12-10-2001, 11:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21