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