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:
#include <iomanip>
#include <iostream>
usingnamespace std;
int main()
{
int arraysize = 5; //sets initial array size
int* a = newint[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 = newint[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??
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.)
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.
#include <iomanip>
#include <iostream>
usingnamespace std;
int main()
{
int arraysize = 5;
int* a = newint[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 = newint[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?
}