Construct a program in C that takes a variable number as a string from the comma
ID: 3761841 • Letter: C
Question
Construct a program in C that takes a variable number as a string from the command line. Program must implement these functions. (Clue: Use a loop with argc in the terminating condition to take in a variable number of values)
1. int getDataType(char*num)
will take string as input and return 1 if number is an int and 2 if number is a double (check "." in a string)
2.void printValuePlusTen(char*num, int dataType)
conver string num to an integer/ floating point number with atoi or atof and then add ten to the number and print it out
Function should loop through the input values in argv and it will check the data type and print out the value plus ten using the functions above for each value.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
int getDataType(char *num)
{
int c=0,i;
for(i=0;i<strlen(num);i++)
{
if(num[i]=='.')
{
c=1;
}
}
return (c);
}
void printValuePlus(char *num, int dataType)
{
int d;
float e;
if(dataType==0)
{
d=atoi(num)+10;
printf("%d",d);
}
else
{
e=atof(num)+10;
printf("%f",e);
}
getch();
}
void main(int argv, char *argc[])
{
int i,a;
a=getDataType(argc[1]);
printValuePlus(argc, a);
}