Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

help with blank in put that does not require to hit enter

This topic is 5011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have only one question. Why does the below code require enter to be press twice? this is what I am trying to achive if fdisk is the input and you press enter goes to function if fdisk /[anything here] will display another iteam So I figured that if input 1 was fdisk and input 2 was blank that it would just go so how can I get it that I only have to hit enter once when just fdisk is entered and hit enter only once if fdisk /mbr is entered?
if (input2 == "FDISK")
{
        
 cin.ignore();
getline (cin, pingv); 


            if (pingv == "/mbr")
           {cout <<"MBR suscsefully writen"<<endl;
            menuinput();}
           else 
           if (pingv.empty())
            {
                      FDISK();}
                     
               }      


so another words I want the second input to be optinal and dont want to have to put anything in . Unless there is another way to progream in switches for a command [Edited by - kingpinzs on March 3, 2005 5:29:11 PM]

Share this post


Link to post
Share on other sites
Advertisement
Can you please explain what you are trying to do here? I'm not getting it from your original post. I also recommend using better spacing in your code like this:
if (input2 == "FDISK")
{
cin.ignore();
getline (cin, pingv);

if (pingv == "/mbr")
{
cout <<"MBR suscsefully writen"<<endl;
menuinput();
}
else if (pingv.empty())
{
FDISK();
}
}



That way, it's much easier to line up the braces and check your logic.

Share this post


Link to post
Share on other sites
want I am trying to do is this.

promt c:>
input fdisk
gets a display
or
promt c:>
input fdisk /mbr
gets a display

now I could make it so each ine was it's own input but that is not what I want. I want to beable to add swiches to a command to do other things with the same command. you know like if you were in dos a you typed in format c: /s it would format the drive then add system files to it. But you could also just type in format c: and dont have to add the switch.

Hope that helps describing my issue

Thanks For the help so far

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinz
now I could make it so each ine was it's own input but that is not what I want. I want to beable to add swiches to a command to do other things with the same command.

Going back to what smart_idiot said, you'd need to write a parser that read the input string, and then decided what to do. You probably should use strtok for this. However, the parsing itself is made caek by the fact that each command is separated by a space, but you could support other separation characters. You might also build a parsing tree, pre-sorted in some form to handle multiple switch commands.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <string>

// 'string' is the string you want to tokenize.
// 'delimiters' contains the characters you have seperating the token.
// string is replaced with the untokenized remainder.
std::string getNextToken(std::string &string, const std::string &delimiters = " ")
{
std::string::size_type start = string.find_first_not_of(delimiters, 0),
end = string.find_first_of(delimiters, start);

if(start == std::string::npos)
start = end = string.length();
else if(end == std::string::npos)
end = string.length();

std::string result(string.substr(start, end-start));
string.erase(0, end);
return result;
}

int main()
{
while(true)
{
std::string command, token;
std::cout << "> ";
std::cout.flush();
std::getline(std::cin, command);

while(!(token = getNextToken(command)).empty())
std::cout << "token: '" << token << "'" << std::endl;
}

return 0;
}




Quote:

MBR suscsefully writen


You're not going to fool anyone with spelling like that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!