Thread: Help

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    24

    Help

    I need to create a program that reads a whole bunch of info from one file and then writes it into another in a formatted columns such as name store no. amount sold etc
    ----- ----- ---------- ----


    b 2
    c 1

    etc here is the code I have so far and I am stuck because the info in bagel.seq has no name so do I need to create a struct for the bagel.seq and if so how do I do that. I hope someone understands this I need to get done by tomorrow. I have been working and researching but I am stuck.
    Code:
    // filebagel.cpp : main project file.
    
    #include "stdafx.h"
    #include <stdio.h>
    
    int main()
    {
    typedef struct
    {
     int store no.
     char bagel description
     double bagel price 
     int no. sold 
    }bagelreport;
    
    bagelreport report;
    FILE = *ptr_report;
    FILE= *ptr_bagel;
    if((ptr_bagel= fopen("Bagel.seq","r"))==NULL)
    {
        printf("\a\afile could not be opened\n");
    }
     if((ptr_report=fopen("report.rpt","w"))==NULL)
     {
         printf("\a\aFile could not be opened\n");
     }
     while(!feof(ptr_bagel && ptr_report){
     fprintf(ptr_report,"%d%s%d%d",&report.srore no,report. bagel description, report.bagel price, report. no sold);
     fscanf(ptr_bagel,"%d%s%d%d");

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Show us a few lines from your source file.
    Show us how you need it formatted.

    You'll get much better help.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
     char bagel description
    Spaces are NOT allowed in C variable names.

    Tim S.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    24

    Help

    The only way I know how to do that is to send it in a text file or m or here is a jpeg. that jpeg is how the format in a file called bagel.rpt should look and then bagel.seq look like this
    [code]
    1 Cinnamon 1.000000 300




    1 Plain 1.250000 200
    1 Garlic 1.200000 100
    2 Plain 1.100000 100
    2 Apple 1.300000 50
    2 Cheddar 1.500000 75
    3 Pumpkin 1.350000 150
    3 Apple 0.750000 100
    3 Cinnamon 1.350000 150
    3 Plain 1.000000 100
    [\code]
    and here are the instructions
    he “Bogus Bagels” stores require that a weekly report be written for all three of their stores. The input file – “Bagel.seq” contains records with the fields: Store No.(integer), Bagel Description (string), Bagel Price (double), No. Sold (integer). Bagel.seq contents are listed below:

    Write a program that will read the “Bagel.seq” file and print out a report listing the sales totals for each store and the percentage of the total district’s sales that the store contributed. You may write these as two separate programs or as one program with separate functions.

    Process:
    1.Open the file “Bagel.seq”.
    2.Create report shown below. Name it “Bagel.rpt.” Make sure that a Total Sales and the percentage each store contributed to the Total Sales is calculated and reported as shown below.
    Hints:
    ·fgets and/or fscanf may be useful in reading the file, and fput and/or fprintf may be useful for printing the formatted report.
    ·An array for keeping track of Store Totals may, also, be useful.

    the jpeg show bagel.rpt then I need to figure out how to do the math part of it to
    Attached Images Attached Images Help-bagel1-jpg 

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Well I am not sure how to make it look like the report then do I need to put in \t's or something to get spaces as you can see from the picture I need to create spaces or should I use \n but I didn't think I could use those in variables

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    So many errors already. Here's a few that I've spotted:

    Code:
    if((ptr_bagel= fopen("Bagel.seq","r"))==NULL)
    
    {
        printf("\a\afile could not be opened\n");
    }
     if((ptr_report=fopen("report.rpt","w"))==NULL)
     {
         printf("\a\aFile could not be opened\n");
     }
    The program should probably exit if it fails to open the files.

    Code:
    while(!feof(ptr_bagel && ptr_report)
    Other than the fact that the syntax is wrong, you only need to run the loop until ptr_bagel reaches the end of the file. I could be wrong, but I don't think ptr_report can ever reach the end of the file because you're constantly writing to it.

    Code:
    fprintf(ptr_report,"%d%s%d%d",&report.srore no,report. bagel description, report.bagel price, report. no sold)
    You're printing a whole bunch of data that hasn't had any values set to it. This will end up printing garbage data to your file.

    Also, %s is for strings. In your struct, "bagel description" is a char. If you want "bagel description" to be a string, then you need to make it a char * or if you don't know about pointers, a char array.

    Code:
     char bagel description
     double bagel price 
     int no. sold
    Variables can't have spaces in their names. So for instance, you could change "bagel description" to just "description" or "bagel_description" or "bagelDescription".

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    I understand that you can't have spaces....but My teacher wants the report to look like that jpeg so How am I suppose to do that

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Thanks for the point on the while loop

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Here's a reference guide for the printf function. This should help you.

    printf - C++ Reference

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    it keep saying my functions have invalid arguements then when I fix one which was an extra } it just says another one has an invalid arguements

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    You should probably create a second topic for this other program.

    Look at this piece of code by itself.

    Code:
    char is_valid(char valid_y_n) {              
          valid_y_n = tolower(valid_y_n);              
          while(valid_y_n != 'y'&& valid_y_n != 'n') {                  
               printf("You can only enter 'y' or 'n'");                  
               scanf("%c",&valid_y_n);                   
               valid_y_n = tolower(valid_y_n);              
          }              
    return (valid_y_n);
    Do you not see the error?
    Last edited by failure67; 12-14-2011 at 05:21 PM.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    I am just overwhelmed so any help would be appreciated and as I become more fatigued and more caffinated I am making dumber and dumber errors I just wrote that program an hour ago and now I have to write this one

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    alright disregard the 2nd code created a new thread

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    No finishing brackets on the function am I correct

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    No anyone have any suggestions on the first code I posted about the bagel. rpt and bagel.seq file problem

Popular pages Recent additions subscribe to a feed