Debug

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 4
Reputation: debugger is an unknown quantity at this point 
Solved Threads: 0
debugger debugger is offline Offline
Newbie Poster

Debug

 
0
  #1
May 26th, 2006
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. //car.cpp
  2. //program to get information about a persons cars.
  3.  
  4. #include<stdafx.h>
  5. #include<iostream>
  6. #include<string.h>
  7.  
  8. using namespace std;
  9.  
  10. int number;
  11. float total = 0;
  12.  
  13.  
  14. class car
  15. {
  16. private:
  17.  
  18. char colour[10];
  19. float weight;
  20. char model[50];
  21.  
  22. public:
  23.  
  24. void setdata(char c[], float w, char m[])
  25. {
  26. strcpy_s(colour,c);
  27. weight = w;
  28. strcpy_s(model, m);
  29. }
  30.  
  31. void showdata()
  32. {
  33. for(int i = 0; i<number; i++)
  34. {
  35. total += fleet[i].weight;
  36. }
  37. cout << "\n Your " << number << " cars weigh a total of " << total << " pounds." <<endl;
  38.  
  39. }
  40.  
  41. };
  42.  
  43. void main()
  44. {
  45.  
  46.  
  47. cout << "\nPlease enter the number of cars you own.";
  48. cin >> number;
  49.  
  50. new car fleet[number];
  51.  
  52.  
  53. char col[10];
  54. float wgt;
  55. char mod[50];
  56. float total;
  57.  
  58.  
  59.  
  60. for(int i = 0; i<number; i++)
  61. {
  62. cout << "\n Please enter the colour of car number" << i+1 << ":"<<endl;
  63. cin >> col;
  64.  
  65. cout << "\n Please enter the weight of car number"<< i+1 <<":"<<endl;
  66. cin >> wgt;
  67.  
  68. cout << "\n Please enter the model of car number" << i+1 << ":"<<endl;
  69. cin >> mod;
  70.  
  71. fleet[i].setdata(col,wgt,mod);
  72. fleet[i].showdata();
  73.  
  74. }
  75.  
  76.  
  77. }//end main
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Debug

 
0
  #2
May 26th, 2006
>#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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 4
Reputation: debugger is an unknown quantity at this point 
Solved Threads: 0
debugger debugger is offline Offline
Newbie Poster

Re: Debug

 
0
  #3
May 26th, 2006
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

  1. //car.cpp
  2. //program to get information about a persons cars.
  3.  
  4.  
  5. #include<iostream>
  6. #include<string.h>
  7.  
  8. using namespace std;
  9.  
  10. int number;
  11. float total = 0;
  12.  
  13.  
  14. class car
  15. {
  16. private:
  17.  
  18. char colour[10];
  19. float weight;
  20. char model[50];
  21.  
  22. public:
  23.  
  24. void setdata(char c[], float w, char m[])
  25. {
  26. strcpy(colour,c);
  27. weight = w;
  28. strcpy(model, m);
  29. }
  30.  
  31. void showdata()
  32. {
  33. for(int i = 0; i<number; i++)
  34. {
  35. total += fleet[i].weight;
  36. }
  37. cout << "\n Your " << number << " cars weigh a total of " << total << " pounds." <<endl;
  38.  
  39. }
  40.  
  41. };
  42.  
  43. int main()
  44. {
  45.  
  46.  
  47. cout << "\nPlease enter the number of cars you own.";
  48. cin >> number;
  49.  
  50. car fleet[number];
  51.  
  52.  
  53. char col[10];
  54. float wgt;
  55. char mod[50];
  56. float total;
  57.  
  58.  
  59.  
  60. for(int i = 0; i<number; i++)
  61. {
  62. cout << "\n Please enter the colour of car number" << i+1 << ":"<<endl;
  63. cin >> col;
  64.  
  65. cout << "\n Please enter the weight of car number"<< i+1 <<":"<<endl;
  66. cin >> wgt;
  67.  
  68. cout << "\n Please enter the model of car number" << i+1 << ":"<<endl;
  69. cin >> mod;
  70.  
  71. fleet[i].setdata(col,wgt,mod);
  72. fleet[i].showdata();
  73.  
  74. }
  75.  
  76.  
  77. }//end main
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Debug

 
0
  #4
May 26th, 2006
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:-

  1. //car.cpp
  2. //program to get information about a persons cars.
  3.  
  4. //#include<stdafx.h>
  5. #include<iostream>
  6. #include<string.h>
  7.  
  8. using namespace std;
  9.  
  10. int number;
  11. float total = 0;
  12.  
  13.  
  14. class car
  15. {
  16. private:
  17.  
  18. char colour[10];
  19. double weight;
  20. char model[50];
  21.  
  22. public:
  23.  
  24. void setdata(char c[], double w, char m[])
  25. {
  26. strcpy(colour,c);
  27. weight = w;
  28. strcpy(model, m);
  29. }
  30. double getweight()
  31. {
  32. return weight;
  33. }
  34.  
  35. void showdata()
  36. {
  37.  
  38. cout<<model<<"\t: Weight:"<<weight<<"\t: Color:"<<colour<<endl;
  39.  
  40. }
  41.  
  42. };
  43.  
  44. int main()
  45. {
  46.  
  47.  
  48. cout << "\nPlease enter the number of cars you own:";
  49. cin >> number;
  50.  
  51. car *carpt = new car[number];
  52. car *pt;
  53.  
  54.  
  55. char col[10];
  56. double wgt;
  57. char mod[50];
  58.  
  59.  
  60.  
  61. pt = carpt;
  62. for(int i = 0; i<number; i++)
  63. {
  64. cout << "\n Please enter the colour of car number " << i+1 << ":"<<endl;
  65. cin >> col;
  66.  
  67. cout << "\n Please enter the weight of car number"<< i+1 <<":"<<endl;
  68. cin >> wgt;
  69.  
  70. cout << "\n Please enter the model of car number" << i+1 << ":"<<endl;
  71. cin >> mod;
  72.  
  73. pt->setdata(col,wgt,mod);
  74.  
  75. pt++;
  76.  
  77. }
  78.  
  79. double total_weight=0;
  80. for(int i = 0; i<number; i++)
  81. {
  82.  
  83. carpt[i].showdata();
  84. total_weight+=carpt[i].getweight();
  85.  
  86.  
  87. }
  88. delete[] carpt; //free memory
  89.  
  90. cout<<"The combined weight is "<<total_weight;
  91.  
  92. cin.get();
  93. cin.get();
  94.  
  95. return 0;
  96.  
  97. }//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*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC