Thread: dynamic variables help

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    dynamic variables help

    need help with the first part to enter the name and display it
    when i enter a name nothing happens




    Code:
    #include <iostream>
    using namespace std;
    
    const int MAXNAME = 10;
    
    int main()
    
    {
       int pos;
       char * name;
       int * one;
       int * two;
       int * three;
       int result;
    
       one = new int;//  Fill in code to allocate the integer variable one here
       two = new int;//  Fill in code to allocate the integer variable two here
       three= new int;//  Fill in code to allocate the integer variable three here
       name = new char [MAXNAME];//  Fill in code to allocate the character array pointed by name
       
    
       cout << "Enter your last name with exactly 10 characters." << endl;
       cout << "If your name has < 10 characters, repeat last letter. " << endl
    		<< "Blanks at the end do not count." << endl;
       
       for ( pos = 0;  pos < MAXNAME;   pos++)
    
    	   cin >> name;//name[MAXNAME];// Fill in code to read a character into the name array
                 // WITHOUT USING a bracketed subscript
    
      cout << "Hi ";
       for ( pos = 0;  pos < MAXNAME;  pos++)
    
          cout <<  &name;// Fill in code to a print a character from the name array
                  // WITHOUT USING a bracketed subscript
      
     
       
       
       
       cout << endl << "Enter three integer numbers separated by blanks" << endl;
    
      
       int *ptr1= one;
       int *ptr2= two;
       int *ptr3= three;
    
       cin >>  *one >> *two >> *three;// Fill in code to input three numbers and store them in the
       // dynamic variables pointed to by pointers one, two, and three.
       // You are working only with pointer variables
    
       //echo print
       cout << "The three numbers are " << endl;
    
       cout <<  *ptr1 << endl;  
       cout <<  *ptr2 << endl; 
       cout <<  *ptr3 << endl;// Fill in code to output those numbers
    
       result = *one + *two + *three;// Fill in code to calculate the sum of the three numbers
       cout << "The sum of the three values is " << result << endl;
    
      delete one;// Fill in code to deallocate one, two, three and name
      delete two;
      delete three;
      delete name;
    
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what does "WITHOUT USING a bracketed subscript" allow you to do?

    Is replacing
    ptr[i]
    with
    *(ptr+i)
    allowed?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why are you going crazy with new? There is no need for new here. Is there a special reason you use it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    yes its allowed, but i cannot figure this name array part out at all

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So if you had just
    char name[MAX];

    without any of the dynamic allocation, would you know what to do?

    If so, you already have the answer - dynamic allocation doesn't change how you solve the problem in this case.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    lol if you know the limit of your the string length why use DMA?
    use DMA only when necessary..make your life and other programmers easier
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 06:42 PM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM
  5. Data Storage Question, and Dynamic variables?
    By Zeusbwr in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2004, 11:01 PM

Tags for this Thread