Thread: Trouble finding error in code

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    7

    Trouble finding error in code

    While writing my code, I checked to see if it would run and got a pop-up window saying that my project (Final Project.exe) has stopped working. This tells me that there is an error in it, but I wasn't sure where. Here is my code:
    Code.c

    And the file I'm reading from:
    Car.txt

    After adding printf("Hello\n"); a couple of times in my code, I've narrowed down to that my while loop has an error in it because it's printing the first 4 Hello's but not the last two. I'm still not sure what the error is though after looking through several sites. Help please

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    When you want to print/scan strings you should use %s, not %c .

    Next time wrap your code in code tags and post it here.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    ok, I've made that fix but it keeps saying that it has stopped working.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FILENAME "Car.txt"
    struct CarData
    {
           char Driver[6];
           char Type[12];
           int CarNumber;
           char Colour[7];
    };
    int main(void)
    {
      printf("Hello\n"); //Testing to see if it prints
      
      FILE *Car = fopen(FILENAME, "r");
      int i;
      
      printf("Hello\n"); //Testing to see if it prints
      struct CarData Veh[5];
     
      printf("Hello\n"); //Testing to see if it prints
      
      //Determining if file contains info
      if(Car == NULL)
      {
         printf("File does not exist.\n");
         system("PAUSE");
         exit (1);
      }
      printf("Hello\n"); //Testing to see if it prints
      
      while (!feof(Car))
      {
            fscanf(Car, "%s %s %d %s", Veh[i].Driver, Veh[i].Type, Veh[i].CarNumber, Veh[i].Colour);
            i++;
      }
      
      printf("Hello\n"); //Testing to see if it prints
      
      printf("%s %s %d %s",Veh[i].Driver, Veh[i].Type,
            Veh[i].CarNumber, Veh[i].Colour);
            
      printf("Hello\n"); //Testing to see if it prints
      
      system("PAUSE"); 
      return 0;
    }

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Initialize i to zero.

    Also welcome to the forum
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    lol thanks. unfortunately it still isn't working even after initializing i. :/

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Fix your scanf formats
    Code:
    $ gcc -std=c99 -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:34:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 5 has type ‘int’ [-Wformat]
    > while (!feof(Car))
    This does not do what you expect.
    SourceForge.net: Feof - cpwiki

    Use this
    Code:
    while ( i < 5 &&
            fscanf(Car, "%s %s %d %s", Veh[i].Driver, Veh[i].Type, &Veh[i].CarNumber, Veh[i].Colour) == 4 ) {
        i++;
    }
    1. It checks that there is room in the array to store the data
    2. It checks that the data was read successfully.
    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.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    Ok, it runs and prints now but all I get is gibberish:

    Ðþ( -2 Ú˜ruú suȤ
    Ðþ( -2 Ú˜ruú suȤ
    Ðþ( -2 Ú˜ruú suȤ


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FILENAME "Car.txt"
    struct CarData
    {
           char Driver[6];
           char Type[12];
           int CarNumber;
           char Colour[7];
    };
    int main(void)
    {
      FILE *Car = fopen(FILENAME, "r");
      int i=0;
      int j=0;
      struct CarData Veh[5];
      
      //Determining if file contains info
      if(Car == NULL)
      {
         printf("File does not exist.\n");
         system("PAUSE");
         exit (1);
      }
      
      while ( i < 5 &&      
       fscanf(Car, "%s %s %d %s", Veh[i].Driver, Veh[i].Type, &Veh[i].CarNumber, Veh[i].Colour) == 4 ) 
       {
        i++;
       } 
      
      while (j < 5)
      {
      printf("%s %s %d %s\n",Veh[i].Driver, Veh[i].Type,
            Veh[i].CarNumber, Veh[i].Colour);
      j++;
     }
      system("PAUSE"); 
      return 0;
    }

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You modify j, but you still requesting for the ith index
    Code:
    while (j < 5)
      {
      printf("%s %s %d %s\n",Veh[i].Driver, Veh[i].Type,
            Veh[i].CarNumber, Veh[i].Colour);
      j++;
     }
    The indexes should be with j not i.

    But do you really need two variables? I would keep only the i and set it to zero again before printing.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while (j < 5)
    Actually, this should be
    j < i
    so you only print however many cars were read.
    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.

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    Ouch, fail on my part. Thank you guys so much, it works now.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FILENAME "Car.txt"
    struct CarData
    {
           char Driver[6];
           char Type[12];
           int CarNumber;
           char Colour[7];
    };
    int main(void)
    {
      FILE *Car = fopen(FILENAME, "r");
      //FILE *Output = fopen("Output.txt", "w");
      int i=0;
      struct CarData Veh[5];
      
      //Determining if file contains info
      if(Car == NULL)
      {
         printf("File does not exist.\n");
         system("PAUSE");
         exit (1);
      }
      
      while ( i < 3 &&      
       fscanf(Car, "%s %s %d %s", Veh[i].Driver, Veh[i].Type, &Veh[i].CarNumber, Veh[i].Colour) == 4 ) 
       {
        i++;
       } 
      
      for(i=0; i<3; i++)
      {
      printf("%s %s %d %s\n",Veh[i].Driver, Veh[i].Type,
            Veh[i].CarNumber, Veh[i].Colour);
     }
      system("PAUSE"); 
      return 0;
    }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Since the "3" part of your code can only serve to wreck it, I'd delete that part.

    Make your second for loop work with j=0;j<i;j++, instead of i.

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    Quote Originally Posted by Adak View Post
    Since the "3" part of your code can only serve to wreck it, I'd delete that part.

    Make your second for loop work with j=0;j<i;j++, instead of i.
    huh?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > huh?
    Add 2 more lines to Cars.txt and then re-test your program.

    Then add a few more lines, and re-test your program.
    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.

  14. #14
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    Ooooh, gotcha.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with peak finding in canny algorithm
    By DaNxTh3xMaNx in forum C Programming
    Replies: 3
    Last Post: 01-29-2012, 10:42 PM
  2. need help in finding the error in my code
    By zeybek258388 in forum C Programming
    Replies: 5
    Last Post: 12-23-2011, 04:38 PM
  3. Trouble finding correct value
    By cool_guy in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2010, 10:36 PM
  4. need help finding where the problem is for this code
    By edmorgan in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2007, 12:41 PM
  5. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM