Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am using visual studio and I am kind of a beginner at programing In C++ you wi

ID: 3709808 • Letter: I

Question

I am using visual studio and I am kind of a beginner at programing

In C++ you will be making a program that can take command like args.

Using command line arguments, allow 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

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

void printAll()
{
   cout<<"All the valid commands are :"<<endl;
   cout<<"1. -i {integer}"<<endl;
   cout<<"2. -f {float}"<<endl;
   cout<<"3. -s {string}"<<endl;
   cout<<"4. -h"<<endl;
   return;
}
void printCorrect(string c)
{
   if(c=="-i")
       cout<<"The correct syntax is -i {integer} ";
   else if(c=="-f")
       cout<<"The correct syntax is -f {float} ";
   else if(c=="-s")
       cout<<"The correct syntax is -s {string} ";
   else if(c=="-h")
       cout<<"The correct syntax is -h ";
   return;
}
int checkInt(string s)
{
   for(int i=0;i<s.length();i++)
   {
       if(int(s[i])<48 || int(s[i])>57)
           return false;
   }
   return true;
}
int checkFloat(string s)
{
   for(int i=0;i<s.length();i++)
   {
       if(!((s[i]>=48 && s[i]<=57) || s[i]==46))
           return false;
   }
   return true;
}
int main (int argc, char *argv[] )
{
   if(argc>0)
   {
       int i=1;
       string input[argc];
       for(int i=0;i<argc;i++)
           input[i]="";
       while(i<argc)
       {
           int len = (unsigned)strlen(argv[i]);
           for(int j = 0; j < len; j++)          
               input[i] = input[i]+argv[i][j];
           i++;
       }
       for(int i=1;i<argc;)
       {
           if(input[i]=="-i" && checkInt(input[i+1]))
           {

               int x;
           char charr[1024];
               strcpy(charr, input[i+1].c_str());
           sscanf(charr, "%d", &x);
               cout<<"Integer "<<x<<endl;
               i=i+2;
           }
           else if(input[i]=="-f" && checkFloat(input[i+1]))
           {
               float x;
               char charr[1024];
               strcpy(charr, input[i+1].c_str());
           sscanf(charr, "%f", &x);
               cout<<"Float "<<x<<endl;
               i+=2;
           }
           else if(input[i]=="-s" && input[i+1]!="-i" && input[i+1]!="-f" && input[i+1]!="-s" && input[i+1]!="-h" )
           {
               cout<<"String "<<input[i+1]<<endl;
               i+=2;
           }
           else if(input[i]=="-h")
           {
               printAll();
               i+=1;
           }
           else
           {
               printAll();
               return 0;
           }
       }
      
   }
   else
       printAll();
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote