Thread: parse/split a string and store it in variables

  1. #1
    StRiKeFoRcEnOoB
    Guest

    Question parse/split a string and store it in variables

    hey im trying to make a program and i need to parse/split a line of code and store it in string variables ex: scan documents\download ---> command=scan; arg=documents download, now i saw another thread similar but when i tried to impliment everything into variable, the program crashes, comment it out, runs perfectly, any ideas?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Never assume code you find somewhere is correct. Try to understand what is there. Other than that its the same old routine. Hard enough to read tea leaves in the cup before me, let alone over the phone.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    I wrote a tutorial on tokenizing in our faq

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Boost also has a StringTokenizer template.

  5. #5
    StRiKeFoRcEnOoB
    Guest
    yes i understand why the code works. the code works on its own, however when i attempt to modify it to put it in a variable instead of printing it out but it gives me a system crash, i put in spot checks and i found out the code chrashes as soon as it hits the point where im assigning the values to the variables, here is what it looks like:

    #include <iostream>
    #include <vector>
    #include <string>
    #include <conio.h>

    void Prompt()
    {
    std::string s;
    std::cin>>s;
    std::string command;
    std::string arg;
    std::cout<<"PROMPT1";
    std::vector<std::string> vec;
    std::cout<<"PROMPT2";
    int start = 0, end = 0;
    std::cout<<"PROMPT3";
    while ( end != std::string::npos ) {
    end = s.find ( ' ', start );
    std::cout<<"PROMPT4";
    vec.push_back ( s.substr ( start, end - start ) );
    std::cout<<"PROMPT5";
    start = end + 1;
    }

    std::cout<<"PROMPT6";
    command = vec[0];
    arg = vec[1];
    std::cout<<"PROMPT7";
    std::cout<<command<<arg;

    if(command=="cls")
    {
    //clrscr();
    }
    else
    {
    std::cout<<"ERROR";
    }
    }

    int main()
    {
    Prompt();
    }

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    code tags...

    put [code] ... [/code] around your source code, so it's readable.

  7. #7
    StRiKeFoRcEnOoB
    Guest

    sorry

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <conio.h>
    
    void Prompt()
    {
    std::string s;
    	std::cin>>s;
    std::string command;
    std::string arg;
    						  	  std::cout<<"PROMPT1";
      std::vector<std::string> vec;
    							  std::cout<<"PROMPT2";
      int start = 0, end = 0;
    							  std::cout<<"PROMPT3";
      while ( end != std::string::npos ) 
     {
        end = s.find ( ' ', start );
    							  std::cout<<"PROMPT4";
        vec.push_back ( s.substr ( start, end - start ) );
    							  std::cout<<"PROMPT5";
        start = end + 1;
      }
    
    							  std::cout<<"PROMPT6";
    command = vec[0];
    arg = vec[1];
    							  std::cout<<"PROMPT7";
    	std::cout<<command<<arg;
    
    if(command=="cls")
    	{
    	//clrscr();
    	}
    else
    	{
    	std::cout<<"ERROR";
    	}
    }
    
    int main()
    {
    Prompt();
    }
    sorry i forgot and since im guest i cant edit

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    Re: sorry

    [B]
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <conio.h>
    
    void Prompt()
    {
    std::string s;
    //std::cin>>s;
    getline(cin, s); // change here
    std::string command;
    std::string arg;
    						  	  std::cout<<"PROMPT1";
      std::vector<std::string> vec;
    							  std::cout<<"PROMPT2";
      int start = 0, end = 0;
    							  std::cout<<"PROMPT3";
      while ( end != std::string::npos ) 
     {
        end = s.find ( ' ', start );
    							  std::cout<<"PROMPT4";
        vec.push_back ( s.substr ( start, end - start ) );
    							  std::cout<<"PROMPT5";
        start = end + 1;
      }
    
    							  std::cout<<"PROMPT6";
    command = vec[0];
    arg = vec[1];
    							  std::cout<<"PROMPT7";
    	std::cout<<command<<arg;
    
    if(command=="cls")
    	{
    	//clrscr();
    	}
    else
    	{
    	std::cout<<"ERROR";
    	}
    }
    
    int main()
    {
    Prompt();
    }
    I made one change at the start of your code, try it now

  9. #9
    StRiKeFoRcEnOoB
    Guest
    ok thx laasunde it now works, so i got the main part down, now how do you evaluate the string? i believe i use:
    Code:
    if(command=="cls")
    	{
    	clrscr();
    	}
    else
    	{
    	std::cout<<"ERROR";
    	}
    but when i compile i get the error when i enter cls home, it gives me the error instead of the clrscreen. any ideas on that?

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by StRiKeFoRcEnOoB
    ok thx laasunde it now works, so i got the main part down, now how do you evaluate the string? i believe i use:
    Code:
    if(command=="cls")
    	{
    	clrscr();
    	}
    else
    	{
    	std::cout<<"ERROR";
    	}
    but when i compile i get the error when i enter cls home, it gives me the error instead of the clrscreen. any ideas on that?
    Are you typing cls with lowercase ?

    You should check out Faq : Clear the screen.

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    1
    Code:
    if(command==string("cls"))
    	{
    	clrscr();
    	}
    else
    	{
    	std::cout<<"ERROR";
    	}
    this works now i need to know this real quick. i got this
    Code:
    command = vec[0];
    arg = vec[1];
    but i get an error if there is nothing stored in vec[1], how can i make an if-else evalutaion to fulfill this pseudocode
    Code:
         if(vec[1]=true] //meaning something is present
          { string arg=vec[1];}
         else
          { string arg;}
    i recall see how to do it before but i can not find my reference guide, so if you can help that would be appreciated

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
      if (vec.size() > 1)
      { 
        // if true, you have more than one parameter...
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM