Hi there, i was trying to make a program (to do)using list class here
is the code

using namespace std;
void display(const list<string>& lsy){
    list<string>::const_iterator iter = lsy.cbegin();
    int i = 0;
    while (iter != lsy.end()){
        cout <<++i<<"- "<< *iter << endl;
void add(list<string>&ls){
    string name;
    cout << "Enter the task : "; 
    getline(cin, name);

int main(){
    //cout << "1- add item 2- remove item 3- display items 4- terminate";
    bool active = true;
    unsigned int command;
    while (active){
        cout << "\n> "; cin >> command;
        if (command == 1)add(items);//here is the problem !
        else if (command == 2);// remove();
        else if (command == 3)  display(items);
        else if (command == 4) active = false;
            cout << "bad command...try again";


    return 0;
//make a display function
//add items
//remove item
//clean all items(restart)

the add item function works fine individually when i tested it , but when i insert it inside the if statements or using switch
when i choose command==1 to get into the function , the program reaches the cout statement then it returns to main as it didn't see the rest of the code :/ ,test it please and what's the problem and how to fix it?!

Edited 2 Years Ago by Yahia Farghaly: wrong spelling

Have you tried single-stepping through the code in your debugger? If you don't know how to use a debugger, it is time to learn! We could analyze your code for you, but it is "better to teach one how to fish, than just give them a fish"... :-)

thank you for your reply, i already know some of dubugging tools in MS visual studio, and i used them to figure out what's gonne on but i just realize the problem and i don't know why this is happening..?

Your most obvious issue is this: else if (command == 2);

Since you dont do anything is this branch, you need to remove the trailing semi-colon. Also, since this branch does nothing, why not just remove or comment it out?

Final comment. ALWAYS bracket all conditional statements (branches). It eliminates a lot of problems like this. IE:

 while (active)
    cout << "\n> "; cin >> command;
    if (command == 1)
        add(items); //here is the problem !
    else if (command == 2)  // remove();
        // nothing to do here is obvious.
    else if (command == 3)
    else if (command == 4)
        active = false;
        cout << "bad command...try again";

As for the fact that add(items) is your problem, in that function you do no error testing. In my experience, error testing should be about 50% of your code for reliable system software.

Edited 2 Years Ago by rubberman

thank you for your advice and i did it and still doesn't work, i found that the problem lies at getline(cin,name) , it doesn't read what i type , it takes it as and empty string and put it inside the list class.

This question has already been answered. Start a new discussion instead.