2016-02-26 46 views
-6

我需要制作一个程序,它需要6个浮点数,最后我必须在数组中从最小到最大排序它们,然后删除最大和最小数字。C++ for循环接受输入来填充数组

#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个输入分配到它在数组'scoresheet'中的位置?提前致谢。

+0

如果你要单独声明(并使用)'score1','score2'等,为什么还要有一个'scoresheet [6]'? –

+0

带上你的验证循环,把它放在for循环中。使用数组。 –

+0

我认为这是制作数组的正确方法@R_Kapp –

回答

0
#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 << "\n\nSorted 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; 
} 
+0

编辑器*确实*让你编写完整的代码,请检查我的编辑。顺便说一句,为什么你在'size()'上使用'decltype'? “布尔停止”的目的是什么?你似乎从来没有真正使用它。 –

+0

非常感谢法比奥 –

相关问题