对于分配,我们用用户定义的字符填充数组,一旦用户输入句号“。”,该字符将停止填充。作业的一部分是打印出在数组中输入的字符,但是我似乎什么也没打印。第一次问,很抱歉这是一个愚蠢的问题。预先感谢。
#include <iostream>
using namespace std;
//Function declarations
bool fillArray(char charArray[], int arraySize, int& numberUsed);
void outputInReverse(const char charArray[], int& numberUsed);
int main() {
const int arraySize = 100;
char charArray[arraySize] = { };
int numberUsed = 0;
//Function calls
cout << "\nFILLING ARRAY....\n";
fillArray(charArray, arraySize, numberUsed);
cout << "\nARRAY OUTPUT....\n";
outputInReverse(charArray, numberUsed);
}
//Function definitions
bool fillArray(char charArray[], int arraySize, int& numberUsed) {
char inputChar;
int index = 0;
const char sentinel = '.';
bool sentinelEntered = false;
bool arrayFull = false;
int count = 0;
//Take user input
for (int i = 0; i < arraySize; i++) {
if ((!sentinelEntered)) {
cout << "Enter up to " << arraySize << " character values. Enter full stop to end. " << "Enter char " << (i + 1) << ": " << endl;
cin >> inputChar;
charArray[index] = inputChar;
//How many entries made
numberUsed = i;
count++;
if ((inputChar == sentinel)) {
sentinelEntered = true;
cout << "Number of entries: " << (count - 1) << endl;
return count;
}
}
}
if (numberUsed == arraySize) {
arrayFull = true;
return arrayFull;
}
return sentinelEntered;
return count;
}
// Reverse
void outputInReverse(const char charArray[], int& numberUsed) {
for (int i = numberUsed; i > 0; i--) {
cout << "Output in reverse: " << charArray[i] << endl;
}
}
填充数组...输入最多100个字符值。输入句号结束。输入字符1:一种输入最多100个字符值。输入句号结束。输入字符2:b输入最多100个字符值。输入句号结束。输入字符3:C输入最多100个字符值。输入句号结束。输入字符4:d输入最多100个字符值。输入句号结束。输入字符5:Ë输入最多100个字符值。输入句号结束。输入字符6:。条目数:5
阵列输出...反向输出:反向输出:反向输出:反向输出:反向输出: