VC++ CMD Console App

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SamV522
    New Member
    • Sep 2008
    • 3

    VC++ CMD Console App

    Alright, so I'm trying to re create Command Prompt and it's sort of working, but it doesn't parse argument's properly. The code and output is below.

    Code:
    Code:
    #include "stdafx.h"
    #include <windows.h>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	while(1){
    	string var = "";
    	cin >> var;
    	if(var == "exit"){
    		break;
    	};
    	int ret = system(var.c_str());
    	if(ret == 0){
    
    	} else {
    
    	};
    		
    	};
    }
    Output:
    Color 0a
    '0a' is not recognized as an internal or external command,
    operable program or batch file.

    Can anyone help me out here?

    p.s: I'm using a Win32 Console App
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    The cin's >> operator stops at any whitespace, but it looks like you are expecting it to stop at a newline only. "color" is read as one command, then in the next iteration, it gets "0a" as another. I'm trying to think of a way to get a whole line from the input stream, but the only way I can think of is this, which looks slow and clunky to me.
    Code:
    string var;
    char gotten;
    while ((gotten = cin.get()) != '\n') {
        var += gotten;
    }
    BTW, you don't need to put semicolons after the closing braces of if statements and loops. That's just for classes and structs.

    Hope this helps some.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by boxfish
      I'm trying to think of a way to get a whole line from the input stream,
      What's wrong with using getline?

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        Awsome, I had no idea that worked with strings. I only knew how to do it with a char* buffer.

        Comment

        • SamV522
          New Member
          • Sep 2008
          • 3

          #5
          Thanks for the help guy's, I've got it working now.
          On a side note, have any of you used LuaBind before?
          If so, how would I implement it into my app to use DoString <code>?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Sorry I haven't used that.

            Comment

            • SamV522
              New Member
              • Sep 2008
              • 3

              #7
              Darn, okay.
              Too Short.

              Comment

              Working...