1. ## array Calculations

I have a 2d array with chars and ints, what i want to do is to use the char to search and sum the ints the file looks something like this
array example:
Flour 1000 January
Eggs 500 January
Butter 500 January
Sugar 600 January
Flour 1200 February
Eggs 500 February
Butter 500 February
Sugar 400 February
Flour 2000 March
Eggs 1000 March

for example i want to use flour and for every occurrence of flour add all and then display the total using the strgcmp function

2. Originally Posted by HolyMan
I have a 2d array with chars and ints
In C, you can have an array with chars or ints, not both.

what i want to do is to use the char to search and sum the ints
[...]
for example i want to use flour and for every occurrence of flour add all and then display the total using the strgcmp function
There's a lot of ways you could do this, depending on your skill level, how efficient the search has to be, how much work you want to put in, and so on, but I think all of the (sane) ways will involve a struct:

Code:
```struct eg {
char name[64];
int quantity;
};```
You may or may not want a "month" field in there, depending on how you organize this.

3. this is what i have coded
Code:
```#include <stdio.h>
int main()
{
char ingred[50][50]= {" "};
int amount[50]= {0};
char month[50][50]= {" " };
int i = 0;
FILE *ptr;
ptr = fopen ("OrderList.txt","r");
printf ("\t\t\t\tINGREDIENTS\t\tAMOUNT \t\tMONTH");
while(!feof(ptr))
{

fscanf (ptr,"%s%d%s",ingred[i], &amount[i], month[i]);
printf ("\t\t\t\t%s\t\t%d\t\t%s\n", ingred[i], amount[i], month[i]);

i++;
}```
what i want to do is is search the file for Eg: "FLOUR" and then calculate the running total for every occurrence of flour

4. Originally Posted by HolyMan
what i want to do is is search the file for Eg: "FLOUR" and then calculate the running total for every occurrence of flour
There is a difference between scanning a file for this, and scanning an array (or set of arrays) into which data from a file has been placed -- the later is what your first post implied, and it seems to be the direction you are going. But this implies a further purpose for the data -- eg, a user interface where search criteria can be provided during execution (otherwise, why keep all the data?).

So you might want to be more clear about exactly what it is this program has to accomplish.

Going with what you have, however, you can use the parallel arrays to get a total for each individual foodstuff. Just iterate thru "ingred", and for each category keep a += count from the parallel int array. If strcmp() on ingred[i] matches "flour", then amount[i] is what you want to add to the count.

5. yes that is exactly what i want to do getting the total for each food stuff. canyou give me an example how I would code what you said

6. supppose n is the number of lines, and you have read in the data till now:
Code:
```    int n;    // suppose number of lines
int cnt = 0;
for (i = 0; i < n; ++i) {
if (strcmp(ingred[i], "Flour") == 0) {
cnt += amount[i];
}
}
printf("total amount of flour: %d\n", cnt);```
cnt gives total amount for flour. Try adding more else if statements if you want to get the total amount for each food stuff. Tip: you can use an array for cnt then, with the number of different foods you have.

7. Originally Posted by naturegirl
supppose n is the number of lines, and you have read in the data till now:
Code:
```    int n;    // suppose number of lines
int cnt = 0;
for (i = 0; i < n; ++i) {
if (strcmp(ingred[i], "Flour") == 0) {
cnt += amount[i];
}
}
printf("total amount of flour: %d\n", cnt);```
cnt gives total amount for flour. Try adding more else if statements if you want to get the total amount for each food stuff. Tip: you can use an array for cnt then, with the number of different foods you have.
Please explain why strcmp is opening with ingred[i] and not the filename?

If I remember correctly the format for strcmp is
Code:
`strcmp(filename, "string")`
... or some sort of pointer...

8. Originally Posted by Spamtacus
Please explain why strcmp is opening with ingred[i] and not the filename?

If I remember correctly the format for strcmp is
Code:
`strcmp(filename, "string")`
... or some sort of pointer...
Errr, no. strcmp - C++ Reference

9. Originally Posted by Spamtacus
Please explain why strcmp is opening with ingred[i] and not the filename?

If I remember correctly the format for strcmp is
Code:
`strcmp(filename, "string")`
... or some sort of pointer...

Because Naturegirl said the data had already been read in from the file. That would normally use a 2D array of char, with each ingredient on a separate line:

ingred[0] = "flour"
ingred[1] = "sugar"
ingred[2] = "salt"
etc.

so ingred[i] IS the pointer to the string of "flour" or "sugar", or "salt", etc.

strcmp(), will have no problem with ingred[i].

And that is OK for strcmp(), I'm not sure what Tclausex means.

I'm not sure what Tclausex means.
I guess I mean that strcmp() doesn't operate on files ...

11. The entire solution for this problem would be great, im trying to do the same thing with my code..

12. Originally Posted by Kristyy_mariee
The entire solution for this problem would be great, im trying to do the same thing with my code..
We're not here to give away free code...

You need to get your thinking cap on and figure this stuff out for yourself.

13. ok
will try this and see how it works out