Thread: Please check my C++

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Please check my C++

    Being doing C wanna get involved in C++... Will do this by showing you my Program... I will post two files first to try avoid confusion... After these files has been scrutinized and corrected, will post the next file... & so forth... I will also include the errors am getting

    TITLE & Description: To Read from file & List a fleet of cars, and update them
    Program Usage: Car Rental Shop

    First File ... Class Car

    Car.h
    Code:
    #include <string>
    #include <fstream.h>
    #include <iostream>
    #include "Menu.h"
    
    #ifndef DATE
    #define DATE 1
    
    struct Date						
    {
    	unsigned day;
    	unsigned month;
    	unsigned year;
    };
    
    #endif
    
    class Car						
    {
    	
    public: 
    	
    	Car();						
    	
    	enum TankStatus {unknown, empty, half, full};	 
    	static std::string getTankStatusName(TankStatus);
    	bool sameModel(const std::string&) const;
    	bool operator<(const Car&) const;	
    	void read(FILE *, bool);
    	void write(FILE *, bool);
    	void setDatesAndTank(Car&);
    	CarFleetIterator find(CarFleetIterator&, CarFleetIterator&, std::string);
    
    private:
    
    	std::string	make;			// Make as in..."ford"
    	std::string	model;			// Model as in ... "icon"
    	unsigned	engineCapacity;	          // Engine capacity may be stored as "1800" not 1.8
    	unsigned	yearModel;			
    	Date		returnDate;			
    	TankStatus	tankStatus;		// Maybe filled, half-filled, or empty
    };
    Q : Does this class make sense? should read/write member functions be there...
    Errors :
    1. <fstream.h> No such file or directory ??? Using VS 2005
    2. missing ';' before identifier 'find' ... Find defined below in Car.cpp


    Car.cpp
    Code:
    #include "car.h"
    
    Car::Car() {};	
    
    std::string Car::getTankStatusName(TankStatus status)
    {
    	switch (status)
    	{
    		case unknown: 
    			return "unknown";
    			break;
    		case empty: 
    			return "Empty";
    			break;
    		case half: 
    			return "Half-Empty";
    			break;
    		case full: 
    			return "Full";
    			break;
    		default:
    			throw std::runtime_error("Invalid tank status");
    			return "";
    	} 
    }
    
    // Operator < will be used by sort() function 
    
    bool Car::operator<(const Car& car) const
    {
    	return make < car.make;
    }
    
    
    // Loading data from the text file
    
    void Car::read(FILE *fin, bool file_out)
    {
    	char make_temp[15], model_temp[15];
    
    	if(file_out)	// Get record from file
    	{	
    		fscanf(fin, "&#37;s%s", make_temp, model_temp);
    		fscanf(fin, "%d", &yearModel);
    		fscanf(fin, "%d", &engineCapacity);
    		fscanf(fin, "%d%d%d", &returnDate.day, &returnDate.month, &returnDate.year);
    		fscanf(fin, "%d", &tankStatus);
    		make = make_temp;
    		model = model_temp;
    	}
    	else	// Get record from user
    	{
    		fscanf(fin, "%s%s", make_temp, model_temp);
    		fscanf(fin, "%d", &yearModel);
    		fscanf(fin, "%d", &engineCapacity);
    		fscanf(fin, "%d", &tankStatus);
    		make = make_temp;
    		model = model_temp;
    
    		// No dates  yet!
    		returnDate.day  = returnDate.month = returnDate.year =0;
    	}
    }
    
    // Saving data to the text file
    
    void Car::write(FILE *fout, bool file_out)
    {
    	char make_temp[15], model_temp[15];
    
    	strcpy(make_temp, make.c_str());
    	strcpy(model_temp, model.c_str());
    
    	if(file_out)	// To the output screen
    	{
    	
    		fprintf(fout, "%-9s%-11s", make_temp, model_temp);
    		fprintf(fout, "%-12d", yearModel);
    		fprintf(fout, "%-10d", engineCapacity);
    		fprintf(fout, "%-3d%-3d%-8d", returnDate.day, returnDate.month, returnDate.year);
    		fprintf(fout, "%-4s", getTankStatusName(tankStatus).c_str());
    
    		puts("");
    	}
    	else			// To the file
    	{
    		fprintf(fout, "%s %s ", make_temp, model_temp);
    		fprintf(fout, "%d ", yearModel);
    		fprintf(fout, "%d ", engineCapacity);
    		fprintf(fout, "%d %d %d ", returnDate.day, returnDate.month, returnDate.year);
    		fprintf(fout, "%d", tankStatus);
    	}
    }
    
    // Comparing the user entry with current record
    
    bool Car::sameModel(const std::string& car_model) const
    {
    	return model == car_model;	
    }
    
    // Allow Dates and tank to be modified
    
    void Car::setDatesAndTank(Car& car_dates_tank)
    {
    	cout << "\nEnter return dates [DAY - MONTH - YEAR]" << endl;
    	cin >> car_dates_tank.returnDate.day;
    	cin >> car_dates_tank.returnDate.month; 
    	cin >> car_dates_tank.returnDate.year;
    
    	cout << "\n\nEnter tank status [1...3]";
    	scanf("%d", car_dates_tank.tankStatus);
    
    	/* 
    	try {
    		cin >> car_dates_tank.tankStatus;
    	} 
    	catch ( out_of_range& ex ) {
    		cerr<< ex.what() <<endl;
    	}
    	*/
    }
    
    Menu::CarFleetIterator Car::find(CarFleetIterator& b, CarFleetIterator& e, string s)
    {
    	int pos;
    
    	Car& car = *b;
    	
    	// Find the make from the list from begin to end
    
    	while (b <= e)
    	{	
    		if( pos = strcmp(s, make) )
    		{ 
    			return b;             
    		}
    		else {
    			b++;
    		}
    	} 
    
    	return e;
    }
    Lastly, Menu.h

    Code:
    // Purpose	: To declare a class Menu
    
    #pragma once
    
    #include <vector>
    #include "Car.h"
    
    /* Menu class acts as a Main controller */
    
    class Menu
    {
    
    public:
    
    	Menu();
    
    	void run();
    
    private:
    
    	enum Options {DESCRIBE = 49, READF, DISPLAY, SEARCHF, ADDCAR, UPDATEF, EDITF};	// Menu Options
    	typedef std::vector<Car> CarFleet;
    	typedef CarFleet::iterator CarFleetIterator;
    
    private:
    
    	void pause();
    	void clrscr();
    	void sortAlpha();
    	void AddNewCar();
    	void EditFleet();
    	void programLimits();
    	void titleDescription();
    	void updateFleetFile();
    	void readFleetFromFile();
    	CarFleetIterator searchCarDetails(bool&);
    	void showFleetList(CarFleetIterator, bool);
    
    private:
    
    	CarFleet fleet; 
    };
    NOTE: In Car.h i included Menu.h, then in Menu.h i included Car.h... I think there's a problem there... But i need the vector declared in Menu.h within Car class & the Car object within Menu.h ... How do i resolve this error " 'Car' : 'class' type redefinition "
    Last edited by csonx_p; 07-09-2008 at 04:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 11:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM