Home > front end >  How to define 'i' in a (I think) constant array and the sum in c with variables?
How to define 'i' in a (I think) constant array and the sum in c with variables?

Time:11-16

I keep on getting an error message about line 29, and that the 'i' in "individualCommission[i]" isn't defined. Also, I am trying to find the sum of the entire array.

#include <iostream>
 #include <iomanip>
 
 using namespace std;
 
 void heading( string assighnmentName ); // prototype - declare the function
 void dividerLine( int length, char symbol );
 
 int main()
 {
     // assighnment heading
     heading( "GPA Calculator" );
     
     //create variables to hold user data and calculated data
     double commissionRate = 0.00;
     int    salesNumber    = 0;
     
     cout << endl;
     
     cout << "Hello there! How many sales did you make? ";
     cin >> salesNumber;
     
     if( salesNumber <= 0 )
     {
         cout << "Invalid entry - enter 1 or more, please" << endl;
     }
     
     // convert the salesNumber into a constant to use with our array structures & create array structures
     const int arraySize = salesNumber;
     
     string salesName[ arraySize ];
     double salesAmount[ arraySize ];
     double individualCommission[ arraySize ];
     
     // collect input from user for arraySize
     for ( int i = 0; i < arraySize; i   )
     {
         cin.ignore( 256, '\n' ); // because it doesn't like the switches with cin to string
         
         cout << "What is your #" << i   1 << " sale labeled? ";
         getline( cin, salesName[i] );
         
         do
         {
             cout << "How much does " << salesName[i] << " cost? $ ";
             cin >> salesAmount[i]; //pointing the location in the array
             
             if( salesAmount[i] <= 0 )
             {
                 // add this line to prevent keyboard buffer issues //
                 cout << "Invalid entry - input valure more than zero";
             }
             
             // the else if statments
             if( salesAmount[i] <= 10000 )
             {
                 commissionRate = .1;
             }
             else if( salesAmount[i] <= 15000 )
             {
                 commissionRate = .15;
             }
             else if( salesAmount[i] > 15,000 )
             {
                 commissionRate = .2;
             }
         }while( salesAmount[i] <= 0 );
     }
     
     individualCommission[i] = salesAmount[i] * commissionRate)[i];
     
     dividerLine( 40, '-' );
     
     for( int i = 0; i < arraySize; i   )
     {
         cout << salesName[i];
         cout << "\t\t\t";
         
         cout << salesAmount[i];
         cout << "\t\t\t";
         
         cout << individualCommission[i];
         cout << endl;
     }
     
     // This is what I need: comissionEarned = BLAH BLAH SOMETHING I DONT KNOW THE ANSWER TO 
     // cout << "Total Commission: " << setw(10) << setprecision(2) << fixed << "$ " << commissionEarned << endl;
     
     dividerLine( 40, '-' );
     
     cout << endl << "Thank you for using my commission calculator!";
     
     return 0;
 }
  // DOMAIN OF MY FUNCTIONS //////////////////////////////////////////////////
 
 void heading( string assighnmentName )
 {
     cout << endl << "Amelia Schmidt"      << endl;
     cout << "Mrs. Carr, Period 3" << endl;
     cout << assighnmentName       << endl;
     cout << "November 8, 2022"    << endl << endl; 
     
     dividerLine( 40, '-' );
 } 
 
 void dividerLine( int length, char symbol )
  {
      for( int i = 0; i < length; i   )
      {
          cout << symbol;
      }
      cout << endl;
  } // end the function dividerLine(int, char)


This is the error message I keep getting.

I've tried some arr statements, but I honestly don't know what they actually do or if I'm writing the wrong statement. I have no clue how to work with the [i] undefined part.

CodePudding user response:

Your code was simple to fix. You had put a statement outside the for and it couldn't save the data inside the array. The change was made in line 61:

#include <iostream>
#include <iomanip>
#include <numeric>

using namespace std;

void heading( string assighnmentName ); // prototype - declare the function
void dividerLine( int length, char symbol );

int main() {
    // assighnment heading
    heading( "GPA Calculator" );
    
    //create variables to hold user data and calculated data
    double commissionRate = 0.00;
    int    salesNumber    = 0;
    
    cout << endl;
    
    cout << "Hello there! How many sales did you make? ";
    cin >> salesNumber;
    
    if( salesNumber <= 0 ) {
        cout << "Invalid entry - enter 1 or more, please" << endl;
    }
    
    // convert the salesNumber into a constant to use with our array structures & create array structures
    const int arraySize = salesNumber;
    
    string salesName[ arraySize ];
    double salesAmount[ arraySize ];
    double individualCommission[ arraySize ];
    
    // collect input from user for arraySize
    for ( int i = 0; i < arraySize; i   ) {
        cin.ignore( 256, '\n' ); // because it doesn't like the switches with cin to string
        
        cout << "What is your #" << i   1 << " sale labeled? ";
        getline( cin, salesName[i] );
        
        do {
            cout << "How much does " << salesName[i] << " cost? $ ";
            cin >> salesAmount[i]; //pointing the location in the array
        
            if( salesAmount[i] <= 0 ) {
            // add this line to prevent keyboard buffer issues //
                cout << "Invalid entry - input valure more than zero";
            }
        
            // the else if statments
            if( salesAmount[i] <= 10000 ) {
                commissionRate = .1;
            }
            else if( salesAmount[i] <= 15000 ) {
                commissionRate = .15;
            }
            else if( salesAmount[i] > 15,000 ) {
                commissionRate = .2;
            }
        } while( salesAmount[i] <= 0 );
        individualCommission[i] = salesAmount[i] * commissionRate;
    }
    
    dividerLine( 40, '-' );
    
    for( int i = 0; i < arraySize; i   ) {
        cout << salesName[i];
        cout << "\t\t\t";
        
        cout << salesAmount[i];
        cout << "\t\t\t";
        
        cout << individualCommission[i];
        cout << endl;
    }
    
    double comissionEarned = accumulate(individualCommission, individualCommission arraySize, 0); 
    cout << "Total Commission: " << setw(10) << setprecision(2) << fixed << "$ " << comissionEarned << endl;
    
    dividerLine( 40, '-' );
    
    cout << endl << "Thank you for using my commission calculator!";
    
    return 0;
}
// DOMAIN OF MY FUNCTIONS //////////////////////////////////////////////////

void heading( string assighnmentName ) {
    cout << endl << "Amelia Schmidt"      << endl;
    cout << "Mrs. Carr, Period 3" << endl;
    cout << assighnmentName       << endl;
    cout << "November 8, 2022"    << endl << endl; 

    dividerLine( 40, '-' );
} 

void dividerLine( int length, char symbol ) {
    for( int i = 0; i < length; i   ) {
        cout << symbol;
    }
    cout << endl;
} // end the function dividerLine(int, char)

However I would recommend giving you a couple of tips about the code.

First, always try to understand where the problem lies and don't post hundreds of badly formatted lines of code. Even just a minimum of reproducible code is enough. This also allows us to focus more on the error without thinking about what your program is really doing.

Also, in the current C standard, automatic array allocation is only allowed via compile-time constants. So if you want to create an array with a user-entered size, you just have to use dynamic allocation -> pointers.

That said, I hope I was clear.

  • Related