Write a program that will read in 10 test scores. Print out all 10 scores, then print the highest score, and then the lowest score.

Ok i got this so far but i do not know how to print the highest or lowest.

#include<iostream>
using namespace std;
int main()
{
int score;
int t[10];
while(cin >> t[10])
{
cout << t[10] << endl;
}
}

Dani AI

Generated

The immediate bug in 's snippet is writing to t[10] — an array of size 10 has valid indices 0..9, so t[10] writes past the end. and are correct that a loop plus tracking min/max is the right direction, and 's note about readable code tags is useful for posting examples. Below is a safer, clearer approach that avoids off‑by‑one mistakes and uses the standard algorithms to get the highest and lowest values.

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    const int N = 10;
    std::vector<int> scores;
    scores.reserve(N);

    int x;
    while ((int)scores.size() < N && (std::cin >> x))
        scores.push_back(x);

    if (scores.empty()) return 0;

    for (std::vector<int>::size_type i = 0; i < scores.size(); ++i)
        std::cout << "Score " << (i + 1) << ": " << scores[i] << '\n';

    std::vector<int>::const_iterator min_it = std::min_element(scores.begin(), scores.end());
    std::vector<int>::const_iterator max_it = std::max_element(scores.begin(), scores.end());

    std::cout << "Lowest: " << *min_it << '\n';
    std::cout << "Highest: " << *max_it << '\n';
    return 0;
}

Notes and quick troubleshooting:

  • Using a container like std::vector avoids manual index mistakes (no t[10] problem).
  • std::min_element / std::max_element return iterators; dereference them for the actual values.
  • If tracking min/max while reading (no storage), initialize with the first valid input or use std::numeric_limits<int>::max() / ::min() before updates.
  • Handle bad input (non-numeric) by checking std::cin.fail() and clearing the stream if necessary.
  • To require exactly 10 scores, check scores.size() == N and report a user-friendly error if not.

This combines the corrections suggested by and with a robust, easy-to-read implementation.

Recommended Answers

All 3 Replies

  1. Use code tags, [code ] /* put code here */ [code]
  2. You code is not doing what you think it is doing.
  3. To make it do what you think its doing, you need to use loop(s).
  4. Create a min and max variable. Whenever the user enters a number
    update the min and max variable so that it points to the correct value.
int t[10];
while(cin >> t[10])

Be careful. You declare an array of size 10. This will begin at t[0] and go up to t[9]. You're trying to put data into location t[10] which isn't part of the array. This may crash your program.

What I assume you want to do is to enter 10 integers into the array. To do this use a for loop like this:

for (int i = 0; i < 10; i++)
{
    cout << "Enter next integer: "; //Don't really need this line
     cin >> t[i];
}

1) Use code tags,

/* put code here */ 
[code]

[/QUOTE] 

Small correction here:
[noparse]
[code]
 /* put code here */

[/noparse]

commented: Indeed. Even some of the moderators seem to give quite unclear instructions on how to use these so-hard-to-get-right code tags. +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.