help with an exercise

I am sorry that many things are in italian language but i need help. I wanted to create an array where you can insert numbers, then the numbers will appear on the screen. Also the program will ask you if you want to search for a specific number in the array from the numbers you inserted. At the beggining when you write the dimension of the array, if it's from 1 to 7 and then you want to search a number in the array it's okay, but when the dimension of the array is 8 or higher, the program stops and close (maybe it goes in loop, i don't know). Can you help me to find why it's not working with numbers which are 8 or higher.


#include <iostream>
#include <string>
using namespace std;

int main ()
{
int dim=0;
float v[dim];
float somma=0;
float media=0;
int presenze=0;
float numero=0;
string risposta;

cout<<"\tQuanti numeri vuoi scrivere?"<<endl;//definiamo la dimensione del vettore
cin>>dim;

for (int i=1; i<=dim; i++)//cout che ci indica la sequenza dei numeri che inseriamo, nello stesso tempo calcola la somma di tutti i numeri che inseriamo
{
cout<<"inserisci il "<<(i)<<" valore"<<endl;
cin>>v[i];
}
system ("cls");
cout<<"I numeri che hai fornito sono: ";
for (int i=1; i<=dim-1; i++)//il programma ci mostra i numeri che abbiamo fornito
{
cout<<(v[i])<<", ";
}
cout<<(v[dim])<<"."<<endl<<endl;
cout<<"Adesso vuoi cercare quante volte e' \npresente un numero di quelli che hai fornito?"<<endl;
cout<<"si o no"<<endl;
cin>>risposta;
system ("CLS");
if (risposta=="si")
{
cout<<"Che numero vuoi cercare?"<<endl;
cin>>numero;
for (int i=1; i<=dim; i++)
{
if (numero==v[i])
{
presenze=presenze+1;
}
}
}

if (presenze>=1)
{
cout<<"Il numero "<<(numero)<<" e' presente "<<presenze<<" volte."<<endl;
}
else
{
if (risposta=="si")
{
cout<<"Il numero "<<(numero)<<" non e' presente nemmeno una volta"<<endl;
}
else
cout<<"va bene"<<endl;

}
system ("pause");
}
Last edited on
1
2
3
int dim=0;
float v[dim];
cin>>dim
code executes sequentially from top to bottom: when you created the array `dim' was 0, so that is v's dimension.
`v' would not resize itself whenever `dim' changes. It just doesn't work that way.

Now, the size of an array must be know at compile time (when creating the executable, before executing it), so you can't assign it a dimension from user input.
1
2
3
int dim;
cin>>dim;
float v[dim]; //illegal in c++, but some compilers support it 


To solve this you may overdimension the array or use std::vector (which can resize)
1
2
3
int dim;
cin>>dim;
std::vector<float> v(dim); //need to #include <vector> 
To access an element in a vector is the same as with the array: v[i]


for (int i=1; i<=dim; i++)
In c++ index starts in 0, and goes to size-1
for (int i=0; i<dim; ++i)
Thank you very much.
It worked. ^_^
I used this method:

int dim;
cin>>dim;
float v[dim];
As ne555 has already explained, that is illegal by the C++ standard. Yes, some compilers might support it as an extension, but you are not writing standard C++ here.
Which is the standart of those 3 lines?
I am writing on Dev-C++
C++ is exactly defined by international committee, not by DevC++. There are multiple implementations of the C++ language, but a strictly conforming implementation provides exactly all of the semantics and features described by the standard.

Variable-length arrays (VLAs) don't exist in standard C++. Your compiler provides VLAs as a language extension, but this must not be relied upon if you ever change your tool. For that (and other) reasons, using variable-length arrays is discouraged.

vector exists in every standard revision of C++. It is preferred over arrays.
Topic archived. No new replies allowed.