2014-02-09 223 views
-1

我想創建一個使用動態分配數組而不是矢量的函數,因爲我想看看它們是如何工作的。基本上,這個功能要求用戶輸入他們要輸入的人數以及他們的姓名;然後,他們輸入這些人中有多少人想註冊一個ID,然後是他們的電話#。比較兩個數組C++

例如:

"How many customers will you like to enter? " 3 //user inputs 3 
Bob Allen //user input 
Ellen Michaels //user input 
Jane Andrews //user input 

"How many people want to register for the VIP ID? " 4 //user inputs 4; user can enter a new name here if they forgot to enter it the first time 
Jane Andrews 213-2312 
Bob Allen 111-1212 
Jonathan Drew 211-9283 
Michael Jacks 912-3911 

//what the program spits out after the function is implemented 
Final Registration: Guaranteed VIP IDs 
Bob Allen 111-1212 
Ellen Michaels NOT GUARANTEED 
Jane Andrews 213-2312 

//The new users entered in the 2nd round will not be listed in the final registration because it is too late for them to sign up 

基本上,我遇到的問題是:

1. How to check if the person is not listed in the second round 
2. Ignore the new people the user entered in the second round 
3. Print the final registration in the same order the user entered the names in the first round 

這就是我現在所擁有的,但是這隻能如果相同的用戶數是進入第一次和第二次。它不檢查是否有新的或省略的

string* people; 
cout << "How many customers will you like to enter? " << endl; 
int howmany; 
cin >> howmany; 
people = new int[howmany]; 

for (int i=0;i<howmany;i++) 
{ 
    cin >> people[i]; 
} 

cout << "How many people want to register for the VIP ID? " << endl; 
int num_of_register; 
string* vip; 
cin >> num_of_register; 
vip = new int[num_of_register]; 

for (int i=0;i<num_of_register) 
{ 
    cin >> vip[i]; 
    for (int j=0;j<howmany;j++) 
    { 
     if (num_of_register == howmany) { 
      if people[j] == vip[i] //check if they're the same people or not 
        cout << "Final Registration: Guaranteed VIP Ids" << endl; 
        cout << people[i]; 
     else { 
      //check if the names are the same 
     } 
    } 
} 

任何指導/建議將有所幫助。我不知道如何解決這個問題。謝謝!

+0

這是如何編譯的?你聲明瞭一個字符串*,但是給它賦值「new int [whatever]」。 – PaulMcKenzie

回答

1

首先你得到howmany併爲人分配內存。在第二輪中,用戶有機會進入一個新的人。您可以先獲取客戶數量和名稱,然後在第二部分中檢查名稱是否存在於第一部分

cin >> num_of_register; 
if(num_of_register > howmany) 
{ 
    cout << "You can't register new people\n"; 
} 

for (int i=0;i<num_of_register; i++) 
{ 
    cin >> vip[i]; 
    int customerExists = -1; 

    for (int j=0;j<howmany;j++) 
    { 
     if(people[j] == vip[i]) 
     { 
      customerExists = i; 
      break; 
     } 
    } 
    if(customerExists == -1) 
    { 
     cout << "You can not enter new customer\n"; 
     // get people[i] again 
     i--; 
    } 
    else 
    { 
     cout << "Final Registration: Guaranteed VIP Ids" << endl; 
     cout << people[customerExists]; 
    } 
}