Thread: menu help

  1. #1
    Unregistered
    Guest

    menu help

    hi, I am trying to take a command input by the user and continue looping until the user types exit

    within the loop there are 5 keywords which will send throw control elsewhere

    it works the first time through

    i think the problem is that the strings holding the input are not cleared on subsequent iterations

    i have tried various things with no success

    could someone offer me some suggestions on how to fix this?

    or

    is there (i'm sure there is) a better way in which a beginner (me) can do this with the most basic of skills.

    Thanks

    Code:
    while(true)
      {
        string str, cmd, titl;         //str entire input
        int loc;                       //cmd first word - determines where to send
        cout << "Enter command: ";     //titl text after cmd
        getline(cin, str);
        loc = str.find(" ", 0);
        cmd.append(str,0,loc);
        titl.append(str,loc,titl.size()-1);
        
        //cout << "1st word: " << cmd << endl << "text: " << titl <<endl;
        
           char c_cmd1[80];                     //convert string to lower
           const char *c_cmd = cmd.c_str();     //  c_cmd(c_style string)
           strncpy(c_cmd1, c_cmd, 79);           // c_cmd1 lowercase
           strlwr(c_cmd1);                      //  c_cmd2 is
           string c_cmd2(c_cmd1);               //converted back to string class
           
           //cout << c_cmd << endl << c_cmd1 << endl << c_cmd2 << endl;
      
        if(c_cmd2 == "took")
          took();
          //pass to took function, send string title
          
        else if(c_cmd2 == "clear")
          clear();
          //pass to clear function, send string title
        else if(c_cmd2 == "list")
          list();
            //pass to list function
        else if(c_cmd2 == "advice")
          advice();    
            //pass to advice function
        else if(c_cmd2 == "exit")
          break;
        else huh();
      } 
    
    //Function Took
    void took()
    {
     cout << "took" << endl;
    }
    
    //Function Clear
    void clear()
    {
     cout << "clear" << endl;
    }
    
    //Function List
    void list()
    {
     cout << "list" << endl;
    }
    
    //Function Advice
    void advice()
    {
     cout << "advice" << endl;
    }
    
    void huh()
    {
     cout << "Huh??" << endl;
    }

  2. #2
    Unregistered
    Guest
    after looking at it some more it seems that it only works when there are two words input separated by a space

    when one word is input it crashes

  3. #3
    Unregistered
    Guest
    I think I fixed it,

    added the following if statement:

    Code:
    if(loc < str.size() )
          titl.append(str,loc,titl.size()-1);
    but would still like to hear suggestions on improving code if anyone is up for it

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM