Please look at this code (and forgive the lack of knowledge). It outputs errors that I couldn’t solve. I need to declare a vector of elements of struct C,but I need the number of elements be i (a input of type int).
I also tried others approaches but in all of them I received an error (cannot convert C to int,etc). How can I do this?
# include < iostream > using std::cout; using std::cin; using std::endl; # include < vector > using std::vector; struct C{ int cor; vector<int>cores; }; void LerVector( vector< C> &array ) ; int main () { int n; bool done=false; bool don=false; vector<C>cidade; int i; while(!done){ cout<<'Entre o número de cidades '<<endl; cin>>n; if(n>500) { cout<<endl; cout<<'O número máximo é 500'<<endl; } else done=true; } n--; while(!don){ cout<<'Entre o número de confederações'<<endl; cin>>i; if(i>100){ cout<<endl; cout<<'Número máximo de 100 cidades'<<endl; } else { LerVector( cidade) ; don=true; } } cin.get(); return 0; } //resolve... void LerVector( vector< C> &array ) { for ( size_t i = 0; i < array.size(); i++ ) cin>>array[i]; } // end function inputVector
Let’s try with an explanation 🙂
That tries to extract from
cininto an object of struct C. Well, so it needs an operator>> that actually does that work:In addition, as another one mentioned, you have to actually add the elements to the vector first:
For the next time, please format the text (correct indent it). It was hard for me to step through it 🙂