Thread: My while statement is never ending!!!

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    My while statement is never ending!!!

    This while statement is not cooperating and I am not really sure why. I tried to say while not equal to true and later false, but both produce a never ending loop. I know I posted this before and I got several comments back about different issues with this program. However this question is specific about the while statement.

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    void userPrompt();
    void accessPrompt(char);
    void addContact();
    void deleteContact();
    void CountContact();
    
    
    vector<string> contacts;
    
    void userPrompt()
    {
        char prompt;
        
        cout<<"Enter [a] to add a contact, [d] to delete a contact, [c] to count the contacts and to [e]Exit"<< endl;
        cin >> prompt;
        cin.ignore();
        
        accessPrompt(prompt);
        
    }
    
    void accessPrompt(char user_request)
    {
        
        bool running = true;
        
        while (running != true) {
            
            if (user_request == 'a' || user_request == 'A')
            {
                addContact();
            }
            else if (user_request == 'd'|| user_request == 'D')
            {
                deleteContact();
            }
            else if (user_request == 'c' || user_request == 'C')
            {
                CountContact();
            }
            else if (user_request == 'e' || user_request == 'E')
            {
                running = false;
            }
            else
                cout<<"You did not enter a correct value"<<endl;
        }
        
    }
    
    void addContact()
    {
        string contact;
        cout<<"Add the name of your contact below"<<endl;
        getline(cin, contact);
        
        contacts.push_back(contact);
        
    }
    void deleteContact()
    {
        int position;
        
        cout<<"Where is the contact located that you want to delete?"<<endl;
        cin>>position;
        cin.ignore();
        
        contacts.erase(contacts.begin() + position); //contacts.begin=0 so position will find the contact
        
    }
    void CountContact()
    {
        cout<<"You have"<<contacts.size() << " contact(s)."<<endl;
        
        for (size_t i = 0; i<contacts.size(); i++) {
            cout<< i <<". "<< contacts.at(i) <<endl; //just another way to way to write contacts[i]
        }
        
    }
    
    
    int main(int argc, const char * argv[])
    {
        
        userPrompt();
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    This is outputs addcontact over and over no matter what I input!!!!

    Code:
     void accessPrompt(char user_request)
    {
        
        bool running = true;
        
        while (running == true) {
            
            if (user_request == 'a' || user_request == 'A')
            {
                addContact();
            }
            else if (user_request == 'd'|| user_request == 'D')
            {
                deleteContact();
            }
            else if (user_request == 'c' || user_request == 'C')
            {
                CountContact();
            }
            else if (user_request == 'e' || user_request == 'E')
            {
                running = false;
            }
            else
                cout<<"You did not enter a correct value"<<endl;
        }
        
    }

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It should end when you've entered a 'e' or 'E' anything else should give you an endless loop since you never change running inside the loop.

    Jim

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Looks like I need a switch statement then. I want it to keep asking the user to enter a,d,c or e and if anything else is entered I want to display a invaild statement.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Looks like I need a switch statement then.
    Why do you think a switch statement would help?

    I want it to keep asking the user to enter a,d,c or e and if anything else is entered I want to display a invaild statement.
    Then why aren't you asking the user to enter a value?

    Maybe the loop should be in the userPrompt() function instead of in this function? And maybe this function should return a value to userPrompt()?


    Jim

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by jimblumberg View Post
    Why do you think a switch statement would help?


    Then why aren't you asking the user to enter a value?

    Maybe the loop should be in the userPrompt() function instead of in this function? And maybe this function should return a value to userPrompt()?


    Jim

    I'd do it the other way around... put the function userPrompt() into the loop before the switch. Modify the function userPrompt() to either return the character the user entered, OR pass a pointer to a variable so that userPrompt() can store the value

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Never ending loop
    By mikeman in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2010, 12:03 PM
  2. Ending a game.
    By kpridd in forum C++ Programming
    Replies: 1
    Last Post: 12-08-2009, 08:55 PM
  3. Ending after do while loop
    By Chipmunkey in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2007, 01:55 PM
  4. Ending a C++ program
    By tonevans in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2005, 02:00 AM
  5. The Never Ending Story..
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-03-2001, 01:19 PM