struntz
Jan 19th, 2002, 03:42 PM
Hello everyone! :D
i've been trying to figure this code otu for awhile and i don't know why this isnt' working maybe the problem isn't even with the if condition, but here is the code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int NUM = 3; //for debugging, should be larger #
double amount, total = 0, *data;
int count = 0, size = NUM;
data = new double[size]; //initial array on the heap
do
{
cout <<" Enter amount (or 0 to finish): ";
cin >> amount;
if(amount == 0) break;
if(count == size)
{
double *q = new double[2*size];
cout <<"More memory alocated: size = " << size << endl;
for(int i = 0; i < size; i++)
{
q[i] = data[i];
size *= 2;
delete [] data;
data = q;
}
total += amount;
data[count++] = amount;
}
}while(true);
//get next inputed value
if(amount != 0)
{
cout <<"Out of memory: input was terminated\n";
cout <<"The value " << amount << " is not saved" << endl;
}
cout <<"\n Total of" << count << "values is"
<< total << endl;
if(count == 0) return 0;
cout << "\n Tran no. Amount\n\n";
cout.setf(ios::fixed);
cout.precision(2);
for(int i =0; i < count; i++)
{
cout << setw(4); cout << i+1;
cout << setw(11); cout << data[i] << endl;
}
return 0;
}
when i compile this, it compiles fine, but when i run this, after i enter more than 3 values, it should break off into this condition
if(count == size)
{
double *q = new double[2*size];
cout <<"More memory alocated: size = " << size << endl;
for(int i = 0; i < size; i++)
{
q[i] = data[i];
size *= 2;
delete [] data;
data = q;
}
but it will let me enter mutliply numbers, i can add as much as i want and when i type in 0, it breaks and prints out
total number of 0 values is 0
the problem maybe in the for loop.....
thanksf or listening :D
i've been trying to figure this code otu for awhile and i don't know why this isnt' working maybe the problem isn't even with the if condition, but here is the code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int NUM = 3; //for debugging, should be larger #
double amount, total = 0, *data;
int count = 0, size = NUM;
data = new double[size]; //initial array on the heap
do
{
cout <<" Enter amount (or 0 to finish): ";
cin >> amount;
if(amount == 0) break;
if(count == size)
{
double *q = new double[2*size];
cout <<"More memory alocated: size = " << size << endl;
for(int i = 0; i < size; i++)
{
q[i] = data[i];
size *= 2;
delete [] data;
data = q;
}
total += amount;
data[count++] = amount;
}
}while(true);
//get next inputed value
if(amount != 0)
{
cout <<"Out of memory: input was terminated\n";
cout <<"The value " << amount << " is not saved" << endl;
}
cout <<"\n Total of" << count << "values is"
<< total << endl;
if(count == 0) return 0;
cout << "\n Tran no. Amount\n\n";
cout.setf(ios::fixed);
cout.precision(2);
for(int i =0; i < count; i++)
{
cout << setw(4); cout << i+1;
cout << setw(11); cout << data[i] << endl;
}
return 0;
}
when i compile this, it compiles fine, but when i run this, after i enter more than 3 values, it should break off into this condition
if(count == size)
{
double *q = new double[2*size];
cout <<"More memory alocated: size = " << size << endl;
for(int i = 0; i < size; i++)
{
q[i] = data[i];
size *= 2;
delete [] data;
data = q;
}
but it will let me enter mutliply numbers, i can add as much as i want and when i type in 0, it breaks and prints out
total number of 0 values is 0
the problem maybe in the for loop.....
thanksf or listening :D