title: How to add up repetitive values from input in C?
body: <p>i'm kinda new to C.
  i am trying to make a shopping cart application program that reads data from a txt file that contains a product and a price, seperated by whitespace.
  Then in a new line another product is put and a price(also seperated by whitespace).
  then outputs the bill in another &quot;bill.txt&quot; text file.
  example:</p>
  <p>input taken from file (products.txt):</p>
  <pre><code>Bread 2.78
  cheese 4.59
  vegetables 1.99
  bread 1.99
  Milk 0.56
  cheese 2.79
  </code></pre>
  <p>output written in another file(bill.txt):</p>
  <pre><code>bread : 4.77 , 2 products, average-price: 2.38
  cheese: 7.38 , 2 products, average-price: 3.69
  milk: 0.56 , 1 products, average-price: 0.56
  vegetables: 1.99 , 1 products, average-price: 1.99
  total : 14.70
  </code></pre>
  <p>my main problem is that i am confused about how to add up the sum of the repetitive products like bread from the text file and add them as a quantity instead! The bread is written twice in input but a qty of 2 is written instead.</p>
  <p>usually in a language like java i would have used an ArrayList and solved it easily, but since C doesn't have Arraylists, what to do?</p>
  <p>here is my code:</p>
  <pre><code> #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int count;
  struct Product
  {
      char productName[30];
      double price;
  };
  
   struct Product p1;
  
  int main()
  {
      double total = 0;
  
      //input file
       FILE *fp;
      fp = fopen(&quot;products.txt&quot;,&quot;r&quot;);
      //output file
      FILE *fp1 = fopen(&quot;bill.txt&quot;,&quot;w&quot;);
      if (fp == NULL || fp1 ==NULL)
      {
          perror(&quot;files didn't open&quot;);
      }
      printf(&quot;this input was written in file:\n&quot;);
      while(1)
      {
          fscanf(fp,&quot;%s %d&quot;,&amp;p1.productName,&amp;p1.price);
          total = total + p1.price;
          fprintf(fp1,&quot;%s:\t%d,  \n&quot;,p1.productName,p1.price);
          printf(&quot;%s:\t%d,  \n&quot;,p1.productName,p1.price);
          if (feof(fp))
          {
              fprintf(fp1,&quot;Total: %d&quot;,total);
              break;
          }
  }
  printf(&quot;total: %d&quot;, total);
  return 0;
  }
  </code></pre>
  <p>Any ideas?</p>