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
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.