# Thread: how to transform a char array filled with digits to a simple int variable!!

1. ## how to transform a char array filled with digits to a simple int variable!!

Good day C Gurus,
I want to write a program that acquires 2 strings, each containing the name of a product and its price separated by a space. Once this information is stored into appropriate variables, the program receives an additional string containing the name of a product, and an integer value corresponding to a quantity.The program must:
a. Determine whether the latest product introduced correspond to one ofthe products previously stored
b. In the negative case, request to insert another product name andquantity
c. In the positive case, calculate and print the total price by multiplyingthe quantity by the price of the product entered by the user.
I've already written the following code:
Code:
```int main()
{
char str1[80];
char str2[80];
char str3[80];
char pdt1[80];
char pdt2[80];
char pdt3[80];
char prix1[3];
char prix2[3];
char quantity3[3];

int i,j,k,l,m,n;

gets(str1);
gets(str2);
gets(str3);

for(i=0;str1[i]!=' ';++i) // storing  the name of the product1 in another char array//
{
pdt1[i]=str1[i];
}
pdt1[i]='\0';

for(i=0;str1[i]!='\0';++i) // storing  the price of the product1 in another char array//
{
k=0;
if(isdigit(str1[i]))
{
prix1[k]=str1[i];
k++;
}

}

prix1[k]='\0';

for(j=0;str2[j]!=' ';++j)// storing the name of the product2 in a different string
{
pdt2[j]=str2[j];
}
pdt2[j]='\0';

for(j=0;str2[j]!='\0';++j)// storing the price of the product2 in a different string
{
l=0;
if(isdigit(str2[j]))
{
prix2[l]=str2[j];
l++;
}
}
prix2[l]='\0';
for(m=0;str3[m]!=' ';++m) // storing the name of the product3 in a different string
{
pdt3[m]=str3[m];
}
pdt3[m]='\0';

for(m=0;str3[m]!='\0';++m)// storing the quantity of the product3 in a different string
{
n=0;
if(isdigit(str3[m]))
{
quantity3[n]=str3[m];
n++;
}
}
quantity3[n]='\0';
if(strcmp(pdt1,pdt3)!=0 && strcmp(pdt1,pdt3)!=0)
{
printf ("Enter another product and its quantity!!");
gets(str3);
}

else if (strcmp(pdt1,pdt3)==0)
printf("you have to pay for %d quantity of %s the price %d ",quantity3,pdt1,quantity3*prix1);
else if (strcmp(pdt1,pdt2)==0)
printf("you have to pay for %d quantity of %s the price %d ",quantity3,pdt2,quantity3*prix2);
}```
and as you can see I can't compute the total price because my quantity and prices are strings!!!

Any hint will be highly appreciated!!

Best!!!

2. Generally if you have a string and you want to split it into a string and a number you would use sscanf

This is a similar function to scanf except it reads from a string rather than from keyboard input.

BTW your code posts horribly. Check your compiler settings for a 'replace tabs with spaces' option.