我正在编写一些代码来填充用户输入的数组,然后我将数组传递给一个函数(如下所示),该函数将有关该数组的信息打印到控制台。
我想要打印的信息应如下所示:
array[5] {1, 1, 2, 1, 5}
newArray** = new short*[numRows] //10 rows, one for each single digit including zero
for (int i = 0; i < numRows; i++)
{
newArray[i] = newArray[numColumns];
}
...
cout << "There were " << newArray[leftCounter][1] << " instances of the number " << newArray[leftCounter][0] << ". " << endl;
输出:(当leftCounter = 1时的例子)
“数字1有3个实例。”
所以我构建了代码,但是cout语句的for循环引发了各种错误。我已经看了一会儿,我不知道我做错了什么。
我没有看到任何遗漏; s。 {}是正确的,我没有意外地将循环放在范围之外或任何东西。我没有使用名为leftCounter的变量。
这是我为完成数组打印而编写的代码。
vector<short> numberCounter(short input, vector<short> array);
void displayArray(vector<short> array);
... main() ... numberCounter ... main()... displayArray(array);
void displayArray(vector<short> array)
{
size_t arraySize = (sizeof(array) / sizeof(short));
size_t numColumns = 2;
size_t numRows = 10;
//First, I'm building a dynamic array that's equal the to size of my first array.
short** newArray = new short*[numRows];
//For each row, I want two columns.
for (int i = 0; i < numRows; i++)
{
//The second dimension of my array will have two values. The number, 0-9, and the number of time each of those numbers occurs in the array.
newArray[i] = new short[numColumns];
newArray[i][0] = i; //newArray[i][0] should equal {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
newArray[i][1] = 0; //newArray[i][1] should all be initialized to 0 (so I can incriment them with the numbers of occurrances each of the digits.
}
//For each unique number in the array[], I'm incrimenting the "counter" by one. So if array[j] = 4, then newArray[4][0] would incriment by one, which adds to my count of 4s found in the first array.
for (int j = 0; j < arraySize; j++) //**edit 3: deleted the k = 0, I wasn't using that code anymore.**
{
newArray[array[j]][1]++;
}
cout << "Here are the counts of your number inputs." << endl;
//Now, I will need to loop through my new array to display the counts the number of the numbers that occured at least once.
for (int leftCounter = 0; leftCounter < arraySize; leftCounter++)
{
//If the counter was incrimented (thus, no longer has a value of zero)
if (newArray[leftCounter][1] != 0)
{
cout << "There were " << newArray[leftCounter][1] << " instances of the number " << newArray[leftCounter][0] << ". " << endl;
}
}
// free dynamically allocated memory
for( int row = 0 ; row < numRows ; row++ )
{
delete[] newArray[row];
}
delete[] newArray;
}
此代码产生以下错误:
编辑3:我更新了这些错误,以显示错误的位置。
Error C2086 'int leftCounter': line 76 {cout for loop}
Error C2447 '{': missing function header (old-style formal list?) line 77 {the braces of the cout for-loop}
Error C2447 '{': missing function header (old-style formal list?) line 87 {the braces of the cout for-loop}
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Lab10_Exercise2 line 73 {cout statement before my cout for-loop}
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Lab10_Exercise2 line 76 {cout for-loop}
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Lab10_Exercise2 line 86 {delete[] for loop}
Error C2059 syntax error: ')' line 68 {incrimentor for loop}
Error C2059 syntax error: ')' line 76 {cout for-loop}
Error C2059 syntax error: ')' line 86 {delete for-loop}
Error C2059 syntax error: ';' Line 70 {incrimentor for-loop}
Error C2059 syntax error: '<' line 86 {delete for-loop}
Error C2059 syntax error: 'delete' line 91 {delete[] newArray[]}
Error C2059 syntax error: 'for' line 76 {cout for-loop}
Error C2059 syntax error: 'for' line 86 {delete[] for-loop}
Error C2059 syntax error: '}' line 93 {end of file}
Error C2143 syntax error: missing ')' before ';' line 76 {cout for-loop}
Error C2143 syntax error: missing ')' before ';' line 86 {delete for-loop}
Error C2143 syntax error: missing ';' before '++' line 76
Error C2143 syntax error: missing ';' before '++' line 86
Error C2143 syntax error: missing ';' before '<' line 76
Error C2143 syntax error: missing ';' before '<' line 86
Error C2143 syntax error: missing ';' before '<<' line 73 {cout statement above the for loop}
Error C2143 syntax error: missing ';' before '{' line 77
Error C2143 syntax error: missing ';' before '{' line 87
Error C2143 syntax error: missing ';' before '}' line 93
Error C2062 type 'short' unexpected line 68
编辑:为清晰起见:违规的for-loop就是这个:
for (int leftCounter = 0; leftCounter < arraySize; leftCounter++)
{
//If the counter was incrimented (thus, no longer has a value of zero)
if (newArray[leftCounter][1] != 0)
{
cout << "There were " << newArray[leftCounter][1] << " instances of the number " << newArray[leftCounter][0] << ". " << endl;
}
}
发生什么事了?我错过了一些明显的东西吗?
编辑2:delete [] for循环也引发了编译器错误。似乎当我在循环中声明整数时,它告诉我我有重新定义。
编辑4:我在此文件中的所有代码。
#include <iostream>
#include <vector>
using namespace std;
vector<short> numberCounter(short input, vector<short> array);
void displayArray(vector<short> array);
int main()
{
vector<short> shortArray;
short unsignedShort = 0;
while (unsignedShort != 10)
{
cout << "Please enter a positive, single digit integer." << endl;
cout << "Type 10 to submit your choices." << endl;
cin >> unsignedShort;
if (cin.fail() || ((unsignedShort < 0) || (unsignedShort > 10)))
{
//Clear the inputbuffer
cin.clear();
//Ignore the erroneous input
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please type an integer." << endl;
}
else if (unsignedShort == 10)
{
break;
}
else
{
shortArray = numberCounter(unsignedShort, shortArray);
}
}
displayArray(shortArray);
}
vector<short> numberCounter(short input, vector<short> array)
{
array.push_back(input);
return array;
}
void displayArray(vector<short> array)
{
size_t arraySize = (sizeof(array) / sizeof(short));
size_t numColumns = 2;
size_t numRows = 10;
//First, I'm building a dynamic array that's equal the to size of my first array.
short** newArray = new short*[numRows];
//For each row, I want two columns.
for (int i = 0; i < numRows; i++)
{
//The second dimension of my array will have two values. The number, 0-9, and the number of time each of those numbers occurs in the array.
newArray[i] = new short[numColumns];
newArray[i][0] = i; //newArray[i][0] should equal {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
newArray[i][1] = 0; //newArray[i][1] should all be initialized to 0 (so I can incriment them with the numbers of occurrances each of the digits.
}
//For each unique number in the array[], I'm incrimenting the "counter" by one. So if array[j] = 4, then newArray[4][0] would incriment by one, which adds to my count of 4s found in the first array.
for (short j = 0; j < arraySize; j++)
{
newArray[array[j]][1]++;
}
cout << "Here are the counts of your number inputs." << endl;
//Now, I will need to loop through my new array to count the number of non zero elements.
for (int leftCounter = 0; leftCounter < arraySize; leftCounter++)
{
//If the counter was incrimented (thus, no longer has a value of zero)
if (newArray[leftCounter][1] != 0)
{
cout << "There were " << newArray[leftCounter][1] << " instances of the number " << newArray[leftCounter][0] << ". " << endl;
}
}
//Free dynamically allocated memory
for (short row = 0; row < numRows; row++)
{
delete[] newArray[row];
}
delete[] newArray;
}
答案 0 :(得分:1)
帕维尔发现了这个问题。谢谢你!代码中的另一个问题:size_t arraySize =(sizeof(array) /
的sizeof(短));会给出错误的大小(因为vector不仅包含 价值观,还有一些其他信息)。使用size_t arraySize = array.size(); - Pavel 11分钟前