0

What is wrong with this code?

#include<iostream> using namespace std; int main() { int n,x=0; int arr[x]; cout<<"introdusca un numero: "; cin>>n; while(n!=0){ cout<<"introdusca un numero: "; cin>>n; arr[x]=n; x++; } cout<<arr[x]; return 0;} /* my objective is to crate a code that when I introduce difference number, it save them but if the number is zero then the code "cout" the numbers that I input before the zero. please help.*\

11th Apr 2018, 4:24 AM
Link Skwor
Link Skwor - avatar
6 Answers
+ 5
When you create the array, you must assign the complete length. Since the length is 0, you can not add to it.
11th Apr 2018, 4:29 AM
Ariela
Ariela - avatar
+ 3
Iterate over the elements for(int i=0;i < arr.length;i++){ cout << arr[i]; }
11th Apr 2018, 4:54 AM
Ariela
Ariela - avatar
+ 2
Exacto, lo que dijo Ariela. Cuando creas un array, debes especificar que tan grande serĂĄ. int arr[50]; es posible hacerlo sin establecer un numero determinado de elementos desde un principio, pero implica utilizar vector y alocar memoria dinamicamente. Por ahora, puedes establecer tus arrays con un numero determinado de elementos, por ejemplo 75 ya que probablemente no vayas a ingresar mas de 75 numeros. Suerte!
11th Apr 2018, 4:40 AM
064XP
064XP - avatar
+ 1
/*finally we got it, millions of thanks. this is the code: *\ #include<iostream> using namespace std; int main() { int n=3,x=0; int arr[75]; while(n!=0){ cout<<"introdusca un numero: "; cin>>n; arr[x]=n; x++; } for(int i=0;i <=x; i++){ cout <<endl<< arr[i]; } return 0; }
11th Apr 2018, 5:18 AM
Link Skwor
Link Skwor - avatar
0
thanks I got it, but now how can I "cout " the whole array? mi idioma original es el español.
11th Apr 2018, 4:52 AM
Link Skwor
Link Skwor - avatar
0
thank Ariela I just was trying something more or less like that: #include<iostream> using namespace std; int main() { int n,x=0; int arr[75]; cout<<"introdusca un numero: "; cin>>n; while(n!=0){ cout<<"introdusca un numero: "; cin>>n; arr[x]=n; x++; } for(int t=-1; t!=x; t++){ cout<<arr[t];cout<<endl; } return 0; } it almost worked but I will try with your method.
11th Apr 2018, 5:11 AM
Link Skwor
Link Skwor - avatar