Hi there, could someone please help me debug this code. I need it to perform the following tasks
1. Ask the user for the number of cars he or she owns.
2. Create a class to represent a car, and then declare an array of cars.
3. Ask the user what the color, weight, and model is for each car he or she owns. Store this in the array.
4. Once the user has entered all data, calculate the combined weight of the cars.

//car.cpp
//program to get information about a persons cars.

#include<stdafx.h>
#include<iostream>
#include<string.h>

using namespace std;

int number;
float total = 0;


class car
{
	private:
	
	char colour[10];
	float weight;
	char model[50];

	public:
	
	void setdata(char c[], float w, char m[])
	{
		strcpy_s(colour,c);
		weight = w;
		strcpy_s(model, m);
	}

	void showdata()
	{
		for(int i = 0; i<number; i++)
	{
		total += fleet[i].weight;
	}
	cout << "\n Your " << number << " cars weigh a total of " << total << " pounds." <<endl;

	}

};

void main()
{
	

	cout << "\nPlease enter the number of cars you own.";
	cin  >> number;
	
	new car fleet[number];
	
		
	char col[10];
	float wgt;
	char mod[50];
	float total;

	
	
	for(int i = 0; i<number; i++)
	{
		cout << "\n Please enter the colour of car number" << i+1 << ":"<<endl;
			cin >> col; 
		
			cout << "\n Please enter the weight of car number"<< i+1 <<":"<<endl;
			cin >> wgt;
			
			cout << "\n Please enter the model of car number" << i+1 << ":"<<endl;
			cin >> mod;
	
			fleet[i].setdata(col,wgt,mod);
			fleet[i].showdata();
	
	}

	
}//end main
Member Avatar for iamthwee

>#include<stdafx.h>

What's that get rid of it?

>strcpy_s

What's that drop the subscript s.

>void main()

main should return an int, change to int main.

>new car fleet[number];

You don't need the new keyword

>fleet

This is not defined.

Is this better?
Can you please suggest a good C++ compiler. This code will not run on VC++ 8 thats is why I had all those additional things in my code.
Thanks

//car.cpp
//program to get information about a persons cars.


#include<iostream>
#include<string.h>

using namespace std;

int number;
float total = 0;


class car
{
	private:
	
	char colour[10];
	float weight;
	char model[50];

	public:
	
	void setdata(char c[], float w, char m[])
	{
		strcpy(colour,c);
		weight = w;
		strcpy(model, m);
	}

	void showdata()
	{
		for(int i = 0; i<number; i++)
	{
		total += fleet[i].weight;
	}
	cout << "\n Your " << number << " cars weigh a total of " << total << " pounds." <<endl;

	}

};

int main()
{
	

	cout << "\nPlease enter the number of cars you own.";
	cin  >> number;
	
	car fleet[number];
	
		
	char col[10];
	float wgt;
	char mod[50];
	float total;

	
	
	for(int i = 0; i<number; i++)
	{
		cout << "\n Please enter the colour of car number" << i+1 << ":"<<endl;
			cin >> col; 
		
			cout << "\n Please enter the weight of car number"<< i+1 <<":"<<endl;
			cin >> wgt;
			
			cout << "\n Please enter the model of car number" << i+1 << ":"<<endl;
			cin >> mod;
	
			fleet[i].setdata(col,wgt,mod);
			fleet[i].showdata();
	
	}

	
}//end main
Member Avatar for iamthwee

Hmm, have you tried dev++

I see what you were doing now with the new keyword. So you might need that.

To be honest there is so many ways you can write this. It all depends on the style your teacher is expecting from you.

For example you could do it like so:-

//car.cpp
//program to get information about a persons cars.

//#include<stdafx.h>
#include<iostream>
#include<string.h>

using namespace std;

int number;
float total = 0;


class car
{
	private:
	
	char colour[10];
	double weight;
	char model[50];

	public:
	
	void setdata(char c[], double w, char m[])
	{
		strcpy(colour,c);
		weight = w;
		strcpy(model, m);
	}
	double getweight()
	{
        return weight;
    }

	void showdata()
	{
		
		  cout<<model<<"\t: Weight:"<<weight<<"\t: Color:"<<colour<<endl;

	}

};

int main()
{
	

	cout << "\nPlease enter the number of cars you own:";
	cin  >> number;
	
	car *carpt = new car[number];
	car *pt;
	
		
	char col[10];
	double wgt;
	char mod[50];
	

	
	pt = carpt;
	for(int i = 0; i<number; i++)
	{
		cout << "\n Please enter the colour of car number " << i+1 << ":"<<endl;
			cin >> col; 
		
			cout << "\n Please enter the weight of car number"<< i+1 <<":"<<endl;
			cin >> wgt;
			
			cout << "\n Please enter the model of car number" << i+1 << ":"<<endl;
			cin >> mod;
	
			pt->setdata(col,wgt,mod);
			
			pt++;
	
	}
	
	double total_weight=0;
	for(int i = 0; i<number; i++)
	{
		
	    carpt[i].showdata();
		total_weight+=carpt[i].getweight();
		
	
	}
	delete[] carpt;    //free memory
  
  cout<<"The combined weight is "<<total_weight;
  
  cin.get();
  cin.get();
  
  return 0;
	
}//end main

But you might not be familiar with pointers and dynamic memory. That's the basic idea though.

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.