如何定义'i'在一个(我认为)常量数组和总和在c++与变量?

pdsfdshx  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(129)

我一直收到关于第29行的错误信息,并且“individualCommission[i]”中的“i”没有定义。此外,我还试图找到整个数组的和。

#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.
我试过一些arr语句,但老实说,我不知道它们实际上做了什么,也不知道我是否写错了语句。我不知道如何处理[i]未定义部分。

2g32fytz

2g32fytz1#

你的代码很容易修改,因为你在for之外放了一条语句,它不能保存数组中的数据,修改是在第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)

但是,我建议您提供一些关于代码的提示。
首先,总是试图了解问题所在,不要发布数百行格式不正确的代码。即使只是一个minimum of reproducible code就足够了。这也使我们能够更专注于错误,而不必考虑程序实际上在做什么。
同样,在当前的C++ standard中,automatic array allocation只允许通过compile-time constants,所以如果你想创建一个用户输入大小的数组,你只需要使用dynamic allocation-〉pointers
话虽如此,我希望我说得很清楚。

相关问题