Array
-
I have an array:int amounts[] How do I define size of arrays as an enumeration literal Num in an anonymous enumeration?
Typically when I need to do this I have: enum Test { Item 1 Item 2 . . . Item N TestEnd } then I just use (TestEnd - Item 1) for the size on the array. Just make sure that if you add to the enumeration that TestEnd or whatever you call it is always the last item. This will create a proper sized array.
-
Typically when I need to do this I have: enum Test { Item 1 Item 2 . . . Item N TestEnd } then I just use (TestEnd - Item 1) for the size on the array. Just make sure that if you add to the enumeration that TestEnd or whatever you call it is always the last item. This will create a proper sized array.
Here is my code: Can you help me? #include using namespace std; const NUM = 2; int volumes[NUM], volume; int count=0; int amounts[NUM],amount; const double price=0.05; const double fee=12.0; int main() { do { cout<<"Please enter the volume of water used by a customer "; cout<< "during a month (in gallons).(Zero to end):"; cin>>volume; if (volume==0.0) break; if (count==NUM) break; volumes[count++]=volume; amount=(volume*price)+fee; amounts[count++]=amount; } while(true); return 0; }
-
Here is my code: Can you help me? #include using namespace std; const NUM = 2; int volumes[NUM], volume; int count=0; int amounts[NUM],amount; const double price=0.05; const double fee=12.0; int main() { do { cout<<"Please enter the volume of water used by a customer "; cout<< "during a month (in gallons).(Zero to end):"; cin>>volume; if (volume==0.0) break; if (count==NUM) break; volumes[count++]=volume; amount=(volume*price)+fee; amounts[count++]=amount; } while(true); return 0; }
// include and in stdafx.h #include "stdafx.h" using namespace std; const NUM = 2; // changed int to double because volume usually isn't in whole numbers //int volumes[NUM], volume; double volumes[NUM], volume; int count=0; // use double for amounts because you are using doubles to multiply double amounts[NUM],amount; const double price=0.05; const double fee=12.0; int main() { do { cout<<"Please enter the volume of water used by a customer "; // cout won't get you the value must use cin //cout> volume; // with doubles never check for equality, check for greater or // less than if (volume < DBL_MIN ) break; // move this check to the end of the while loop // and make it check for >= instead of == //if (count==NUM) // break; // also you are incrementing count twice. only do it once //volumes[count++]=volume; amount=(volume*price)+fee; //amounts[count++]=amount; volumes[count] = volume; amounts[count] = amount; count++; if( count >= NUM ) break; } while(true); return 0; }
-
// include and in stdafx.h #include "stdafx.h" using namespace std; const NUM = 2; // changed int to double because volume usually isn't in whole numbers //int volumes[NUM], volume; double volumes[NUM], volume; int count=0; // use double for amounts because you are using doubles to multiply double amounts[NUM],amount; const double price=0.05; const double fee=12.0; int main() { do { cout<<"Please enter the volume of water used by a customer "; // cout won't get you the value must use cin //cout> volume; // with doubles never check for equality, check for greater or // less than if (volume < DBL_MIN ) break; // move this check to the end of the while loop // and make it check for >= instead of == //if (count==NUM) // break; // also you are incrementing count twice. only do it once //volumes[count++]=volume; amount=(volume*price)+fee; //amounts[count++]=amount; volumes[count] = volume; amounts[count] = amount; count++; if( count >= NUM ) break; } while(true); return 0; }
Thanks for the comments. It helps a lot. One more question: I am trying to display all values after user enters 0. I wrote the following code. I can't see what is incorrect in this code. displayArray(); void displayArray(const int volumes[NUM];const int NUM) { count<<"\n"; int count=0; while (count
-
Thanks for the comments. It helps a lot. One more question: I am trying to display all values after user enters 0. I wrote the following code. I can't see what is incorrect in this code. displayArray(); void displayArray(const int volumes[NUM];const int NUM) { count<<"\n"; int count=0; while (count
Also don't forget to predefine the function unless you put the fuction code before main. displayArray( volumes, count); // First the Function declaration is incorrect. // you have a semicolon where there should be a comma // and you have volumes declared as int when it should be // double but that was covered previously // also you don't need NUM as a variable because it is a // program constant. Instead pass Count //void displayArray( const int volumes[NUM]; const int NUM) void displayArray( const double volumes[NUM], const int Count) { cout<<"\n"; int nCount=0; while ( nCount < Count) { cout<< volumes[nCount] << " "; nCount++; } int nPause; cin >> nPause; }
-
Also don't forget to predefine the function unless you put the fuction code before main. displayArray( volumes, count); // First the Function declaration is incorrect. // you have a semicolon where there should be a comma // and you have volumes declared as int when it should be // double but that was covered previously // also you don't need NUM as a variable because it is a // program constant. Instead pass Count //void displayArray( const int volumes[NUM]; const int NUM) void displayArray( const double volumes[NUM], const int Count) { cout<<"\n"; int nCount=0; while ( nCount < Count) { cout<< volumes[nCount] << " "; nCount++; } int nPause; cin >> nPause; }