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

Write a program in C that takes 3 values as strings from the command line. Your

ID: 3567624 • Letter: W

Question

Write a program in C that takes 3 values as strings from the command line. Your program must implement the following functions.
a) int getDataType(char *num)
b) void printValue(char *num, int dataType)
getDataType will take a string as input and return 1 if the number is an int and 2 if the number is a double. (hint: check for a

Write a program in C that takes 3 values as strings from the command line. Your program must implement the following functions. a) int getDataType(char *num) b) void printValue(char *num, int dataType) getDataType will take a string as input and return 1 if the number is an int and 2 if the number is a double. (hint: check for a ???.??? in the string) printValue takes the pointer to character array and data type as input and uses either the atoi or atof function to print out the value. Your main function should loop through the input values in argv where on each value it will check the data type and print out the value using the two functions above. Sample output screenshot:

Explanation / Answer

#include <iostream>
#include <stdlib.h>
using namespace std;

int getDataType(char *num)
{
while(*num != '')
{
if(*num == '.')
return 2;
  
*num++;
}
return 1;
}

void printValue(char *num, int dataType)
{
if(dataType == 2)
{
cout<<atof(num);
}
  
else
{
cout<<atoi(num);
}
}

int main(int argc, char *argv[] )
{
if(argc > 1)
{
for(int i=1; i<argc; i++)
{
cout<<"argc["<<i<<"] = ";
printValue(argv[i], getDataType(argv[i]));
cout<<" ";
}



}

return 0;
}

--------------------------

output

$demo 7 12.8 102