| | |
Debug
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
//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
•
•
Join Date: Mar 2006
Posts: 4
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
//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
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:-
But you might not be familiar with pointers and dynamic memory. That's the basic idea though.
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:-
C++ Syntax (Toggle Plain Text)
//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.
*Voted best profile in the world*
![]() |
Similar Threads
- Outlook giving a debug error (Windows Software)
- How do i debug this app? (Java)
- "Run time error, do you wish to debug?" (Web Browsers)
- cannot debug ASP (ASP)
- Need to debug (Web Browsers)
- Windows 2000 Debug file (Windows NT / 2000 / XP)
- Debug message (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: read & write Binary data
- Next Thread: visual c++ .net 2003 command prompt
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






