ThatGuy1073
ThatGuy1073

Reputation: 1

Taking Input from a loop that runs multiple times c++

I'm fairly new to c++ and I am trying and have searched for how to take an input [Integer] that is looped 6 times and find the average, highest, and lowest input [Integer].So simply put, the program will ask for a score from 6 separate people one after another. Any instructions on how to use a loop to generate 6 different outputs would be greatly appreciated. Sorry if I sound simple in how I’m explaining this. The c++ lingo is a slow learning process for me. This is the for loop I am using.

for(double score = 0.0; score < 6; score++)

Upvotes: 0

Views: 12046

Answers (3)

user4574575
user4574575

Reputation:

Use the rand() or srand() function within the for loop and you should get your random values.

Your other option would be to ask the user to manually enter 6 integers using cin>>number;

Hope this helps :)

Upvotes: 1

Arun A S
Arun A S

Reputation: 7016

I think this is what you want

double score[6];
cout << "Enter 6 different scores \n";
for(int i = 0; i < 6; i++)
cin >> score[i];

With this loop, you will have to input 6 values for score.

With this next loop, you can output those 6 values you inputed earlier

for( i = 0; i < 6; i++)
cout << score[i] << "\n";

Upvotes: 1

coderkevin
coderkevin

Reputation: 359

You'll want to use an index for your loop, and then keep separate variables for your findings, as such:

int sum = 0;
int highest = 0;    // this should be set to the minumum score.
int lowest = 1000;  // this should be set to the maximum score.

for (int i = 0; i < 6; i++) {
  // Read the next number
  int number;
  cin >> number;

  // Add it to the sum
  sum += number;
  // Check to see if it's higher than our current highest.
  if (number > highest) {
    highest = number;
  }
  // Check to see if it's lower than our current lowest.
  if (number < lowest) {
    lowest = number;
  }
}

// Now that we've read our six values, print out the results.
cout << "Average: " << (sum / 6) << endl;
cout << "Highest: " << highest << endl;
cout << "Lowest: " << lowest << endl;

Upvotes: 1

Related Questions