Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Array

Array

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sardinka
    wrote on last edited by
    #1

    I have an array:int amounts[] How do I define size of arrays as an enumeration literal Num in an anonymous enumeration?

    A 1 Reply Last reply
    0
    • S sardinka

      I have an array:int amounts[] How do I define size of arrays as an enumeration literal Num in an anonymous enumeration?

      A Offline
      A Offline
      Adam Gritt
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • A Adam Gritt

        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.

        S Offline
        S Offline
        sardinka
        wrote on last edited by
        #3

        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; }

        A 1 Reply Last reply
        0
        • S sardinka

          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; }

          A Offline
          A Offline
          Adam Gritt
          wrote on last edited by
          #4

          // 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; }

          S 1 Reply Last reply
          0
          • A Adam Gritt

            // 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; }

            S Offline
            S Offline
            sardinka
            wrote on last edited by
            #5

            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

            A 1 Reply Last reply
            0
            • S sardinka

              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

              A Offline
              A Offline
              Adam Gritt
              wrote on last edited by
              #6

              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; }

              S 1 Reply Last reply
              0
              • A Adam Gritt

                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; }

                S Offline
                S Offline
                sardinka
                wrote on last edited by
                #7

                Thank you for comments it explains a lot (I am in school ) :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups