Question

C++. i have a function bool get(int argc, char* argv[], option& opt) ./a.out -a123 -b 56s...

C++. i have a function
bool get(int argc, char* argv[], option& opt)

./a.out -a123 -b 56s -call -0 -uabs these are the arguments
i want to erase the first character '-' and store the output in the option and value string variables.
output will be:
option: = a value : = 123
option: = b value: = 56s
option: = c value := all
option : = 0
option: = u value : = abs
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello, please find the code, code snapshots and output snapshot below.

Code:

//standard c++ libraries
#include<bits/stdc++.h>
#include<string>
using namespace std;

//a c++ class for the string types
class option{
   public:
       string op, value;
};

//get function which is having three parameter values
bool get(int argc, char* argv[], option& opt){
   int i;
   for(i = 1; i < argc; i++){
       string s = argv[i];
       opt.op = s[1];
       opt.value = s.substr(2);
       if(opt.op != ""){
           cout<<"option: = "<<opt.op<<" ";
       }
       if(opt.value != ""){
           cout<<"value: = "<<opt.value;
       }
       cout<<endl;
   }
   return true;
}

//main method to call get method
int main(int argc, char* argv[]){
   option opt;
   get(argc, argv, opt);
   return 0;
}

=======================
Code Snapshot:

=======================

Output snapshot:

=======================

Hope you understood the code. In case of any query, please feel free to ask. And please don't forget to give a like to this explanation.

Add a comment
Know the answer?
Add Answer to:
C++. i have a function bool get(int argc, char* argv[], option& opt) ./a.out -a123 -b 56s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The main(int argc, char *argv[]) function will perform the following: • Evaluate whether value in argc...

    The main(int argc, char *argv[]) function will perform the following: • Evaluate whether value in argc is greater or equal to 3. • If it is not, then print the following message: Incorrect number of arguments - please call with assignment min max where assignment is the name of your executable and min/max define the range of numbers for your array. • If argc is ≥ 3 convert the values for min and max into integers and store them in...

  • implement the following function: void BuildString(int argc, char* argv[]); 1. Arguments passed through the function. Concatenate...

    implement the following function: void BuildString(int argc, char* argv[]); 1. Arguments passed through the function. Concatenate the strings arguments using strcat, display the concatenated string. 2.. If only one argument passed, display the string. 3. If no argument passed, display the message, no argument passed. In main do the following: 1. Call the function BuildString, passing the proper arguments (argc and argv).

  • #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1;...

    #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function  

  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

  • #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='...

    #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='z';      pc=&(c[8]);      pk=&(k[0]);      for (i=0; i<9; i++)     {           *pc=a-(char)i;           pc--;           *pk=(int)a-i;           pk++;           }                     return 0; }//end of main Answer the below questions with the above code Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a...

  • I have included what I need to do for part b, and what I have so...

    I have included what I need to do for part b, and what I have so far. I 2.2 Part B Write a default version of program to report the behavior of the Linux kernel by inspecting kernel state. The program should print the following values to the user screen or console: CPU type and model Kernel version Amount of time since the system was last booted, in thefomdmm:5s (for example, 3 days 13 hours 46 minutes 32 seconds would...

  • Programing In C Add Command Line Arguments (Argc and Argv) and use files to the following...

    Programing In C Add Command Line Arguments (Argc and Argv) and use files to the following program. My program is called Ultimo.exe and it is in (C:\Users\Dell\source\repos\Ultimo\Debug). The file used is a .txt file in the directory of the .exe program. The text file which is address.txt is at the same location as Ultimo.exe (C:\Users\Dell\source\repos\Ultimo\Debug). The program takes information in the address.txt file and sorts it by zip code, from smallest to largest. The program works by using input/output redirection...

  • I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not...

    I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not declared in this scope" in my main.cpp. Here are my codes. main.cpp #include <iostream> #include <string> #include "functions.h" using namespace std; //definition of the main function. //takes arguments from the command-line. int main(int argc, char *argv[]) { //Determine if you have enough arguments. //If not, output a usage message and exit program if (argc<2 || (argc == 2 && argv[1][0] == '-')) { //call...

  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

  • #include <stdio.h> .. int main(int argc, char *argv[]) { int base, power; printf("enter base and power:...

    #include <stdio.h> .. int main(int argc, char *argv[]) { int base, power; printf("enter base and power: "); scanf("%d %d", &base, &power); while (base != -100){ double res = pow(base, power); double res2 = my_pow(base, power); printf("pow: %.4f\n", res); printf("my_pow: %.4f\n", res2); .... } return 0; } // this function should be RECURSIVE // should not use any loop here double my_pow(double base, double p) { } lab4pow.c file contains: 2.1 Specification Write an ANSI-C program that reads input from the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT