I would suggest that you compile your program before you post it here.
There are several problems with your program. Line 8 defines a char array but it is never used or initialized.
Line 11 defines a list, but I believe the end of the definition is incorrect. I looks like your idea is to define the list and set it equal to array "a". Not sure if that can be done. Either way the "[40]" is not needed, still have some testing to do.
Lines 19 - 21, just because they are indented does not make them part of the for loop. For that they need to inside the "{}" of the for loop. Since they are outside the for loop the variable "i" is undefined. Although "i" could be defined outside the for loop I am not sure what the point would be since lines 19 - 21 would work better inside the for loop.
I will have to play wit the program a bit to see if I can get it to work.
#include <iostream>
#include <string>
#include <list>
int main()
{
std::string ary[40]; // <--- Changed to string.
int len{ 0 }; // Length of what is stored in the ary.
//declare list
std::list<std::string> myList;
//std::list < std::string > ::iterator it = myList.begin(); // <--- not needed right now.
std::cout << "Enter 5 lines:\n" << std::endl;
for (int i = 0; i < 5; i++) // <--- Starting at 0 because arrys start at 0.
{
std::cout << i + 1 << ". "; // <---Removed the endl.
std::getline(std::cin, ary[i]); // <--- For using strings.
len = i + 1; // <--- + 1 because its a counter. Do not need 0.
//it++; // <--- Did not want work for me. Caused an error.
}
myList.assign(ary,ary + len); // <--- Assign the arry to the list.
// Prints outthelist/
for (auto lp = myList.begin(); lp != myList.end(); lp++)
{
std::cout << " " << *lp << std::endl;
}
}
I will admit my use of lists is limited, still have some learning to do. If you want to work with "char"s mbozzi's example looks good. See what you think, omeone else may have a better idea.
I do not use lists very often, so yesterday I became focused on the list member functions of ".insert: and ".assign", today I found that like vectors lists have a member function ".push_back". This will add to the end of the list just like a vector.
In my previous message replace lines 15 - 23 with this: