First of all, let me just say that, this is the 1st time I have ever programmed in C, so please be gentle :S (although it shouldn't be that bad).

With regards to the program, I have been set a scenario to create a sales system where the user enters the numbers of "week", "units", and "price" (in pennies) as well as the product "name" (an array of chars) that was sold. The sales should be recorded for a period of 13 weeks......

so basically, to summarize, the user enters a list of sales which have been made over a period of 13 weeks. Once the sales have been entered for 13 weeks (0 - 12), the program should save the information to an external 'C' file (e.g. data1.c) and then read back the values from the same file, displaying only the week numbers and the total amount of sales (as a double) each week.

so, for example. if there was 32 packs of biscuits sold on week 11 at 20p each and there were 12 bars of chocolate sold on week 12 at 10p each, then once "13" was entered into the "week" variable, the program would stop, save all the sales info to the file, then read it back and display it in two columns ("week" and "total sales").

.....showing that, in this case, from the example above, in week 11 the total sales were 6.40 (20 * 32), and in week 12 the total sales were 1.20 (10 * 12). I hope that gives you a better idea of what I am trying to achieve.

so, here is my code so far. it allows me to enter sales, and prints out the sales that have been entered once the 13th week has been entered (I added that part while I was debugging to see if it was working correctly, but it is not required), but for some reason it wont stop as soon as "13" has been entered - which it should.

#include <stdio.h>
#include <string.h>

int main()
{
  int week = 0;
  int units = 0;
  int price = 0;
  char name[30];
  int count = 13;
  int i = 0;
  int total = 0;

  struct sale
  {
    int week;
    int units;
    int price;
    char name[30];
  }weekly_Sale;

     do
     {
       scanf("%i %i %i %s", &weekly_Sale.week, &weekly_Sale.units, &weekly_Sale.price, weekly_Sale.name);
       // i++;
     }

    while (weekly_Sale.week < 13);

    for (i = 0; i < count; i++) //the unneeded part, I used for debugging
    {
      printf("%i %i %i %s ", weekly_Sale.week, weekly_Sale.units, weekly_Sale.price, weekly_Sale.name);
    }
  return 0;
}

....I know this is a stupid problem to have but I have looked in a book, and tried searching the internet but every time I try, I don't get anywhere. Also, I only have until 2 days time (Friday, around midday), and I have been driven mad :confused: so far by the development environment I have to use along with the amount of errors and things I've been getting, but I need to get this one and another one done :'( and I've been working on it for ages now, so please feel free to contribute, as soon as possible. thanks to everyone in advance.

>>e.g. data1.c

The file extension is somewhat important -- *.c files are normally c programs, not data files. Your data file should be names "data.txt".

You need to store the data in an array of struct sale objects. What you are doing is just overwriting all the data on each loop iteration. So just declare an array and use it.

And a for loop would be more appropriate here than a while loop.

struct sale Weekly_Sale[13];
   for(i = 0; i < 13; i++)
   {
       scanf("%i %i %i %s", &weekly_Sale[i].week, &weekly_Sale[i].units, &weekly_Sale[i].price, weekly_Sale[i].name);
   }

Now it is a simple matter to store all that information in a file

FILE* fp = fopen("data.txt","wb");
if(fp !=  NULL)
{
     fwrite( Weekly_Sale, 13, sizeof(struct sales));
     fclose();
}

>>e.g. data1.c

The file extension is somewhat important -- *.c files are normally c programs, not data files. Your data file should be names "data.txt".

You need to store the data in an array of struct sale objects. What you are doing is just overwriting all the data on each loop iteration. So just declare an array and use it.

And a for loop would be more appropriate here than a while loop.

[B]struct sale Weekly_Sale[13];[/B]
   for(i = 0; i < 13; i++)
   {
       scanf("%i %i %i %s", &weekly_Sale[i].week, &weekly_Sale[i].units, &weekly_Sale[i].price, weekly_Sale[i].name);
   }

Now it is a simple matter to store all that information in a file

FILE* fp = fopen("data.txt","wb");
if(fp !=  NULL)
{
     fwrite( Weekly_Sale, 13, sizeof(struct sales));
     fclose();
}

hold on, I just realised the first snippet (which is bold) from the last reply you gave. Does this line turn the struct into an array with 13 elements in it (0-12), and then use it with the for loop to keep storing the sales in each array element, without overwriting the previous sales?

then, in the next snippet (about storing in a file), what does the "wb" mode do? - does it not need to be "r+" mode for reading and writing to the file?
.....and, would i be right if i said that the fwrite function copies each of the structs stored values (sales) to the specified file (data.txt)?

thanks for the help. and thanks for numbering the lines in the original code. its very much appreciated!

>>e.g. data1.c

The file extension is somewhat important -- *.c files are normally c programs, not data files. Your data file should be names "data.txt".

You need to store the data in an array of struct sale objects. What you are doing is just overwriting all the data on each loop iteration. So just declare an array and use it.

And a for loop would be more appropriate here than a while loop.

struct sale Weekly_Sale[13];
   for(i = 0; i < 13; i++)
   {
       scanf("%i %i %i %s", &weekly_Sale[i].week, &weekly_Sale[i].units, &weekly_Sale[i].price, weekly_Sale[i].name);
   }

Now it is a simple matter to store all that information in a file

FILE* fp = fopen("data.txt","wb");
if(fp !=  NULL)
{
     fwrite( Weekly_Sale, 13, sizeof(struct sales));
     fclose();
}

can you please just explain why you added in those positions within the for loop, because when i replaced my original code with them, i got 4 errors, all the same.

....the errors said "request for member week in something not a structure or union", and only the bold bit where it says "week" would change to each variable name (i.e. units, price and name) while the rest of the error message would stay the same.

.... and if i was going to add to each of the things in the for loop, would i not need to add a pointer to each variable in the struct? for example, consider the following:

[B]
struct sale
{
int *week;
int *units;
int *price;
char *name[30];
}weekly_Sale [13];[/B]

   for(i = 0; i < 13; i++)
   {
       scanf("%i %i %i %s", &weekly_Sale[i].week, &weekly_Sale[i].units, &weekly_Sale[i].price, weekly_Sale[i].name);
   }

.....would that snippet of code work?
Thanks again!

can you please just explain why you added in those positions within the for loop, because when i replaced my original code with them, i got 4 errors, all the same.

struct sale Weekly_Sale[13]; What you're doing here is creating a user-defined variable called Weekly_Sale of type 'struct sale'. It's logically similar to creating arrays of defined types and you use them the same way you would use any other array. That's the reason why you use the subscript . If you're still confused about it, better read up about arrays of structures.

....the errors said "request for member week in something not a structure or union", and only the bold bit where it says "week" would change to each variable name (i.e. units, price and name) while the rest of the error message would stay the same.

If you copied the code exactly the way it's given by Ancient Dragon, you're bound to get an error since Weekly_Sale isn't the same as weekly_sale [C is case sensitive].

.....would that snippet of code work?

Nope. It will if you don't declare the variables as pointers.

struct sale
{
int *week;
int *units;
int *price;
char *name[30];
}weekly_Sale [13];

   for(i = 0; i < 13; i++)
   {
       scanf("%i %i %i %s", &weekly_Sale[i].week, &weekly_Sale[i].units, &weekly_Sale[i].price, weekly_Sale[i].name);
   }

>>.....would that snippet of code work?
No -- why did you change the structure to use pointers? Put the structure back the way you had it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.