C++循环,用于接收输入以填充数组
C++ for loops for taking in input to fill an array
我需要创建一个程序,它接受 6 个浮点数,最终我必须在数组中从最小到最大对它们进行排序,并删除最大和最小的数字。
#include <iostream>
using namespace std;
int main(){
bool flag;
float score1, score2, score3, score4, score5, score6;
int size;
float scoresheet [6] = {score1, score2, score3, score4, score5, score6};
cout << "Pleaser enter your score for the gymnast: ";
cin >> score1;
while (cin.fail() || score1 > 10 || score1 < 0)
{
cout << "Invalid score!" << endl;
cout << "Pleaser enter your score for the gymnast: ";
cin >> score1;
}
这就是我目前所拥有的。我知道我需要制作一个 for 循环,但我该怎么做,以便在验证输入后,它会将 6 个输入分配给它在数组"记分表"中的位置?提前谢谢。
#include <iostream>
#include <array>
#include <algorithm>
using namespace std;
int main(int argc, char**argv) {
array<float,6> myArray;//create and array of 6 elements
float number;//to store the individual numbers
cout << "Please type 6 numbers: ";
for(size_t i = 0; i < myArray.size(); ++i)
{
if(i==6){
break;
}
cin >> number;
myArray[i] = number;//adding the numbers to the array
}
cout << "nUnsorted array:" << endl;
for(size_t i = 0; i < myArray.size(); ++i)
cout << myArray [i] << " ";
cout << "nnSorted Array:" << endl;
sort(myArray.begin(), myArray.end());
for(size_t i = 0; i < myArray.size(); ++i)
cout << myArray [i] << " ";
cout << endl;
//Smallest number
float smallest = 1000;
for(size_t i = 0; i < myArray.size(); ++i)
{
if(smallest > myArray[i])
smallest = myArray[i];
}
//Biggest number
float biggest = 0;
for(size_t i = 0; i < myArray.size(); ++i)
{
if(biggest < myArray[i])
biggest = myArray[i];
}
cout << "The Smallest number is: " << smallest << endl;
cout << "The Biggest number is: " << biggest << endl;
return 0;
}
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用用户输入在C++中正确填充2D数组
- 通过for循环使用用户输入填充列表
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- Cryptopp:获取密码输入的填充字符串
- 用空格填充 2D 数组(从文件输入)
- 可选类输入中的 C++ 填充向量
- 如何使用文件填充数组并将其与用户输入C 进行比较
- C++用用户输入在循环中填充 char 数组:输入整个字符串时到底发生了什么?
- 从输入文件中填充向量的向量
- 用输入中给出的单词填充字符串
- 如何用键盘输入来填充C++向量
- 在计算使用了多少个数组索引的同时,如何填充一个来自用户输入的字符的数组?(使用cin.get)
- 从文件中读取输入并填充数组以存储数据供以后使用
- 需要帮助在填充了用户输入的向量中搜索最小值
- 如何让这个数组填充我的输入值
- C++ 使用用户输入填充对象向量
- 从用户输入填充二维数组
- 填充硬件输入结构以与 SendInput 一起使用的正确方法是什么