我已經完成了下面的代碼來交換我的4件衣服,褲子,鞋子和洗漱用品。我希望每個對象都可以保存屬性的數據。但是,儘管我已經糾正了很多次,但仍然有太多的錯誤。如何使用數組,C++交換對象。我想溝通
#include <iostream>
#include <ostream>
#include <cstdlib>
#include <string>
#include <windows.h>
#include <ctime>
using namespace std;
class MySuitcase // Class
{
// Declare all the propertie used for the items.
public:
int quantity;
int size;
int colour;
double weight;
double volume;
char name;
MySuitcase() // Constructor
{
quantity = 0;
size = 0;
colour = 0;
weight = 0.0;
volume = 0.0;
}
// Constructor for items name
MySuitcase (char name_given)
{
name = name_given;
}
// Function prototype
void swap();
void items_name();
};
// Swap Function
void swap()
{
int temp;
int Suitcase[j];
temp = Suitcase[j];
Suitcase[j] = Suitcase[i];
Suitcase[i] = temp;
}
// List of item on display
void items_name()
{
string Suitcase[5];
Suitcase[0]="Clothes";
Suitcase[1]="Pants";
Suitcase[2]="Shoes";
Suitcase[3]="Toiletries";
Suitcase[4]="Total volume & weight of suitcase.";
}
void Propmt_CLothes()
{ int Suitcase;
for (i=0; i<6; i++)
cout << " Please input the quantity: ";
cin >> Clothes[i].quantity;
cout << " Please input the weight: ";
cin >> Clothes[i].weight;
cout << " Please input the volume: ";
cin >> Clothes[i].volume;
cout << " Please input the size: ";
cin >> Clothes[i].size;
cout << " Please input the colour: ";
cin >> Clothes[i].colour;
}
int main(int argc, char** argv)
{
MySuitcase Suitcase[5];
MySuitcase Suitcase[0] = new Suitcase(Clothes);
// Display initial list of items
cout << " This is the list of items for your suitcase: \n";
for (i=0; i<5; i++)
{
MySuitcase().items_name();
cout << i << " - "<< Suitcase[i] << endl;
}
// Prompt user to choose items for swapping
cout << " Please choose 2 items you want to swap: \n";
cin >> i, j;
return 0;
}
也發佈錯誤 – brokenfoot
代碼中有很多錯誤。你的交換功能是你最擔心的問題。您似乎無法理解變量作用域或函數參數。我不想失禮。開始時要小得多,隨時解決錯誤,直到你學習這個例子。 – AndyG
首先看到變量的範圍,然後使用char來存儲名稱,首先更正它們並正確解釋您的需求 – balaji