Hello noobstudent,
I finally managed to get something to compile and run and this is what I found.
At first when I asked about the value of "toDoList" in line 20 As I suspected it it empty at least as far as the vector is concerned.
The code:
1 2 3 4 5
|
toDoList.lists).push_back(List());
//toDoList.lists[listnumber].name) = "0";
getline(cin, toDoList.lists[listnumber].name);
|
Does take care of this problem. Line 3 is not necessary. There is no need to give "name" a value just to overwrite the value on the next line.
The segmentation fault comes from your for loop.
The first time in the function "listnumber" has a value of (0) zero. in the for loop:
1 2 3 4 5 6 7
|
for (i = 0; (i == listnumber) || (match == true); i++)
{
if ((toDoList.lists[listnumber].name) == (toDoList.lists[i].name))
{
match = true;
}
}
|
This starts with "i == listnumber" which is true, so the rhs is never checked and you enter the loop.
At this point:
if ((toDoList.lists[0].name) == (toDoList.lists[0].name))
which evaluates to true, so you enter the if statement and set "match" to true.
The second time through the loop you have:
(i == listnumber) || (match == true)
. Here the lhs is false, so it checks the rhs which is true and will always be true until it creates a segmentation fault to crash the program. BTW "match" never changes here until you get back to the top of the do/while loop.
Inside the for loop:
if ((toDoList.lists[0].name) == (toDoList.lists[1].name))
, but since this is the first time through the function the vector "lists" only has 1 element, so the rhs of == is trying to check an element that does not exist yet.
Not sure what you want here:
while (toDoList.lists[listnumber].name.empty() || (match == true));
? The lhs should always be false because by the time that you reach here "name" will have something in it. The rhs will always be true because the value of "match" has not changed yet, so the whole condition will evaluate to true, always.
I am thinking that B4 the closing } of the do/while loop you do need to move "listnumber++;" inside the do/while loop. Unless the function is to run only 1 time then you will need to change the while condition to end the loop.
Like
seeplus said I do not know what "error(ERR_EMPTY);" and "error(ERR_LIST_NAME);" are or what to do with them. The first I kind of understand because the "std::getline()" will allow you to press "Enter" and create an empty string, but maybe you would want this check after the "std::getline()" to check if an empty string was entered, print out an error message for not allowing an empty string and allow the user to enter something B4 continuing.
Andy