C++ 输入验证:我的程序不能接受负数,但它正在接受负数和I';我真不知道该怎么办 #包括 #包括 使用名称空间std; //功能原型 void sortArray(int数组[],int大小); void showArray(常量int arr[],int size); 无效平均值(整数测试分数[],整数大小); int main() { int*测试分数; 国际货币基金组织,计数; 努姆格拉德斯广场; testScores=newint[numGrades]; cout测试分数[计数]; } Sortaray(测试分数,numGrades); showArray(测试分数、numGrades); 平均值(测试分数、numGrades); 删除[]个测试分数; 测试分数=0; 系统(“暂停”); 返回(0); } //升序函数 int*testScores[]; void排序数组(int数组[],int大小) { 布尔交换; 内部温度; 做 { 交换=假; 对于(整数计数=0;计数数组[count+1]) { temp=数组[计数]; 数组[计数]=数组[计数+1]; 数组[计数+1]=温度; swap=true; } } }while(swap); } //显示阵列函数 void showArray(常量int arr[],整数大小) { 不能
您正在这样做:-C++ 输入验证:我的程序不能接受负数,但它正在接受负数和I';我真不知道该怎么办 #包括 #包括 使用名称空间std; //功能原型 void sortArray(int数组[],int大小); void showArray(常量int arr[],int size); 无效平均值(整数测试分数[],整数大小); int main() { int*测试分数; 国际货币基金组织,计数; 努姆格拉德斯广场; testScores=newint[numGrades]; cout测试分数[计数]; } Sortaray(测试分数,numGrades); showArray(测试分数、numGrades); 平均值(测试分数、numGrades); 删除[]个测试分数; 测试分数=0; 系统(“暂停”); 返回(0); } //升序函数 int*testScores[]; void排序数组(int数组[],int大小) { 布尔交换; 内部温度; 做 { 交换=假; 对于(整数计数=0;计数数组[count+1]) { temp=数组[计数]; 数组[计数]=数组[计数+1]; 数组[计数+1]=温度; swap=true; } } }while(swap); } //显示阵列函数 void showArray(常量int arr[],整数大小) { 不能,c++,C++,您正在这样做:- #include <iostream> #include <iomanip> using namespace std; // Function Prototype void sortArray(int array[], int size); void showArray(const int arr[], int size); void average(int testScores[], int size); int main() { int
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototype
void sortArray(int array[], int size);
void showArray(const int arr[], int size);
void average(int testScores[], int size);
int main()
{
int *testScores;
int numGrades,count;
cout << "How many grades? " << endl;
cin >> numGrades;
testScores = new int[numGrades];
cout << "Please enter the scores below." << endl;
for (count = 0; count < numGrades; count++)
{
cin >> testScores[count];
}
sortArray(testScores, numGrades);
showArray(testScores, numGrades);
average(testScores, numGrades);
delete[] testScores;
testScores = 0;
system("pause");
return (0);
}
//function for ascending order
int * testScores[];
void sortArray(int array[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count]> array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
// display array function
void showArray(const int arr[], int size)
{
cout << " Scores in ascending order." << endl;
for (int count = 0; count < size; count++)
{
cout << " " << arr[count] << "";
}
cout << endl;
cout << endl;
}
// function to get average of the array
void average(int testScores[], int numGrades)
{
float total = 0.0;
for (int count = 0; count < numGrades; count++)
{
total += testScores[count];
}
float average = total / numGrades;
cout << " This is the average of the scores entered." << endl;
cout << endl;
cout << fixed << showpoint << setprecision(2);
cout << " *** " << average << " ***" << endl;
}
这将接受所有正整数和负整数。一种方法是检查数字,如果是正整数,则将其插入数组。步骤1:如果只有正值有效,则停止使用
int
并开始使用unsigned int
。好的,我修复了它。我添加了这个:while(testScores[count]<0)现在我只有一个问题,我能用更多的指针吗?
cin >> testScores[count];