L
L
Leo2020-12-10 22:59:57
C++ / C#
Leo, 2020-12-10 22:59:57

How to implement optional input?

How to make an optional input?

#include <iostream>

using namespace std;

int a,c;
char b;

int main()
{
   cout << "Input a:";
   cin >> a;
   cout << "Input b(optional):";
   cin >> b;
   cout << "Input c:";
   cin >> c;

   cout << a << " " << b << " " << c;

}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Ananiev, 2020-12-11
@SaNNy32

Through if

if (/*условие*/){
cout << "Input b(optional):";
cin >> b;
}

F
FirststepsRu, 2020-12-11
@FirststepsRu

Creating an if for input b is correct, but not complete. It is necessary to define the command line arguments in main, then when an argument is specified on the command line, the condition inside the program will be executed. True C code :)

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {
    int opt_b = 0;
    if (argc == 1) printf("No options\n");

    if ((argc > 1) && (strcmp(argv[1],"b")==0)) {
         opt_b = 1;
    }

    if (opt_b == 1) {
        printf("Option b\n");
    }
    return 0;
}

Then, when you call the program, you set the options.
[email protected]:~# ./a.out
No options
[email protected]:~# ./a.out b
Option b

I
Ivan Tataush, 2016-12-21
@beduin01

.Myheader has a width of 100% and also has a 1px border.
The border is added to the total width of the box and you have a width of 100% + 2px
Add box-sizing: border-box;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question