1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Rep Power

    Error in Output Line for NumArray (Array Program)


    So I'm trying to follow the first exercise in the Ivor Horton book on Visual C++ 2008, and it asks to create an array of unlimited size, which I first defined to be sets of 5 numbers at a time, and then I allowed the user to decide whether or not to continue adding numbers into the array.

    The problem is in the final output line for the code where I just list what the user actually inputted into each element of the 'numarray' array

    When I run the program, I receive an error, and the outputted statement gives me a hexadecimal answer which is obviously just the memory address location of the num array.

    Any idea on how to actually get it to print the values that the user inputted during the course of the program?

    Here's my code:

    #include <iostream>
    using std::cin;
    using std::endl;
    using std::cout;
    int main(){
    // 1. Write a native C++ program that allows an unlimited number of values to be entered 
    // and stored in an array allocated in the free store.
    // Status: Works, but the final output statement is giving me errors and for the
    // actual output line of the entire array (even when I didn't output the numbers
    // pointer, it printed out: 0036518, which is probably just the memory address
    // of the numarray in memory
    	int* numbers = new int[];
    	int numarray[5] = {0};
    	int numberCounter = 0;
    	char indicator = 'y';
    	while (indicator == 'Y' || indicator == 'y'){
    		cout << "Please enter in five numbers:" << endl;
    		for(int i = 0; i < 5; i++){
    			cin >> numarray[i];
    			numbers[numberCounter] = numarray[i]; 
    		cout << "Would you like to continue? (y or n)" << endl;
    		cin >> indicator;
    	cout << "These are the values you decided to input into the array: " << endl;
    	cout << numbers;
    	return 0;
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Rep Power
    One advantage of << is that you can overload it. On one level, that means that if you feed it an object which has an overloaded version of <<, then you can specify exactly how that object will be "printed out". On the other much more common level, the datatype of the operand on the right tells the compiler which version of << to use and hence will display the value of that operand correctly and "automatically". This differs from C's printf format string where you also need to use the correct descriptor flag that matches the datatype that you want to display.

    What is numbers? Is it an object for which you have defined an overload of << ? No, that's not what it is. What numbers is is an int pointer. The value of numbers is an address. Addresses are displayed in hexadecimal. That is what you told your program to display (as opposed to by what you had wanted it to do -- remember, computers never do what we want them to do, but only what we tell them to do) and that is what it did display.

    Since an array is not an object in the sense of being instantiated from a class that you declared, I don't see how you can directly associate an overload of << to one (though you could encapsulate an array within a class, etc). It looks like you would need to write a loop that would << the elements of the array.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Rep Power
    True, it was purely my fault in that case heh.

    I implemented a string that would concatenate another string that converted the current index element to a string form, and I outputted that string at the end of the program.

    It worked with the normal version of the program but it did not work with the second exercise that required a pointer version of the same program (probably because I should have reference the pointer's index value during the course of the loop and not the actual array in that case.

    Anyway, thanks a bunch! That helped a lot!
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Rep Power
    > int* numbers = new int[];
    What does this do?

    $ g++ bar.cpp
    bar.cpp: In function ‘int main()’:
    bar.cpp:19:26: error: expected primary-expression before ‘]’ token
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Rep Power

IMN logo majestic logo threadwatch logo seochat tools logo