我不斷收到有關第 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)
這是我不斷收到的錯誤訊息。
我已經嘗試了一些 arr 陳述句,但老實說我不知道??它們實際上做了什么,或者我是否寫錯了陳述句。我不知道如何使用 [i] 未定義部分。
uj5u.com熱心網友回復:
您的代碼很容易修復。您在 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。
那就是說,我希望我是清楚的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/535332.html
標籤:C 数组
