Im having some trouble with an Array in C++.
See I want to let the user give the program an input in form of a String, and keep doing it until the user is satisfied. My input is working fine but when i want to store the strings in to an array im running in to some problems. I have to define a size for my array apparently? and is there a way to store the input in 2 or 3 different arrays (depending on the input, which i sort with some if-statements) of strings, and the print them out?
My code looks something like this now..
string firstarray[10];
string secarray[10];
//The cin stuff here and reading strings from user-input
if(MyCondition1){
for(int x = 0; x<=9;x++){
firstarray[x] = name;
}
if(MyCondition2){
for(int x = 0; x<=9;x++){
secarray[x] = name;
}
Is there a way to skip the 10-limit of an array? could it be like string
firstarray[];
?
You’re looking for a std::list. Or better, a std::vector which lets you access elements by their position.
Both of them can be expanded dynamically.
About your second question:
You can of course create several arrays / vectors to put your strings in. Maybe even use a std::map of type
map<key, vector<string>>wherekeycan be an enum for the category (or a string, but enum is better).You put a new value into one of the vectors:
Then to output this, you can simply loop over each category and vector element