Combining user inputs to put into System()

This is a discussion on Combining user inputs to put into System() within the C++ Programming forums, part of the General Programming Boards category; First off, let it be known that I'm relatively new to programming and VERY new to C/C++. Now that that's ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    Combining user inputs to put into System()

    First off, let it be known that I'm relatively new to programming and VERY new to C/C++. Now that that's over...

    I have a problem with a simple command line program that is meant to collect a few user inputs and then put them into a shell command with flags. The program then puts those values together to form a command like: program.exe -a value -b value -c value.

    The problem is that I can't find a way to use strings in a system() line(the compiler asks for a const char because it can't use a string for System()). I've searched the forum for a while and haven't seen anything to help me(though it seems that the soulution is always right in front of my eyes whenever I ask for help), so sorry if this has already been answered somewhere else.

    Here is what I've got so far:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        char inone[21];
        char intwo[21];
        char inthree[21];
        char app[21];
        string space( " " );
            
        cout<< "What is the name of the executable?: ";
        cin.getline ( app, 21 );                     //name of file to run, assuming it's in 'system32'
        cout<< "What is flag one?: ";
        cin.getline ( inone, 21 );                   //flag, along with a value. eg: "-b 1024"
        cout<< "What is flag two?: ";
        cin.getline ( intwo, 21 );
        cout<< "What is flag three?: ";
        cin.getline ( inthree, 21 );
        cout<< "Your command will be: " << ( app ) << ( space ) << ( inone ) << ( space ) << ( intwo ) << ( space ) << ( inthree ) << "... Continue?";
        cin.get();
        /*here is where I want to put a system() line or something similar.
        the problem is that I can't figure out how to combine the flags and app
        name into a string and put it into that system() line.*/
    }
    In the end, I want to be able to incorporate this into a larger application that will serve as a "frontend" for people new to terminal-based applications(or just lazy people like myself).

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you are writing a c++ program, so for God's sake use c++ std::string instead of C string arrays.

    After collecting the information from use input, you have to generate the final string for the system() function. system() only takes one string argument, so you have to put that all together yourself. Here is a start:
    Code:
    std::string cmd;
    cmd = app + " " + inone + " "  <etc etc>
    
    system(cmd.c_str());

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Thanks a lot for your help! Worked like a charm. If anyone's interested, this is what I was integrating it into:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        string path;
        string namein;
        string nameout;
        string nomrate;
        string setting;
        string cmd;
        string again;
        
        start:
        cout<< "Welcome to oggencFE for the command line vorbis encoder.\n\n";
        cout<< "Let's get started. First, tell me where the file is on your hard drive\nINCLUDING the ending backslash (leave blank for same path as oggenc.exe):\n";
        getline( cin, path );
        cout<< "What file would you like to encode to ogg(not including the .wav extension)?:\n";
        getline( cin, namein );
        cout<< "What do you want the output filename to be (not inluding the .ogg extention)?:\n";
        getline( cin, nameout );
        cout<< "Which encoding method would you like to use?\n\n1 - Nominal bitrate encoding.  This method is best if you are aiming for a\n    specific file size.  Please note that the ogg encoder will still encode\n    in VBR!\n\n";
        cout<< "2 - Managed bitrate encoding.  This option will encode the file with user set\n    min/max bitrates.  It only allows LOOSE control over bitrate, so it isn't\n    generally recommended if you need an exact bitrate.\n\n";
        cout<< "3 - Quality-based encoding.  This will allow you to set a quality from 1-10, and\n    based on that, the encoder will create the audio file.  Lower numbers will\n    result in smaller file sizes, and higher numbers will result in larger file\n    sizes.\n";
        getline( cin, setting );
        if(setting == "1")
        {
             cout<< "What nominal bitrate would you like to use for the output file?:\n";
             cin>>nomrate;
             cout<< "We have all the information needed to encode your file.  Press \nenter to proceed...";
             cmd = "oggenc -b " + nomrate + " --output=\"" + path + nameout + ".ogg\" " + "\"" + path + namein + ".wav" + "\"";
             cin.ignore();
             cin.get();
             system( cmd.c_str() );
             cout<< "\nEncoding of " << ( namein ) << ".wav finished.  Press enter to exit.";
             cin.get();           
        }
    
    }
    It's not really meant for anyone but myself, and more to help me learn the "basics" than for anything else. Thanks again for your help.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Nice job. Don't forget to add #include<string> to your program. It might work now, but that is only because you are lucky. When you use the string class, you need to include <string>.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > cout<< "Let's get started. First, tell me where the file is on your hard drive\nINCLUDING the ending backslash (leave blank for same path as oggenc.exe):\n";
    You can fold long lines like this
    Code:
        cout<< "Let's get started. First, tell me where the file is on your hard drive\n"
               "INCLUDING the ending backslash (leave blank for same path as oggenc.exe):\n";
    > start:
    So where's the goto ?
    Not that you need one, but even having an unused label will make people stare at the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Actually, I was thinking about restructuring the code to use labels instead of chunks of code inside an if statement. Still trying to decide if that's really a good call. I THINK that in the long run, that will make it easier on me, but I can't really be sure. What I'm thinking is that at the end of each "encoding method" section of code, it will have another goto to bring it to a question: do you want to encode another file? Then, of course based on the answer, it will either goto start or goto end.

    As for the wrapping of lines - I had planned that, but I edit it a lot, and since in the end it will be a very small program, I'll make it "pretty" later. At the moment, I just want it to function, but I can totally understand that it's hard to read that way in a browser.

    For something like a yes/no question, where the user has to type in text, is there a way to make the string comparison NOT case sensitive? I tried this in a "Hello world" program, but when I had it ask a yes/no question, I noticed that it will only recognize yes in lowercase if I have the if statment specifically looking for a lowercase "yes". Hope that makes sense... anyone know what I can do to make it compare text without case?
    Last edited by TDMedia; 09-07-2005 at 10:24 PM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The goto may seem like it is easier now, but it is a maintenance headache and is worse in the long run. A better way is to use loops. You can break out of a loop if the user is done. Use functions to store common code or just the code that belongs inside a loop or if block. This will make your code cleaner and as you separate specific tasks into their own functions you can mix and match what needs to be done depending on the state of the program and the user input.

    You can do case insensitive compares, but they aren't simple to do well. One non-standard option is strcmpi or stricmp if it comes with your compiler libraries.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 09:28 AM
  2. creating a user login system for web pages
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-04-2002, 11:02 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Replies: 6
    Last Post: 04-12-2002, 08:33 AM
  5. Using escape sequences as user inputs
    By musayume in forum C Programming
    Replies: 4
    Last Post: 12-11-2001, 08:35 AM

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