C\C++ tut error

This is a discussion on C\C++ tut error within the C++ Programming forums, part of the General Programming Boards category; Hey everyone I did a tutorial on a website that seems to combines C and C++ for the program witch ...

  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,317
    ^^ 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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    @ 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    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
    212
    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. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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