Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

    #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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Disclamer: this is not standard c++

I have tried your code in an online compiler and apart from your how many variable nog being set, there not much wrong. If you just change the order a bit it works

#include <iostream>
using namespace std;

int main(){
int howMany, max; //defining variables

cout<<"How many numbers do you want to add? (Enter your number and press enter): ";
cin>>howMany;
int num[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;
}
std::cout << max << std::endl; // to output the max
return 0;   

}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading