Deleting 1D Array Contents


MPD78
09-21-2009, 07:12 AM
Hello all,

I am having trouble deleting the contents of a 1D array.

Below is my program. This program doesn't do anything other than read in the user input values until 100% volume is reach and then prints the array contents out to the screen. If the user exceeds 100% volume, the array contents are to be deleted and the user has to start entering in values from the begining again.

#include "K:\Engineering Program Header Files\nr3.h"

Int sfnum[20]; // array for the shell side fluid component I.D. numbers
Doub pv[20]; // array for the primary fluid volumes
Doub volumearray[20]; // array for the shell side fluid gas component volumes
string sfarray[20]; // array for the shell side fluid gas component names

Doub total; // used to check for bad input of volume greater than 100 percent
Doub vol; // volume

Int count,j,y,i,s,v; // generic counter variables used for looping
Int compnum; // gas component I.D. number
Int pause; // dummy variable to keep the input screen visible

char szInput[256]; // char input variable to help obtain correct user input

void print_std_gases(Int compnum)
{
cout << "Below are the Standard Gases" << endl;
cout << endl;
cout << "1. N2" << setw(10) << "8. AIR" << setw(10) << "14. CH4" << setw(10) << "19. HCL" << endl;
cout << "2. O2" << setw(9) << "9. H2" << setw(12) << "15. C2H6" << setw(9) << "20. H2S" << endl;
cout << "3. CO2" << setw(8) << "10. CO" << setw(12) << "16. C3H8" << setw(9) << "" << endl;
cout << "4. H2O" << setw(8) << "11. NO" << setw(13) << "17. C4H10" << setw(8) << "" <<endl;
cout << "5. SO2" << setw(9) << "12. NO2" << setw(12) << "18. C5H12" << setw(8) << "" << endl;
cout << "6. SO3" << setw(9) << "13. NH3" << setw(12) << "" << setw(8) << "" << endl;
cout << "7. CL2" << setw(9) << "" << setw(12) << "" << setw(8) << "" << endl;

}

string compname(Doub compnum){
if (compnum==00) exit(1);
if (compnum==1)return("N2 ");
if (compnum==2)return("O2 ");
if (compnum==3)return("CO2 ");
if (compnum==4)return("H2O ");
if (compnum==5)return("SO2 ");
if (compnum==6)return("SO3 ");
if (compnum==7)return("CL2 ");
if (compnum==8)return("AIR ");
if (compnum==9)return("H2 ");
if (compnum==10)return("CO ");
if (compnum==11)return("NO ");
if (compnum==12)return("NO2 ");
if (compnum==13)return("NH3 ");
if (compnum==14)return("CH4 ");
if (compnum==15)return("C2H6 ");
if (compnum==16)return("C3H8 ");
if (compnum==17)return("C4H10 ");
if (compnum==18)return("C5H12 ");
if (compnum==19)return("HCL ");
if (compnum==20)return("H2S ");
}

int main()
{
// Enter the primary fluid
cout << endl;
cout << "PRIMARY FLUID COMPOSITION" << endl;
cout << "=============================================" << endl;
print_std_gases(compnum); // Print the standard gases.
count = j = 0; // Initialize counters and array index to zero
do {
do
{
cout << endl;
printf("Select gas component ");
gets(szInput);
compnum = atoi(szInput);
if (compnum==00) exit(1);
if (compnum < 1 || compnum > 20)
{
cout << "Gas not available " << endl;
}
}while(compnum < 1 || compnum > 20);
cout << endl << compname(compnum) << endl;
printf("Enter the volume percent ");
gets(szInput);
vol = atof(szInput);
vol = vol/100; // Convert volume to a percent
total += vol;
// Logic to reset the counters and delete the current array contents
// if the user goes above 100% volume.
if (total > 1){
cout << "Total volume > 100% You must re-enter your values" << endl;
total = count = j = 0; // Re-set the counter and total to zero

// How do I delete the array contents?

}
// End of logic for use trying to enter a volume percent greater than 100.
cout << "Total volume " << (total*100) << endl;
volumearray[j]=vol; // Send volume to array
sfarray[j]=compname(compnum); // Convert component ID # to component name and send to array
sfnum[j]=compnum; // Send component ID # to array
j++;
count++;
}while (total < 1 && total >= 0);

// Print out the array contents
cout << endl;
cout << "===========================================" << endl << endl;
cout << "PRIMARY FLUID COMPOSITION" << endl;
cout << "===========================================" << endl;
for(Int j=0;j<count;j++){
cout << " " << fixed << sfarray[j] << fixed << " Volume % " << volumearray[j]*100 << endl;
}

cin >> pause; // Dummy variable to keep the output screen visible
return 0;
}

However, I can't seem to delete the array contents. I have looked in some reference material and that instructs me to use pointers and the delete [] command but I am not sure how to proceed.

Here is the output.

===========================================

PRIMARY FLUID COMPOSITION
===========================================
N2 Volume % 101.000000
N2 Volume % 100.000000

Any help with this would be great.

Thanks
Matt

davekw7x
09-21-2009, 09:28 AM
...

I am having trouble deleting the contents of a 1D array.

You can't "delete the contents" of an array. An array has a fixed number of elements, determined when you declare it.

However...

In your application, by "delete the contents," don't you just mean that you need to set all of the element values equal to zero?

Use a loop.


Regards,

Dave

MPD78
09-21-2009, 09:39 AM
Use a loop.

That was the first method that I tried.

Yes, I am trying to zero out the array values.

No matter what I do I still get the same result, both the contents of the array when 100 was exceeded and the contents when 100 was achieved.

Any thoughts?

Thanks
Matt

davekw7x
09-21-2009, 10:13 AM
...
Any thoughts?...

If the numbers are wrong, then the logic is wrong. I made a separate block to do the zeroing just so I wouldn't have to modify anything else in your program. The important thing is the "else" part, to make sure you don't put in the current value after you zero everything out. (You did say that you want to start all over again, right?)


if (total > 1) {
cout << "Total volume > 100% You must re-enter your values" <<
endl;
total = count = j = 0; // Re-set the counter and total to zero
{
int num = sizeof(volumearray)/sizeof(volumearray[0]);
for (int i = 0; i < num; i++) {
volumearray[i] = 0;
sfarray[i] = "";
sfnum[j] = 0;
}
}
} // End of logic for use trying to enter a volume percent greater than 100.
else { // So far, so good. Put this value and continue
cout << "Total volume " << (total * 100) << endl;
volumearray[j] = vol; // Send volume to array
sfarray[j] = compname(compnum); // Convert component ID # to component name and send to array
sfnum[j] = compnum; // Send component ID # to array
j++;
count++;
}


Here's a run:


Select gas component 1

N2
Enter the volume percent 79
Total volume 79

Select gas component 2

O2
Enter the volume percent 20
Total volume 99

Select gas component 3

CO2
Enter the volume percent 2
Total volume > 100% You must re-enter your values

Select gas component 1

N2
Enter the volume percent 79
Total volume 79

Select gas component 2

O2
Enter the volume percent 20
Total volume 99

Select gas component 3

CO2
Enter the volume percent 1
Total volume 100


In fact, you don't have to zero out the array elements themselves at all. Just delete the extra block that I showed above. You will only print out as many values as you counted, so you only need to zero out the count value(s).

So it becomes

if (total > 1) {
cout << "Total volume > 100% You must re-enter your values" <<
total = count = j = 0; // Re-set the counter and total to zero
}
else {
// End of logic for use trying to enter a volume percent greater than 100.
cout << "Total volume " << (total * 100) << endl;
volumearray[j] = vol; // Send volume to array
sfarray[j] = compname(compnum); // Convert component ID # to component name and send to array
sfnum[j] = compnum; // Send component ID # to array
j++;
count++;
}



Regards,

Dave

MPD78
09-21-2009, 10:39 AM
Dave,

All works fine now.

My logic was incorrect.

Note: My wife says I have no logic anyways.

Thanks
Matt