Batch File? [Newbie help]

This is a discussion on Batch File? [Newbie help] within the C++ Programming forums, part of the General Programming Boards category; I'm currently reading the book Problem Solving, Abstraction and Design using C++, by Frank L. Friedman and Elliot B. Koffman. ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Batch File? [Newbie help]

    I'm currently reading the book Problem Solving, Abstraction and Design using C++, by Frank L. Friedman and Elliot B. Koffman. I've taken several programming classes before and never had to deal with batch mode and to my surprise they cover this topic directly after learning how to do simple arithmetic. It's chapter 2.7, if you happen to own the book.

    I was wondering how I get make this program into a batch file:

    Code:
    //File: milesBatch.cpp
    //Converts distance in miles to kilometers
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    const float KM_PER_MILE = 1.609;
    
    float miles, kms;
    
    cin >> miles
    cout  << "The distance in miles is " << miles;
    
    kms = KM_PER_MILE * miles;
    
    cout << "The distance in kilometers is " << kms << endl;
    
    return 0;
    }
    It's a very simple program, all I'm asking is what exactly am I supposed to do with it? The book mentions placing the symbols "> mydata" at the end of the command line that causes your compiled and linked program to execute, but I'm using an IDE, Microsoft Visual C++ Express, and don't know exactly what that means or what to do.

    If someone could give me easy-to-follow, step-by-step instructions of what to do, I would be incredibly grateful.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,163
    That depends on what you mean by "batch mode" and what you consider it means to turn a program into a batch file.

    My interpretation is that batch mode refers to the execution of one or more programs on a computer, to achieve some required result, without human intervention.

    That would normally mean taking input from one or more files and producing output to one or more files. It might also mean executing the program as a background process (although the method of doing that depends on operating system).

    All the "> mydata" does, under unix at least and maybe other operating systems, is redirect output that the program writes to standard output (aka std::cout in C++) to a file named mydata. For your program, it might also be necessary to use some technique so input is taken from some other file, rather waiting for human input.

    Personally, I wouldn't bother with a program as simple as you describe. The last time I ran a program in batch mode, IIRC, it consumed about 25 hours of CPU time - it involved a bit more than reading a single value, multiplying it by a constant, and outputting two lines as a result. The post processing (producing various graphs and analysis) involved some intellectual effort as well.
    Last edited by grumpy; 12-12-2010 at 05:15 AM.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Well, I don't really consider this a priority on my programming fundamentals, but the fact that have this is listed on the second chapter makes me think this is easier then what I'm making it.

    Basically, I am trying to do what you are explaining to me. I want to make this program run without the interaction of the user and instead of getting it's input from a keyboard, it receives it's data from the file "mydata."

    There isn't a particular reason why I am using a simple program. I was just hoping to gain some ground in input/output redirection. I'm just not understanding what this book is wanting me to do.

    Thanks for the help anyways, I'll probably just ask my instructor next semester.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,163
    The techniques for redirecting input and output are operating system (and command line shell or interpreter) dependent. Which means you need to read documentation for your target system.

    Some unix shells would allow what you want with a typed in command of the form "programname < input_data > output_data &" where input_data is the input file, output_data is the output file. This assumes programname reads from standard input and writes to the standard output device. The & simply runs the process in the background.

    However, keep in mind that not all unix shells do it quite this way.

    Under windows, it is often a little trickier redirecting standard input, but the windows command shell happily allows redirection of standard output in the same manner as unix.

    There are various ways you can write your program (eg accept command line arguments that specify the input and output files, read data from other sources).

    Generally, it is pretty easy, once you get past the concerns specific to your operating system. But, as with any software related activity, there is no upper limit on how much complexity you might wish to introduce (eg support various high level and low level I/O mechanisms).
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    After reading your reply I went on a nice long run and I had that 'Ah-hah!' moment.

    I appreciate all the help you've given me. I managed to run my program perfectly. It did end up being much, much easier than I was making it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-11-2010, 11:05 AM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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