944,001 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1144
  • C++ RSS
May 26th, 2006
0

Debug

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
debugger is offline Offline
4 posts
since Mar 2006
May 26th, 2006
0

Re: Debug

>#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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 26th, 2006
0

Re: Debug

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
debugger is offline Offline
4 posts
since Mar 2006
May 26th, 2006
0

Re: Debug

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:-

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: read & write Binary data
Next Thread in C++ Forum Timeline: visual c++ .net 2003 command prompt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC