> what are the things I need to learn
what if the user was asked to input a hundreds of numbers
For this, you'll need to learn about arrays. I suggest you first learn about C-style arrays (int x[3]{};
), and then std::array
(std::array<int, 3> x{};
). You also need to learn about loops.
and should display the greatest among the numbers given
Having to find the largest number in an array is very common. If you want to learn how to do so manually, the other answers here should answer your question. Otherwise, look towards the standard library algorithms std::ranges::max()
(C++20) and std::max_element
.
Examples
Example 1
Here's a program that uses a C-style array and a simple algorithm to get the largest number:
#include <iostream>
int main(){
// Amount of numbers user should input
constexpr int count{ 3 };
std::cout << "Enter " << count
<< " numbers to decide which is the largest:\n";
// The numbers entered by the user
double numbers[count]{}; // Declare and zero-initialize a C-style array of 3 ints
// Get each number from the user and put it in the array
for (int i{ 0 }; i < count; ++i) {
std::cin >> numbers[i];
}
// The biggest number found so far
int max{ numbers[0] }; // Initialize it with the first number
for (int i{ 1 }; i < count; ++i) { // Start at the second element (element 1)
if (numbers[i] > max) { // If the current number is larger than max...
max = numbers[i]; // ...assign it to max
}
}
std::cout << max << " is the greatest among the numbers given\n";
return 0;
}
Note:
-
int numbers[count]{};
This creates a C-style array called numbers
which has count
(3) elements. The first element's "index" is 0 and the last element's is 2. The {}
initializes the values of all of the numbers to 0 (good practice).
-
for (int i{ 0 }; i < count; ++i)
std::cin >> numbers[i];
This loops until i
isn't less than count
(3) and increments i
(++i
) each time. It starts at 0, so it loops 3 (0
1
2
) times. On each iteration, it gets a number from the console and stores it in numbers[i]
.
Example 2
Here's a shorter program that uses the standard library:
#include <algorithm> // ranges::max()
#include <array> // array<>
#include <iostream> // cin, cout
int main() {
// Amount of numbers user should input
constexpr int count{ 3 };
std::cout << "Enter "
<< count
<< " numbers to decide which is the largest:\n";
std::array<double, count> numbers{}; // Declare an array of 3 ints
for (int i{ 0 }; i < count; ++i) {
std::cin >> numbers[i];
}
// Return the largest number in array "numbers"
std::cout << std::ranges::max(numbers)
<< " is the greatest among the numbers given\n";
return 0;
}
Note:
-
std::array<int, count> numbers{};
Declares an array of count
(3) ints and zero-initializes it.
-
std::ranges::max(numbers)
This neat function finds the largest number in numbers
. It was added in C++20 -- if you're using an older compiler, you should use *std::max_element(numbers.begin(), numbers.end())
. If you want to learn how the latter works, you need to learn about iterators and pointers.
Here are some good practices that your tutorial hasn't taught you yet (if it ever will):
DON'T use using namespace std
. It's unsafe because it brings everything in the standard library into global scope. The standard library contains a lot of commonly used identifiers like count
and list
. Bringing these into global scope is dangerous because it can cause naming conflicts.
Don't use copy initialization (int x = 3
). Use uniform/brace/list initialization instead (int x{ 3 }
). The former sometimes makes an unnecessary copy, whereas the latter doesn't. The latter also refuses to do narrowing conversions (e.g. initializing a short
with a long
).
Always initialize variables (do: int x{}
, don't: int x
), even when it seems redundant. If you don't, then the value stored is undefined - it could be anything. Undefined behaviour is hard to debug but luckily easy to avoid.
Use \n
instead of std::endl
. Both do the same, except std::endl
does an extra buffer flush which is slow and unnecessary. \n
is shorter anyways.
DRY -- Don't Repeat Yourself. You have the string " is the greatest among the numbers given"
three times in your code. You could have stored it in a std::string
instead -- then it wouldn't have repeated.
Repeating code is bad, because:
- It's harder to read
- It's harder to maintain (you would have to modify it everywhere it's repeated)
- Maintenance is more error-prone
If I were you, I'd immediately find a different tutorial/book. See this thread.