Like Tree4Likes
  • 2 Post By Salem
  • 2 Post By hk_mp5kpdw

Cannot figure out my problem can anyone help?

This is a discussion on Cannot figure out my problem can anyone help? within the C++ Programming forums, part of the General Programming Boards category; i need the program to read from a file and write to a file.. everything was working perfectly until i ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    Cannot figure out my problem can anyone help?

    i need the program to read from a file and write to a file.. everything was working perfectly until i tried to add a menu to it. Here's my code. Any help is appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    typedef struct car
    {
    int year;
    char number[9];
    char colour[10];
    float engine;
    int tax;
    } car_type;
    
    
    
    
    int main()
    {
    int i,j,x;
    
    void print_file ();
    void print_screen ();
    
    car_type cars[6];
    
    FILE* fp;
    FILE* fp2;
    
    fp = fopen("indata.txt","r");
    fp2 = fopen("car_tax.txt","w");
    
    i = 0;
    while(!feof(fp))
    
    {
    fscanf (fp, "%d", &cars[i].year);
    fscanf (fp, "%s", &cars[i].number);
    fscanf (fp, "%s", &cars[i].colour);
    fscanf (fp, "%f", &cars[i].engine);
    i++;
    }
    
    for (j=0;j<=i;j++)
    {
    if (cars[j].year <=5 && cars[j].engine <=1.6)
    cars[j].tax = 150;
    
    if (cars[j].year <=5 && cars[j].engine >1.6)
    cars[j].tax = 300;
    
    if (cars[j].year >5 && cars[j].engine < 1.4)
    cars[j].tax = 200;
    
    if (cars[j].year >5 && cars[j].engine >= 1.6)
    cars[j].tax = 600;
    
    if (cars[j].year > 5)
    {
    if (cars[j].engine >= 1.4 && cars[j].engine < 1.6)
    cars[j].tax = 400;
    }
    
    }
    
    
    printf ("Welcome to the program\n");
    printf ("\nPlease enter a choice");
    printf ("\n\tMENU\n\n");
    printf ("1.\tPrint on screen\n");
    printf ("2.\tPrint data to file\n");
    printf ("3.\tEnd program\n");
    
    switch (x)
    
    {
    
    case '1':
    print_file (j, cars[].year, cars[].number, cars[].colour, cars[].engine, cars[].tax);
    
    break;
    
    
    
    case '2':
    print_screen (j,cars[].tax);
    break;
    
    
    case '3':
    
    printf ("\nThank you for using the programme goodbye\n");
    break;
    
    
    default:
    printf ("Error! incorrect choice please enter another choice");
    break;
    }
    
    
    
    
    fflush (fp);
    fclose (fp);
    
    
    fflush (fp2);
    fclose (fp2);
    
    return (EXIT_SUCCESS);
    
    }
    
    
    void print_screen (int j, int cars[].tax)
    {
    int a;
    
    
    for (a=0;a<=j;a++)
    {
    printf ("\nTax on car %d = Euro %d\n", a, cars[j].tax);
    }
    
    }
    
    
    void print_file (int j, int cars[].year, ch cars[].number, ch cars[].colour, float cars[].engine, int cars[].tax)
    {
    int b;
    
    for (b=0;b<j;b++)
    {
    fprintf(fp2,"%d\t",cars[b].year);
    fprintf(fp2,"%s\t",cars[b].number);
    fprintf(fp2,"%s\t",cars[b].colour);
    fprintf(fp2,"%f\t",cars[b].engine);
    fprintf(fp2,"%d\n",cars[b].tax);
    }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Perhaps if you applied the knowledge in this FAQ
    SourceForge.net: Indentation - cpwiki
    to your post, then

    a) people would want to read your code
    b) it might become apparent to yourself just what it is you're doing wrong.

    Crappy indentation kills more programs than pretty much anything else.
    jimblumberg and Elysia like this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    You need to find an indentation style and use it. As your code is now it is almost impossible to read.

    What are your error messages?

    Jim

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    #1 Your prototypes of the print functions do not match the later definitions you provide.
    #2 You are using feof to control a loop.
    #3 Your fscanf calls are wrong for the number and colour fields.
    #4 You don't get user input into variable 'x' after displaying the menu resulting in the use of an uninitialized variable in the switch construct.
    #5 Your variable 'x' is an int but the switch seems to be comparing against char values.
    #6 You are passing your car data into the print functions incorrectly.
    #7 You assume that the output file is open without actually verifying that it is plus you could probably move the open/close for that inside the file printing function closer to where it is needed.
    #8 You are flushing a file (fp) that is open for reading.
    #9 Your menu states option 1 for printing to screen and option 2 to print to file but the code seems to want option 1 to print to file and option 2 to print to screen.
    #10 You appear to be missing a loop of some kind around your menu/switch code.
    #11 This is the C++ board, what you've posted should probably be under the C board.
    Last edited by hk_mp5kpdw; 04-20-2011 at 02:55 PM.
    Salem and Elysia like this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. figure - problem
    By jamesfisher in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2009, 05:28 PM
  2. I cant figure the problem out!
    By jturner38 in forum C Programming
    Replies: 5
    Last Post: 03-25-2009, 02:13 AM
  3. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 07:45 PM
  4. I simple little problem but i cant figure it out
    By SebastionV3 in forum C++ Programming
    Replies: 9
    Last Post: 05-24-2006, 06:55 PM
  5. Can'nt figure out problem
    By HAssan in forum C Programming
    Replies: 7
    Last Post: 12-26-2005, 08:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21