Read the text the problem actually gives
Choose words or full lines, preserve empty cases, and reset only the state that needs it.
A token and a line are different
std::cin >> s reads the next whitespace-delimited token. It might be a word, a code, or punctuation. std::getline(std::cin, s) reads the whole remaining line, preserving its spaces and discarding the newline.
For HNU ICPC Community, two token reads produce HNU and ICPC, leaving Community unread. One getline receives the entire text. An empty line is a valid string of length zero.
A leftover newline is still input
Reading a number with >> leaves its line ending. An immediate getline may therefore read an empty remainder of that same line. When the format puts the desired text on the next line, ignore through the newline once after the number.
Do not apply this fix blindly. If the text belongs on the number’s line, discarding that line loses useful input. If you only read tokens, no ignore is needed. getline already consumes its delimiter, so repeated line reads do not need repeated ignore calls.
#include <iostream>
#include <string>
#include <limits>
int main() {
int t = 0;
std::cin >> t; // Count on its own line; 1 <= t <= 100.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
for (int test = 0; test < t; ++test) {
std::string line;
std::getline(std::cin, line);
std::cout << line.size() << '\n';
}
return 0;
}Reset state, preserve the input boundaries
For multiple cases, declare each sum, count, or candidate inside the case loop. Reusing an array is fine when the current n cells are overwritten and every scan uses the current n. Old values in inactive cells then do not matter.
This does not mean arrays never need clearing: an array used as accumulated state may need a reset. Understand which values will actually be read next. For line cases, an empty line still counts as a case and must not be silently skipped.
Input is an integer on its own line followed by three lines that may be empty. Where should ignore be called?
Turn the idea into code
Read t cases. Each case gives 1 ≤ n ≤ 1000 integers. Print the maximum of each case. Test one case with maximum 90 followed by the single value −5.
Give me a hint
Initialize the candidate from the first element of the current case, not from an earlier case.
Show the explained solution
The two suggested answers are 90 and −5. Each case’s fresh candidate and active length keep state independent.
#include <iostream>
int main() {
int a[1000]{};
int t = 0;
std::cin >> t;
for (int test = 0; test < t; ++test) {
int n = 0;
std::cin >> n;
for (int i = 0; i < n; ++i) { std::cin >> a[i]; }
int best = a[0];
for (int i = 1; i < n; ++i) {
if (a[i] > best) { best = a[i]; }
}
std::cout << best << '\n';
}
return 0;
}Your notes
Mark complete when you can explain it yourself.