In C++ take the code below Using command line arguments, add to the the program
ID: 3819810 • Letter: I
Question
In C++ take the code below
Using command line arguments, add to the the program to do the following
If the command is -i, print: Integer, and then print the integer after it.
If the command is -f print: float, and then print the float after it
if the command is -s print: string, and then print the string after it.
If the command is -h print: all the commands, and the syntax
If the command is something not listed, do the same as the -h command.
It doesn't matter the number or the order -- do all the commands that the person typed.
Is someone does a command, but it is not the right syntax -- show the correct syntax.
Example entries into the program:
prog.exe -i 3
prog.exe -f 3.14 -i 6
prog.exe -h
________________________________________________________________________________________________________________________________________
#include <iostream>
#include <iostream>
#include <stdlib.h>
#include<conio.h>
#include <sstream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
string current_exec_name = argv[0]; // Name of the current exec program
string first_arge, second_arge;
vector<string> all_args;
if (argc > 1) {
first_arge = argv[1];
second_arge = argv[2];
if(first_arge=="-i"){
cout<<" Integer :"<< atoi (argv[2]);
}else if(first_arge=="-f"){
float f2 = atof (argv[2]);
cout<<" Float :"<<f2;
}else if(first_arge =="-s"){
cout<<" String :"<< second_arge;
}else if(first_arge =="-h" && second_arge =="-i"){
cout<<" Syntex :"<< "-i [your input]";
}else if(first_arge =="-h" && second_arge =="-f"){
cout<<" Syntex :"<< "-f [your input]";
}else if(first_arge =="-h" && second_arge =="-s"){
cout<<" Syntex :"<< "-s [your input]";
}else {
cout<<"Help: Use Syntex : -i for integer and then value eg: -i 12. -f for float and then your value. -s for String and then your string. for help use -h and then command eg -h [command]. End...... ";
}
}else if (argc != 3){
cout<<"Error! ";
cout<<"Help: Use Syntex : -i for integer and then value eg: -i 12. -f for float and then your value. -s for String and then your string. for help use -h and then command eg -h [command]. End...... ";
}
getch();
}
Explanation / Answer
#include"stdafx.h"
#include <iostream>
#include <iostream>
#include <stdlib.h>
#include<conio.h>
#include <sstream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
string current_exec_name = argv[0]; // Name of the current exec program
int index = 1;
if (argc >= 3&& argc%2!=0)//at least 3 arguments must be there and if geater than 3
// it must be an odd number or it will throw index out of bounds
{
while (index < argc) {
if (argv[index] == "-i") {
cout << " Integer :" << atoi(argv[index + 1]);
}
else if (argv[index] == "-f") {
float f2 = atof(argv[index + 1]);
cout << " Float :" << f2;
}
else if (argv[index] == "-s") {
cout << " String :" << argv[index + 1];
}
else if (argv[index] == "-h" && argv[index + 1] == "-i") {
cout << " Syntex :" << "-i [your input]";
}
else if (argv[index] == "-h" && argv[index + 1] == "-f") {
cout << " Syntex :" << "-f [your input]";
}
else if (argv[index] == "-h" && argv[index + 1] == "-s") {
cout << " Syntex :" << "-s [your input]";
}
else {
cout << "Help: Use Syntex : -i for integer and then value eg: -i 12. -f for float and then your value. -s for String and then your string. for help use -h and then command eg -h [command]. End...... ";
}
index = index + 2;
}
}
else
{
cout << "Error! ";
cout << "Help: Use Syntex : -i for integer and then value eg: -i 12. -f for float and then your value. -s for String and then your string. for help use -h and then command eg -h [command]. End...... ";
}
_getch();
}