2014-03-19 18 views
-4

我已經完成了下面的代碼來交換我的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; 
} 
+1

也發佈錯誤 – brokenfoot

+1

代碼中有很多錯誤。你的交換功能是你最擔心的問題。您似乎無法理解變量作用域或函數參數。我不想失禮。開始時要小得多,隨時解決錯誤,直到你學習這個例子。 – AndyG

+0

首先看到變量的範圍,然後使用char來存儲名稱,首先更正它們並正確解釋您的需求 – balaji

回答

1

1 - 你應該聲明你在這個函數swap使用像j任何變量女巫:

// Swap Function 
void swap() 
{ // what are i and j 
    int temp; 
    int Suitcase[j]; 

    temp = Suitcase[j]; 
    Suitcase[j] = Suitcase[i]; 
    Suitcase[i] = temp; 
} 

而像i的int for-loopfor (int i = 0; i < 6; i++)並添加{}執行中的所有指令-loop like:

for (int i = 0; i < 6; i++) { 
    cout << " Please input the quantity: "; 
    // ... 
} 

2 - 您有創建一個abject(套房)數組的錯誤。你應該聲明它是這樣的:

MySuitcase Suitcase[6]; // in Main method 

3 - 不要忘了在使用前也聲明變量是:

int i, j; 
cin >> i, j; 

4 - 在

void Propmt_CLothes() { 
    MySuitcase Clothes[6]; 
    for (int 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; 
    } 
} 

而許多陣列的宣言其他錯誤。您應該閱讀the C++ doc

+0

我是什麼?爲什麼他交換一個空的數組? – balaji

0

您似乎對C++有一些誤解。

  • Variable Scope定義變量所在的「where」。當你在一個地方創建一個變量時,它並不總是可以在代碼的其他地方訪問。

例如:

void foo(){ 
    int a = 0; 
} 

void bar(){ 
    int b = 1; 
} 

abar訪問,bfoo訪問。這是因爲變量只存在於函數的範圍內。點擊鏈接瞭解更多信息。

 
void swap(int& a, int& b){ 
    int tmp = a; 
    a = b; 
    b = tmp; 
} 

只能修改全局訪問的變量(見範圍),或這些變量傳遞到或聲明的函數裏面。在這裏,我們的swap函數需要2 ints並交換它們的值。 int s作爲參數被賦予(也稱爲參數)

找出這些鏈接,並使用該知識來更正您的代碼。你會看到,聲明`旅行箱

  • 在C++中,您可以訪問array值略有不同比你想,例如

int Suitcase[j];

沒有得到第j 元素Suitcase。事實上,這個說法根本就沒有意義。閱讀創建和訪問數組以瞭解更多信息。

閱讀關於這些主題的更多信息,然後重新訪問您的代碼。你會希望看到你不恰當地試圖用C++表達自己,並且可以糾正它(沒有我們的幫助!)

相關問題