#include <iostream> using namespace std; int main() { int b[]; int i=0,sum=0; while(cin>>b[i]) {sum=sum+b[i]; i++;} cout<<sum<<endl; }//what is wrong in this code //how can I take multiple inputs into an array //I want to take unknown no. Of inputs | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

#include <iostream> using namespace std; int main() { int b[]; int i=0,sum=0; while(cin>>b[i]) {sum=sum+b[i]; i++;} cout<<sum<<endl; }//what is wrong in this code //how can I take multiple inputs into an array //I want to take unknown no. Of inputs

It's showing error in declaration,

16th Sep 2016, 5:21 PM
prasanth pra
prasanth pra - avatar
3 Answers
0
Add for loop for cin. And give limits how many inputs you want. Define sum=0 before that for loop.
16th Sep 2016, 5:29 PM
Geek
Geek - avatar
0
#include <iostream> using namespace std; int main() { int b[]; int i=0,sum=0; for(i=0;i<=limit;i++) cin>>b[i] ; for(i=0;i<=limit;i++) {sum=sum+b[i];} cout<<sum<<endl; }
16th Sep 2016, 5:32 PM
Geek
Geek - avatar
0
for multiple input #include<iostream.h> using namespace std; void main() { int b[],i=0,sum=0; cout<<"Enter value(enter 0 to exit): while(1) { cin>>b[i]; if(b[i]==0) { break; } else { sum+=b[i]; } i++; } cout<<"\nSum of array:"<<sum; } so this is the code,,,you can try
17th Sep 2016, 2:19 PM
Reyad Hossain
Reyad Hossain - avatar