Why do i get an empty output for my code in C++ ?? Where am I mistaken? [closed]

    #include <iostream>
using namespace std;

int main(){
    int howMany, num[howMany], max; //defining variables
    
    cout<<"How many numbers do you want to add? (Enter your number and press enter): ";
    cin>>howMany;
    
    for(int i=1;i<=howMany;++i){
        cout<<"Enter you number: ";
        cin>>num[i-1];
    }
    
    max=num[0];
    for (int element:num){
        max=(max>=element)?max:element;
    }
    return 0;   
}

well i actually wanted to find the largest number out of a given set of numbers.... i wanted to take an input from a user on how many numbers he wants to add. then i take all those numbers, for that i made a loop. and then i compare each element of that array of numbers with the previous one to find the largest number. i compiled my code, received no errors....and then when i run it i get an empty output. like this:my output img. please help me out!!!


Solution 1:

You declared a variable length array num

 int howMany, num[howMany], max; //defining variables

Variable length arrays is not a standard C++ feature.

Moreover in the declaration you are using an uninitialized variable howMany. So the program has undefined behavior.

Instead of the variable length array you could use the standard container std::vector<int>.

Pay attention to that there is standard algorithm std::max_element in C++ that allows to find the maximum element in an array.

Here is a demonstration program.

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

int main()
{
    size_t howMany = 0;

    std::cout << "How many numbers do you want to add? (Enter your number and press enter): ";

    std::cin >> howMany;

    std::vector<int> num( howMany );

    for ( size_t i = 0; i < howMany; ++i ) 
    {
        std::cout << "Enter you number: ";
        std::cin >> num[i];
    }

    auto it = std::max_element( std::begin( num ), std::end( num ) );

    if (it != std::end( num ))
    {
        std::cout << "The maximum value is " << *it << '\n';
    }
}

Solution 2:

There are 2 problems with your program.

Mistake 1

You didn't initialize the variable howMany. This means it holds an indeterminate value. And using this uninitialized variable as you did when you wrote num[howMany] leads to undefined behavior.

Undefined behavior means anything can happen including but not limited to program being successfully compiled and giving your expected output. But don't rely on the output of a program that has undefined behavior.

For more reading(technical definition of) on undefined behavior you can refer to undefined behavior's documentation which mentions:

there are no restrictions on the behavior of the program.

This is why it is advised that

Always initialize built in types in local/block scope.

Mistake 2

In Standard C++ the size of an array must be a compile-time constant. So for example, consider the following examples:

int n = 10;
int arr[n]; //INCORRECT

The correct way to write the above would be:

const int n = 10;
int arr[n]; //CORRECT

Similarly, in your code,

int howMany, num[howMany]; //INCORRECT because howMany is not a constant expression

Solution

You can solve this by using a std::vector as shown below:

#include <iostream>
#include <vector>
int main(){
    int howMany = 0, max = 0; //defining variables
    
    std::cout<<"How many numbers do you want to add? (Enter your number and press enter): ";
    std::cin>>howMany;
    
    //create a vector of size howMany
    std::vector<int> num(howMany);
    
    //iterate and take input from user
    for(int &elem: num){
        std::cout<<"Enter you number: ";
        std::cin>>elem;
    }
    
    max=num.at(0);
    for (int i = 1; i < num.size(); ++i){
        if(num[i] > max)
        {
            max = num[i];
        }
    }
    std::cout<<"biggest number is: "<<max;
    return 0;   
}