无法打印用户填充的部分数组的反面



对于赋值,我们要用用户定义的字符填充数组,一旦用户输入句号".",该字符就会停止填充。部分作业是反向打印出在数组中输入的字符,但我所拥有的似乎什么也没打印。 第一次问,所以如果这是一个愚蠢的问题,请道歉。提前谢谢。

#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: e 最多输入 100 个字符的值。输入句号结束。输入字符 6: . 条目数:5

阵列输出.... 反向输出: 反向输出: 反向输出: 反向输出: 反向输出:

不确定您要尝试从fillArray((返回什么,但是由于它是布尔类型,因此假设您尝试返回数组是否为空。 观察添加的注释以查看更正。

int main() {
const int arraySize = 100;
//corrected
char charArray[arraySize] = { NULL };
int numberUsed = 0;
//Function calls
cout << "nFILLING ARRAY....n";
fillArray(charArray, arraySize, numberUsed);

cout << "nARRAY OUTPUT....n";
outputInReverse(charArray, numberUsed);
return 0;
}
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;
//corrected: shifted here so before '.' can enter into array we return
if ((inputChar == sentinel)) {
sentinelEntered = true;
cout << "Number of entries: " << (count) << endl;
//correction: update numberUsed  before returning and no of 
//elements = count
numberUsed = i;
return count;
}
//correction: array index should not be "index" but i
charArray[i] = inputChar;
//How many entries made
numberUsed = i;
count++;
}
}
if (numberUsed == arraySize)
return true;
return false;
}
void outputInReverse(const char charArray[], int& numberUsed) {
for (int i = numberUsed-1; i >= 0; i--) {
cout << "Output in reverse: " << charArray[i] << endl;
}
}

最新更新