Using "cin" to fill a dynamic array not working...

Greetings wise ones and fellow C++ noobs alike.

I am writing a program (yes indeed.. for a class) that will read an arbitrary number of nonnegative integers into an array. The user will enter a negative number as the sentinel value to terminate input. The goal here is basically to replicate what a vector does, but to not use a vector itself. The array is initialized to a size of five, and then doubled if the user enters more than 5 numbers.

My problem here is that I cannot get "cin" to write the input to the array.
Here is the example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
    int arraysize = 5; //sets initial array size
    int* a = new int[arraysize]; // create dynamic array
    int n = 0; //will represent number of integers entered
   
    cout << "Enter a list of nonnegative integers. \n"       //example input:
         << "Place a negative integer after the list.\n";    // 1 2 3 4 5 6 -1
                                                             //[enter/return]   
    do   //dowhile loop starts the process of gathering input
    {
    cin >> a[n]; // This is where the problem is (well one of them anyway)
    
        while (cin >> a[n])   // This might also be a trouble spot
        {
        n++; cout << n << "\n"; cout << "a[n]=" << a[n] << endl;
        //The above line of code demonstrates what's happening.    

            if (n >= arraysize) // 
            {
                arraysize = arraysize * 2; // double the previous size
                int* temp = new int[arraysize]; 
                
                    for (int shifter=0; shifter<n; shifter++) 
                    {
                        temp[shifter] = a[shifter]; // copy values to new array.
                    }
        
                delete [] a; // deletes array, frees memory, etc. 
                a = temp; //this might be another trouble area
            }
        } 
     } while (a[n] >= 0);
    
}
    


Any ideas as to why "cin >> a[n]" is not working??
Last edited on
It is working fine. The problems are mostly variations of fencepost errors.

To find most of them, I suggest you get yourself a text editor or a piece of paper and start out with something like

a[] = _ _ _ _ _
n = 0

Then manually trace your algorithm, filling in a and updating n as you cross each line.

You will also learn why your program won't stop without pressing ctrl-C.

Post back if you can't find all the problems. There are three. (Plus an error with your debugging on line 21 --it should print before you increment n.)

[edit] BTW, your shifter loop is fine. Very nice!

Hope this helps.
Last edited on
I see that removing line 17 solves the problem for the most part.

But I still can't figure out the mandatory ctrl-c issue. According to my tracing of the algorithm... a[n]'s final value is negative so why doesn't the outer loop stop iterating?

Thank you for your help. I appreciate your assistance.

Updated code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
    int arraysize = 5; 
    int* a = new int[arraysize]; 
    int n = 0; 
   
    cout << "Enter a list of nonnegative integers. \n"       
         << "Place a negative integer after the list.\n";   
                                                               
    do
    {
	while (cin >> a[n])   
        { 
        cout << n << "\n"; cout << "a[n]=" << a[n] << endl;
        //The above line of code demonstrates what's happening.    
	n++; 
            if (n >= arraysize) 
            {
                arraysize = arraysize * 2; 
                int* temp = new int[arraysize]; 
                
                    for (int shifter=0; shifter<n; shifter++) 
                    {
                        temp[shifter] = a[shifter]; 
                    }
        
                delete [] a; 
                a = temp; 
            }
        }
     }while (a[n] >= 0);   //this loop is not terminating.  WHY??????
                                      //Is there an equivalent statement that would work?
}
 
Last edited on
Topic archived. No new replies allowed.