capturing commands at prompt

This is a discussion on capturing commands at prompt within the C++ Programming forums, part of the General Programming Boards category; i am having no luck at all!! i am trying to prompt the user to enter commands at a prompt ...

  1. #1
    Unregistered
    Guest

    capturing commands at prompt

    i am having no luck at all!!

    i am trying to prompt the user to enter commands at a prompt and capture what is entered in to a vector of strings

    the user can enter any number of whitespaces before or in his input

    enter command: (5 spaces) buy (3 spaces) 1000 bushels(2 spaces) corn 3.00

    so i am trying to get 'buy' into the vector at pos[0]
    '1000' in pos [1]
    'bushels' in pos[2]
    'corn' pos[3] and so on and so on

    i know i can capture the entire line with getline(cin, string);
    i know i can get 'buy' by converting string to a stream (iss) and using iss >> cmd;
    but the string entered can have any number of words

    is there a simpler method which will enable me to do what i want??

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the >> operators ignores any leading whitespace and stops input at the next encountered whitespace after the first non-whitespace character that was input.

  3. #3
    Unregistered
    Guest
    yes i think i get that, but the problem for me is that there is an unknown # of words to be placed in the vector

    so i am having trouble using a loop to figure out when to stop

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include "sstream.h"
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
          string word0, word1, word2, word3, word4, temp;
          vector <string> v;    
          string s("  buy    1000 bushels  corn 300 ");
          cout << s << endl;
          istringstream iss(s);  //this works cause i know # words
          iss >> word0;
          iss >> word1;
          iss >> word2;
          iss >> word3;
          iss >> word4;
          
          cout << word0 << " " << word1<< " " << word2 << " " << word3 << " "
               << word4 << endl; 
          
    
          //HOW DO I MAKE THIS WORK??    
    
          string t("  buy    1000 bushels  corn 300 ");
          istringstream jss(t);
          while( **WHAT GOES HERE TO CONTROL LOOP ???? ****)
          {
             jss >> temp;
             v.push_back(temp);
          }
          cout << v[0];
    
          system("PAUSE");
          return 0;
    }
    sorry for caps, just wanted to be clear
    thanks for your response

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Wouldn't while (jss>>temp) work? A stream extraction operation (to my knowledge), is true iff the stream has data to be extracted. I'm away from my compiler, but you could give it a shot.

  5. #5
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100
    I really have know idea of what you are talking about since my only experience would tell me to just do a "cin" but i dont even know what you want...
    Signature is optional, I didnt opt for one.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    in your first post you indicate you wanted

    vector<string> v;
    v[0] = "buy";
    v[1] = "1000";
    v[2] = "bushels";
    v[3] = "corn";
    v[4] = "200";

    but in you second post I think you want this:

    v[0] = "buy 1000 bushels corn 200";
    v[1] = "sell 400 tons soybeans 333";
    v[2] = "buy 1 porkbelly 0.1";

    Which is it?

    if the latter, but input can include any number of spaces before or after tokens then I would try the following. It uses char arrays rather than STL strings because I am not as familiar with STL strings or istringstreams:

    Code:
    char input[256];
    
    cout << "enter order.  Press enter key to terminate the order. ";
    cin.getline(input, 256, '\n');
    
    //parse input string removing "extra" spaces
    char dummy[256];
    int i, j, k = 0;
    
    //ignore any leading spaces in input string
    while(input[i] == ' ')
    {
      i++;
    }
    
    //a single space only between tokens is OK.  Remove others
    int spaceCount = 0;
    for(j = i ; j < input.length(); j++)
    {
      //place each non space char in input into dummy
      if(input[j] != ' ' && count == 0)
        dummy[k++] = input[j];
      else if(input[j] != ' ' && count > 0)
      {
         dummy[k++] = input[j];
         count = 0;
       }
    
      //put first space between tokens of input string into dummy
      else if (input(j) == ' ' &&  count == 0)
      {
         dummy[k++] = input[j];
         count++;
       }
    
       //ignore any consecutive spaces between tokens of input string
       else if(input[j] == ' ' && count > 0)
       { 
          count++;
        }
    }
    
    //there may be an extra space at end of dummy so look for it and 
    //add null chart to end of dummy to make it a string. 
    if(dummy[k - 1] == ' ')
    {
       dummy[k - 1] = '\0';
    }
    else
       dummy[k] = '\0';
    
    //now convert dummy to STL string and add that to your vector
    it may be that string class contains some "automatic function" to do this, but if so I am not familiar with it.

  7. #7
    Unregistered
    Guest
    Thanks for the replies all!

    This is what I ended up with:

    Code:
     while( true )
          {
             if( jss.eof() ) break;
             jss >> word;
             v.push_back(word);            
          }
    code tags end in a /, not \ - Govtcheez

    I saw on another post someone said you have to test the validity of the stream. So here I assume it good, and break if bad.

    I had tried this once, but did not work in Bloodshed or Visual C++. It does work in Code Warrior and in UNIX (at my university) This is only my second programming class and I was not aware of the big difference the choice of compiler makes. I thought they would all be similar. (MY MISTAKE)

    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ and command prompt commands
    By homeyg in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2004, 05:20 PM
  2. Removing spaces from a "string"
    By sytaylor in forum C++ Programming
    Replies: 20
    Last Post: 03-25-2004, 09:14 AM
  3. Replies: 1
    Last Post: 03-11-2003, 04:36 PM
  4. Capturing system commands
    By manwhoonlyeats in forum C Programming
    Replies: 2
    Last Post: 12-07-2002, 06:38 PM
  5. Password prompt in unix w/o \b
    By rafe in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 08:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21