2014-09-30 67 views
-1

我在程序中編寫了一個函數,用於輸入唯一號碼,但它不起作用。 for循環出錯了。程序要求用戶輸入非重複號碼C++

我需要驗證該員工ID是唯一的。 我做了一個名爲employee的結構,「emp.id」是員工ID。當用戶輸入一個ID時,它不應該與之前輸入的以前的ID相匹配。這只是主程序的一個功能,它驗證員工ID是唯一的。

void uniquieid()  
{ 
    int check,i; 
    string code; 
    string tempemp1; 
    cout<< "enter id"; 
    cin>> code; 
    while(!(num-1)) 
    { 
     for(i=0;i<=num-1;i++) 
     { 
      if(emp[i].id.compare(code)==0)//comparing 
      { 
       check =1; 
       cout<<"enter id again"; 
       break; 
      } 
      if(check=0) //csaasc 
      { 
       emp[i].id=code; 
      } 
     } 
    } 
    getch(); 
} 
+0

使用映射''檢查,如果用戶輸入一個已經輸入的ID。看起來你的情況下'id'是字符串。使用'map ' – venki421 2014-09-30 20:15:38

+0

你不能從我的答案更新這個問題。如果你需要更多的幫助,你可以創建一個新的問題,你甚至可以在那裏使用我的答案。你應該更準確地描述症狀。 – quamrana 2014-10-01 05:40:17

回答

0

您可以發佈員工結構嗎? 因爲從這裏,一切看起來都不錯,但您的if函數指的是emp。 所以你的結構中的某些東西導致了這個問題。 沒有你的結構,任何人回答可能無法找到問題。

現在,我建議你做的就是將僱員ID存儲在一個向量中,並使用for循環遍歷它。

你可以做

void uniqueid() { 
    std::vector<std::string> empIds; 
    std::string code; 
    CODE TO STORE IDs INTO VECTOR HERE; 
    int vectorLength = empIds.size(); 
    std::cout << "enter id"; 
    std::cin >> code; 
    for (int i = 0; i < vectorLength; i++) { 
     if (empIds[i] == code) { 
      std::cout << "enter id again"; 
      std::cin >> code; 
     } else { 
      empIds.push_back(code); 
     } 
    } 
} 
+0

這應該是一個評論..... – thermite 2014-09-30 20:18:32

+0

存儲在一個'vector'中,並且迭代的效率會低於map或hashtable。 – venki421 2014-09-30 20:32:23

+0

我是新的c + +我不知道如何使用向量可以exprio更多 – Jals 2014-09-30 20:52:12

0

一開始,像下面應該工作。

map <string, bool> seen; 

bool isUniqueId(string id) 
{ 
    return seen[id]; 
} 

void addId(string id) 
{ 
    seen[id] = true; 
} 

main(),當用戶輸入一個字符串id,使用isUniqueId(id)以確保其獨一無二的,如果其獨特的,叫addId(id)

編輯:(在從OP請求)

你的變換碼可能看起來像以下,使用圖之後。

// Global map, defaults to false 
map <string, bool> seen; // seen map to store if an id is seen already or not. 
void uniqueId()  
{ 
    bool good = true; // set up a good flag to check if id is good or not. 
    int numEmployees = 0; // Count to store number of employees with unique ids so far 
    string id; 

    cout<< "enter id\n"; 
    cin>> id; 

    while(good) 
    { 
     good = false; // Assume this is unique! 
     if(seen[id]) // Check if we already saw this id before 
     { 
      good = true; // Alas! We already have seen this id 
      cout<<"enter id again\n"; 
      continue; // If id already exists, ask for another id setting good = true; 
      // Note that the above continue is NOT required as loop will run again (good = true). 
      // Just for clarity sake. 
     } 
     else 
     { 
       // Voila, we have a new employee with unique id. 
       seen[id] = true; // Unique, mark as seen now 
       emp[numEmployees].id=code; // Note numEmployees here 
       numEmployees++; // Increment the count 
     } 
    } 
    getch(); 
} 

在while循環,你就已經成功得到了來自用戶的唯一ID結束,否則將繼續要求新id用戶。

+0

我是新的C++我不知道如何使用地圖你可以expolite更多 – Jals 2014-09-30 20:51:40

+0

請參閱我的編輯。如果有什麼不清楚,請告訴我。 – venki421 2014-09-30 21:04:31

0

有這麼多的事情錯誤的代碼,但也許它應該看起來更像是這樣的:

void uniqueid() { 
    int check=1; 
    string code; 
    string tempemp1; 
    cout<< "enter id"; 
    while(check) { 
     cin >> code; 
     check = 0; 
     for (int i = 0; i < num; ++i) { 
      if (emp[i].id.compare(code)==0) { 
       check = 1; 
       cout << "enter id again"; 
       break; 
      } 
     } 
     if (check==0) { 
      /* emp[i].id=code; */ 
     } 
    } 
    getch(); 
} 

int check=1;開始於如何意味着代碼需要重新輸入。

所以while(check)意味着雖然代碼不唯一繼續前進。

for循環會像以前一樣進行比較,但請注意慣用形式。

另一if (check==0)是外部for循環,這意味着未檢測到重複的,因此可以使用code。但是,我不確定code應該適用哪位員工,所以我剛剛註釋掉了代碼。

+0

它的工作!萬分感謝!!但它有一點問題,我不得不按兩次輸入到下一行,你可以給我任何建議 – Jals 2014-09-30 21:08:43

+0

將「\ n」添加到'cout'語句。喜歡這個。 'cout <<「輸入id \ n」;'和'cout <<「再次輸入id;''如果你想試試看,你可以看到我使用map的答案。 – venki421 2014-09-30 21:13:27

+0

我留在'getch()'這可能意味着按兩次輸入 – quamrana 2014-09-30 21:21:09

2

如果ID被輸入的順序並不重要,我會做這樣的事情(注:未經測試):

using EmpIds = std::set<std::string>; 

void addUniqueId(EmpIds& ids) 
{ 
    std::pair<EmpIds::iterator, bool> inserted; 
    const char* again = ""; 
    do { 
     std::cout << "enter id" << again; 
     again = " again"; 

     std::string id; 
     if (!(std::cin >> id)) 
      throw std::runtime_error("No more ids!"); 
     inserted = ids.insert(id); 
    } while (!inserted.second); 
} 
+0

如果我是你,當你不用時,我會使用['std :: unordered_set'](http://www.cplusplus.com/reference/unordered_set/unordered_set/)想要對數據進行排序,因爲它可能比['std :: set'](http://www.cplusplus.com/reference/set/set/)更快 – GingerPlusPlus 2014-10-01 05:43:20