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

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    33

    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. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    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.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    As Hobbit has addressed your question, I will just add some general notes on your code:

    • Avoid using gets. Use fgets instead.
    • Use meaning variable names. "str1" and "pdt1" do not do a good job of explaining their purpose. Perhaps something like "input1" and "product1" would be better.
    • Two-dimensional arrays would help reduce the need for three separate variables for each input/product.
    • Notice how you're doing the same thing three times, to each string. This would be a good opportunity to use a function. This way, your code is neater and more concise, easier to follow, and easier to fix if there's a problem (if a bug is found in that code, you only have to correct it in one place).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-25-2014, 10:36 PM
  2. how to transform integer to char..
    By transgalactic2 in forum C Programming
    Replies: 7
    Last Post: 01-09-2009, 08:34 AM
  3. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 04:05 PM
  4. transform int to char
    By WrathFox in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2004, 02:27 AM
  5. Filled array
    By GaPe in forum C Programming
    Replies: 2
    Last Post: 12-11-2001, 05:36 AM

Tags for this Thread