Jump to content

What are flags in C++ and how do I use them?

Wayne Geissinger

Hi! I have a project due in three days that requires me to use two flags: -e (to encode a phrase) and -d (to decode a phrase). I have a pretty good idea of how my encoding and decoding works, but I have no idea what flags are or how to use them. I understand the definition, which is that flags signify that the program will due something when the flag is entered, but how do I actually implement them? Do they work like a normal boolean value, as follows?

 

bool -e;

 

...

 

cin << whatever;

 

...

 

if (whatever == -e){

//statements

}

 

 

or are flags something entirely different from a normal variable?

*_*_*_*_*_*_*Personal Rig*_*_*_*_*_*_*

CPU: Core i7 7700k @ 5.1 GHz

GPU: EVGA GTX 1070 SSC ACX 3.0

MOBO: ASROCK Z270 Killer SLI/AC

RAM: 16 GB HyperX Fury

COOLER: NZXT Kraken X52

CASE: NZXT S340 Elite

STORAGE:  500GB NVME Samsung 960 Pro

500GB Samsung 860 EVO

4TB Seagate Barracuda (7200 RPM)

OS: Windows 10 Pro

 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm going to go out on a limb and assume that your program will have to be run via command line, and in that case the "flags" are really just parameters you pass to the program, such as "program.exe -d Some random phrase". As such, these "flags" are just strings.

Here's an article on how to use them: http://www.cplusplus.com/articles/DEN36Up4/

I need to stop lurking.

Link to comment
Share on other sites

Link to post
Share on other sites

Ok. I am having trouble grasping the concept of argc and argv[]. if argc is the argument count of sorts, how does argv[] work to store multiple arguments? Is it a two-dimensional array, and how would the user signal the console to stop recieving input?

 

*_*_*_*_*_*_*Personal Rig*_*_*_*_*_*_*

CPU: Core i7 7700k @ 5.1 GHz

GPU: EVGA GTX 1070 SSC ACX 3.0

MOBO: ASROCK Z270 Killer SLI/AC

RAM: 16 GB HyperX Fury

COOLER: NZXT Kraken X52

CASE: NZXT S340 Elite

STORAGE:  500GB NVME Samsung 960 Pro

500GB Samsung 860 EVO

4TB Seagate Barracuda (7200 RPM)

OS: Windows 10 Pro

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, SoftPoison said:

I'm going to go out on a limb and assume that your program will have to be run via command line, and in that case the "flags" are really just parameters you pass to the program, such as "program.exe -d Some random phrase". As such, these "flags" are just strings.

Here's an article on how to use them: http://www.cplusplus.com/articles/DEN36Up4/

I have my CMD actually returning things now when i input, but it's complete gibberish. I would screenshot to show you, but I can't seem to comment with a photo. any suggestions? I'll paste my code:

 


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(){
string inputFileName;
string outputFileName;
ifstream inStream;
ofstream outStream;
string keyWord;
string plainWord;
string cipherWord;
char key;
char plain;
char cipher;

for (int i = 0; i < argc; i++)
{
    cout << argv[i] << endl;
}

cin >> key;

}

}

char encipher(char key, char plain)

{ return 0; }

char decipher(char key, char cipher)

{ return 0; }

sorry for the weird formatting.

*_*_*_*_*_*_*Personal Rig*_*_*_*_*_*_*

CPU: Core i7 7700k @ 5.1 GHz

GPU: EVGA GTX 1070 SSC ACX 3.0

MOBO: ASROCK Z270 Killer SLI/AC

RAM: 16 GB HyperX Fury

COOLER: NZXT Kraken X52

CASE: NZXT S340 Elite

STORAGE:  500GB NVME Samsung 960 Pro

500GB Samsung 860 EVO

4TB Seagate Barracuda (7200 RPM)

OS: Windows 10 Pro

 

Link to comment
Share on other sites

Link to post
Share on other sites

Are you actually defining what argc and argv[] are? You have int main() but what you really need is int main(int argc, char* argv[]).

Here's an example of it working (note argv[0] is the command which I ran the program with):

 

thing.png

I need to stop lurking.

Link to comment
Share on other sites

Link to post
Share on other sites

This is likely the functionality you want right here:
 

 

thing2.png

I need to stop lurking.

Link to comment
Share on other sites

Link to post
Share on other sites

21 hours ago, Wayne Geissinger said:

Ok. I am having trouble grasping the concept of argc and argv[]. if argc is the argument count of sorts, how does argv[] work to store multiple arguments? Is it a two-dimensional array, and how would the user signal the console to stop recieving input?

 

Whenever you start a program, the OS provides some sort of preliminary input to the program beforehand called arguments. For example, say I want to run the command robocopy in Windows.

robocopy C:\Users\Foo\Documents C:\Users\Bar\Documents\ /MIR

The first word is the program in question. The rest are arguments. If this were a C/C++ program, they would populate argc and argv[] in main, if you declared main to use said variables. argc contains the number of arguments, 3 in this case (it could be 4, but I'm a little hazy on this). argv[] is a 1-dimensional array containing the arguments, or ["C:\Users\Foo\Documents", "C:\Users\Bar\Documents", "/MIR"] (it may also contain the program name as the first entry... but again I'm hazy on this)

 

argc is useful for quickly making sure the minimum number of arguments have been passed, if required. Your program then has to ensure what's in argv[] is what the program is expecting.

 

Arguments are useful because you may need them beforehand to setup your program, or it's just faster to do it this way than to ask the user what the inputs should be.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, SoftPoison said:

This is likely the functionality you want right here:
 

 

thing2.png

That is perfect. THANK YOU! we are doing vinegere ciphers for a project, and when I can't even remember how to use c-strings it gets a little hard. That is a rather easy implementation that I understand. Thanks :)

*_*_*_*_*_*_*Personal Rig*_*_*_*_*_*_*

CPU: Core i7 7700k @ 5.1 GHz

GPU: EVGA GTX 1070 SSC ACX 3.0

MOBO: ASROCK Z270 Killer SLI/AC

RAM: 16 GB HyperX Fury

COOLER: NZXT Kraken X52

CASE: NZXT S340 Elite

STORAGE:  500GB NVME Samsung 960 Pro

500GB Samsung 860 EVO

4TB Seagate Barracuda (7200 RPM)

OS: Windows 10 Pro

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×