Thread: C C++ tut error

  1. #1
    C++ newb in training
    Join Date
    Mar 2005
    Location
    Burlington Wa
    Posts
    14

    C C++ tut error

    Hey everyone I did a tutorial on a website that seems to combines C and C++ for the program witch is an inventory program.

    Here it is. It runs and compiles fine but after I enter the data at the end it crashes on me. I am still new to C\C++ so any help is verry apreciated.

    Code:
     // Program to perform file processing using the FILE structure
    // Author: Jules Larson. Revised by Jeremy Jones 2005
    #include <iostream>
    using namespace std;
    int main()
    {
    	char Make[20], Model[20];
    	unsigned int CarYear;
    	long Mileage;
    	
    	FILE *CarInventory = fopen("cars.inv", "w");
    	
    	cout << "Enter the following pieces of information\n";
    	cout << "Make:	 "; gets(Make);
    	cout << "Model:	"; gets(Model);
    	cout << "Year:	"; cin >> CarYear;
    	cout << "Mileage:  "; cin >> Mileage;
    	
    	fprintf(CarInventory, "%s\n", Make);
    	fprintf(CarInventory, "%s\n", Model);
    	fprintf(CarInventory, "%s\n", CarYear);
    	fprintf(CarInventory, "%s\n", Mileage);
    
    	FILE *Carinventory = fopen("cars.inv", "r+");
    	
    		 fscanf(CarInventory, "%s\n", Make);
    		 fscanf(CarInventory, "%s\n", Model);
    		 fscanf(CarInventory, "%d\n", &CarYear);
    		 fscanf(CarInventory, "%d\n", &Mileage);
    		 
    		 cout << "Information about the car";
    		 cout << "\nMake:   " << Make;
    		 cout << "\nModel:  " << Model;
    		 cout << "\nYear:   " << CarYear;
    		 cout << "\nMIleage:  " << Mileage;
    							   
    		 fclose(CarInventory);
    	
    		 cout << endl;
    		 return 0;
    }
    Thanks
    Find the loop holes, then fix the errors, after you have found all the useful info

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    this seems to be C, should've posted it in the C section.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    ^^ It combines C and C++ as the OP stated, so I guess it's fine here.

    You have two FILE* variables, one called CarInventory and one called Carinventory. Case matters, and so the file pointer that is open for reading is not used, the one open for writing is instead. You should close the first file when you are done with it, then make a separate variable name for reading or just fix the typo. If you fix the typo you will get a compile error that is easy to fix - just remove the FILE* part in front of the second declaration.

  4. #4
    C++ newb in training
    Join Date
    Mar 2005
    Location
    Burlington Wa
    Posts
    14
    Odd...I fixed the spelling and removed the second FILE * and it still crashes...I am using BloodShed Dev-C++. THank you though for all help
    Find the loop holes, then fix the errors, after you have found all the useful info

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Hey everyone I did a tutorial on a website that seems to combines C and C++
    Is it on this site?
    Because it's lousy C and lousier C++.
    gets() is NEVER a good sign in any language.


    > I fixed the spelling and removed the second FILE * and it still crashes
    Well post the latest.
    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.

  6. #6
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Quote Originally Posted by Salem
    > gets() is NEVER a good sign in any language.
    Why?

  7. #7
    C++ newb in training
    Join Date
    Mar 2005
    Location
    Burlington Wa
    Posts
    14
    Here is the revise.

    Code:
     // Program to perform file processing using the FILE structure
    // Author: Jules Larson. Revised by Jeremy Jones 2005
    #include <iostream>
    using namespace std;
    int main()
    {
    	char Make[20], Model[20];
    	unsigned int CarYear;
    	long Mileage;
    	
    	FILE *CarInventory = fopen("cars.inv", "w");
    	
    	cout << "Enter the following pieces of information\n";
    	cout << "Make:	 "; gets(Make);
    	cout << "Model:	"; gets(Model);
    	cout << "Year:	"; cin >> CarYear;
    	cout << "Mileage:  "; cin >> Mileage;
    	
    	fprintf(CarInventory, "%s\n", Make);
    	fprintf(CarInventory, "%s\n", Model);
    	fprintf(CarInventory, "%s\n", CarYear);
    	fprintf(CarInventory, "%s\n", Mileage);
    
    		  CarInventory = fopen("cars.inv", "r+");
    	
    		 fscanf(CarInventory, "%s\n", Make);
    		 fscanf(CarInventory, "%s\n", Model);
    		 fscanf(CarInventory, "%d\n", &CarYear);
    		 fscanf(CarInventory, "%d\n", &Mileage);
    		 
    		 cout << "Information about the car";
    		 cout << "\nMake:   " << Make;
    		 cout << "\nModel:  " << Model;
    		 cout << "\nYear:   " << CarYear;
    		 cout << "\nMileage:  " << Mileage;
    							   
    		 fclose(CarInventory);
    	
    		 cout << endl;
    		 return 0;
    }
    Compiles and runs just fine but after I imput the info it crashes giving me the whole "This program has ran in to an error and needs to close" blah blah typical MS stuff.
    Find the loop holes, then fix the errors, after you have found all the useful info

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    @ arjunajay
    Read the FAQ - http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    @ Keiyentai
    1. You don't check for errors on your fopen calls
    Code:
    FILE *CarInventory = fopen("cars.inv", "w");
    if ( CarInventory == NULL ) {
      // do something else
    } else {
      // read the file
    }
    > CarInventory = fopen("cars.inv", "r+");
    The bigger problem here (apart from not checking for errors) is that you've still got the file open for writing, and now you're trying to open it for reading.

    Try
    Code:
    fclose( CarInventory );
    CarInventory = fopen("cars.inv", "r+");
    I would suggest you stick to "r" mode until you're ready for the extra features which "+" gets you.
    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.

  9. #9
    C++ newb in training
    Join Date
    Mar 2005
    Location
    Burlington Wa
    Posts
    14
    arg...ok now it dies in the middle of doing work after making these changes.

    Code:
     // Program to perform file processing using the FILE structure
    // Author: Jules Larson. Revised by Jeremy Jones 2005
    #include <iostream>
    using namespace std;
    int main()
    {
    	char Make[20], Model[20];
    	unsigned int CarYear;
    	long Mileage;
    	
    	FILE *CarInventory = fopen("cars.inv", "w");
    	if (CarInventory == NULL ) {
    	   // do something else
    	   } else {
    	   // read the file
    }					 
    	cout << "Enter the following pieces of information\n";
    	cout << "Make:	 "; gets(Make);
    	cout << "Model:	"; gets(Model);
    	cout << "Year:	"; cin >> CarYear;
    	cout << "Mileage:  "; cin >> Mileage;
    	
    	fprintf(CarInventory, "%s\n", Make);
    	fprintf(CarInventory, "%s\n", Model);
    	fprintf(CarInventory, "%s\n", CarYear);
    	fprintf(CarInventory, "%s\n", Mileage);
    
    		  CarInventory = fopen("cars.inv", "r+");
    	
    		 fscanf(CarInventory, "%s\n", Make);
    		 fscanf(CarInventory, "%s\n", Model);
    		 fscanf(CarInventory, "%d\n", &CarYear);
    		 fscanf(CarInventory, "%d\n", &Mileage);
    		 
    		 cout << "Information about the car";
    		 cout << "\nMake:   " << Make;
    		 cout << "\nModel:  " << Model;
    		 cout << "\nYear:   " << CarYear;
    		 cout << "\nMileage:  " << Mileage;
    							   
    		 fclose(CarInventory);
    		 CarInventory = fopen("cars.inv", "r+");
    		 
    		 return 0;
    }
    hmm maybe I am just not seeing something or to new to C\C++ sorry for the anoyence. Just dont know why I am getting the "Windows needs to close this program" error...mar..Thanks for the help anyways.
    Find the loop holes, then fix the errors, after you have found all the useful info

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe you should start with reading.
    I rather expected you to intelligently apply what I said to your code, not blindly copy/paste it into what you have.

    Like this for example
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        char Make[20], Model[20];
        unsigned int CarYear;
        long Mileage;
    
        FILE *CarInventory = fopen("cars.inv", "w");
        if (CarInventory == NULL) {
            cout << "Can't open";
        } else {
            cout << "Enter the following pieces of information\n";
            cout << "Make:	 ";
            gets(Make);
            cout << "Model:	";
            gets(Model);
            cout << "Year:	";
            cin >> CarYear;
            cout << "Mileage:  ";
            cin >> Mileage;
    
            fprintf(CarInventory, "%s\n", Make);
            fprintf(CarInventory, "%s\n", Model);
            fprintf(CarInventory, "%s\n", CarYear);
            fprintf(CarInventory, "%s\n", Mileage);
            fclose(CarInventory);
        }
    
        CarInventory = fopen("cars.inv", "r");
        if (CarInventory == NULL) {
    
        } else {
            fscanf(CarInventory, "%s\n", Make);
            fscanf(CarInventory, "%s\n", Model);
            fscanf(CarInventory, "%d\n", &CarYear);
            fscanf(CarInventory, "%d\n", &Mileage);
    
            cout << "Information about the car";
            cout << "\nMake:   " << Make;
            cout << "\nModel:  " << Model;
            cout << "\nYear:   " << CarYear;
            cout << "\nMileage:  " << Mileage;
    
            fclose(CarInventory);
        }
        return 0;
    }
    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.

  11. #11
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    You forgot to close the file before reopening it.
    Code:
    fclose( CarInventory );   /* Add the close statement here */
    CarInventory = fopen("cars.inv", "r+");
    btw, kimi, nihonjin desuka?

  12. #12
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    also, change your code
    Code:
    fprintf(CarInventory, "%s\n", CarYear);
    fprintf(CarInventory, "%s\n", Mileage);
    to
    Code:
    	fprintf(CarInventory, "%d\n", CarYear);
    	fprintf(CarInventory, "%d\n", Mileage);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-31-2015, 02:11 PM
  2. Compile error? Logic error? Syntax Error? Please help
    By Khody Afkhami in forum C Programming
    Replies: 4
    Last Post: 10-11-2014, 01:36 AM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  5. Replies: 3
    Last Post: 10-02-2007, 09:12 PM