i need to write a class for driver.cpp file

// Chapter 13, Programming Challenge 3 Driver
//
#include "Car.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
   // Loop counter
   int count;
   string myMake="Porsche";
   
   // Create a car object.
   Car porsche(2006, myMake);
   
   // Display the car that was created
   cout << "Car Year: " << porsche.getYearModel();
   cout << "   Make: " << porsche.getMake();
   cout << "   Speed: " << porsche.getSpeed() << endl << endl;
   
   // Display the current speed.
   cout << "Current speed: " 
        << porsche.getSpeed() 
        << endl;
   
   // Accelerate five times.
   for (count = 0; count < 5; count++)
   {
      // Accelerate and display the speed.
      cout << "Accelerating...\n";
      porsche.accelerate();
      cout << "Current speed: " 
           << porsche.getSpeed() 
           << endl;
   }

   // Brake five times.
   for (count = 0; count < 5; count++)
   {
      // Brake and display the speed.
      cout << "Braking...\n";
      porsche.brake();
      cout << "Current speed: " 
           << porsche.getSpeed() 
           << endl;
   }

}

here is the class that i wrote

#ifndef CAR_H
#define CAR_H
#include<string.h>

class Car
{
private:
	int YearModel;
	std::string Make;
	int Speed;

public:
	Car::Car(int year,std::string mk,int s=0)
	{
		YearModel=year;
		Make=mk;
		Speed=s;
	}

	int getYearModel() const
	{
		return YearModel;
	}

	std::string getMake() const
	{
		return Make;
	}

	int getSpeed() 
	{	
		return Speed;
	}
	
	int accelerate() 
	{
		getSpeed();
		return Speed=Speed+5;
		 
	}

	int brake()
	{	
		getSpeed();
		return Speed=Speed-5;
	 
	}

};

#endif

i am using visual studio 2008
while linking it shows me this :

LINK : C:\Users\Ankit Arya\Documents\Visual Studio 2008\Projects\car\Debug\car.exe not found or not built by the last incremental link; performing full link
1>Embedding manifest...


thought it then compiles and runs perfectly ryt after that....

i need to submit this HW on some WEB-CAT tool on which after submitting it gives me an error that it cannot compile my class...

The following specific error(s) were discovered while compiling reference tests against your submission:

Car.h:51:7: warning: no newline at end of file
<<reference tests>>:13: error: `string' undeclared (first use this function)

can anybody has any idea what is wrong with my class coz it runs f9 on visual studio.

Ignore the first warning about incremental linking -- I get that sometimes too and means nothing.

The second problem, just go to the bottom of the source code file and hit the Enter key so that there is a blank line at the bottom of the program.

the header file known as string.h and the string header file in namespace std are not the same. Therefore when using the std:: prefix in declaring string objects in the Car.h file you don't have the correct header file included.

okey so newline problem solved...thanks "ancient Dragon"

abt the 2nd error i changed my #include file to #include<string>.........is that what u were trying to suggest lerner ..frankly i dint really gt it..

<string> is where std::string class is declared. <string.h> declares C string handling functions such as strlen(), strcat() etc. It has nothing to do with std::string class.

here is my changed class code:

#ifndef CAR_H
#define CAR_H
#include<string> //changed as suggested in both class and .cpp file

class Car
{
private:
	int YearModel;
	std::string Make;
	int Speed;

public:
	Car::Car(int year,std::string mk,int s=0)
	{
		YearModel=year;
		Make=mk;
		Speed=s;
	}

	int getYearModel() const
	{
		return YearModel;
	}

	std::string getMake() const
	{
		return Make;
	}

	int getSpeed() 
	{	
		return Speed;
	}
	
	int accelerate() 
	{
		getSpeed();
		return Speed=Speed+5;
		 
	}

	int brake()
	{	
		getSpeed();
		return Speed=Speed-5;
	 
	}

};

#endif

the same error still persists...

The error is on line 13 of the main.cpp, not car.h. change it to this: std::string myMake="Porsche";

ok gt ya but the the problem is when i submit this file as home work it uses its own driver.cpp file not the one stated above (the one just fr me to get the idea of what my class should have) so I can just submit car.h file......i have no control over driver.cpp.....

probably i should go and tell my professor coz it might be that his .cpp file may have the error.....

neways thanks a lot buddy!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.