Thread: Please review my code

  1. #1
    Registered User
    Join Date
    Feb 2014

    Please review my code

    Write code to do the following:

    1) declare a variable ptr as a pointer to int and initialize it to NULL
    2) dynamically allocate memory for an array of 100 elements
    3) read 100 elements from the standard input device and store them in the array.

    This is what I have so far, I'd like to know if its ok or if something is wrong.

    int *ptr = NULL;
    ptr = new int[100];
    cin >> dataPtr [arr];

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Your input needs a for loop wrapped around it.
    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
    Join Date
    Oct 2007
    Inside my computer
    >>cin >> dataPtr [arr];
    This reads one element from the input and stores it into the index arr.
    C++ has no concept of matrices, so you have to repeat an action for every element of the array.
    Btw, consider learning about std::vector later.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review please
    By Richardcavell in forum C Programming
    Replies: 3
    Last Post: 05-13-2011, 11:01 AM
  2. How to get a code review
    By Richardcavell in forum C Programming
    Replies: 42
    Last Post: 03-19-2011, 10:36 PM
  3. C Code Review
    By trillianjedi in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 06-18-2008, 12:29 PM
  4. Code Review
    By Thantos in forum C Programming
    Replies: 8
    Last Post: 03-06-2004, 06:20 PM
  5. review code
    By absenta in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2002, 02:13 PM