1

I'm new to C++ and I'm trying to create a program that accepts command through command line.


So My program starts like ./program -f filename -m tablesize. I found that these arguments could be processed by passing arguments to the main() function, namely, by doing main(int argc, char** argv).
However, after the program starts, I would like to make it be able to accept input command such as l key, which would look up a certain value of ID key. So, how should I make my program being able to accept a variety of commands after it started?


My Guess: Can I just accept input and parse the input ? I am given that I cannot use any STL to accomplish the work.

codeMike
  • 65
  • 7
  • This cannot be done without using the standard library (although I suppose you could use a non-standard library like ncurses). – john Feb 02 '21 at 14:35
  • @john, Hi! I think I can do this by directly taking input ? And I parse the input by myself – codeMike Feb 02 '21 at 14:36
  • How are you going to 'directly take input' without using the standard library? – john Feb 02 '21 at 14:37
  • @john, sorry for the confusion. I cannot use STL instead of standard library :) – codeMike Feb 02 '21 at 14:38
  • just to clarify, you are not allowed to use functions such as getch() ? – Sven Nilsson Feb 02 '21 at 14:38
  • OK, well then yes, you can directly take input and parse it yourself. Presumably no STL means no C++ strings, so you will have to use char arrays, but it's perfectly possible to work like this. – john Feb 02 '21 at 14:40
  • @Nathan There is no "STL", there is only the standard library. If your teacher prohibits the use of "STL", they need to define which parts of the standard library they believe that is. – molbdnilo Feb 02 '21 at 14:40
  • @molbdnilo, Hi! I found that STL means 'standard template', which includes mostly data structures. – codeMike Feb 02 '21 at 14:44
  • @john (Bit nitpicky now) It would be possible to work (on most systems) without the STL and even without Standard C libraries if we'd know the target system on which the program has to run. – Brotcrunsher Feb 02 '21 at 14:49
  • 1
    @Nathan Standard Template Library (STL) is an obsolete term that technically should no longer be used since (most of) what used to be the STL has been incorporated in the standard library. – john Feb 02 '21 at 14:57
  • Accepting and using command line parameters does not in any way prevent you from using `std::cin` or `std::cout` – drescherjm Feb 02 '21 at 15:50
  • STL must mean nothing in the std:: namespace since even cin is an object created from a template. So you will have to use the stuff in stdio.h – Jerry Jeremiah Feb 02 '21 at 21:04

1 Answers1

1

It's going to depend upon what your teacher means by "no STL". But let's take your teacher at face value. You have different ways of getting input.

You can use old-school the old-school C library. See this:

How to read a line from stdin, blocking until the newline is found?

You can also use C++ streams:

#include <iostream>
#include <string>

using namespace std;

int main(int, char **) {
    string line;
    getline(cin, line);
    cout << "Line: " << line << endl;
}

I think both of these methods would work for you. I'd use the 2nd.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36