2017-10-12 80 views
-2

我有一个程序,提示用户输入一个值。用户输入的每个值都放入一个向量'other'中,这个值只用于验证。如果输入了重复值,则用户将得到一个提示,直到他们输入唯一值。防止重复进入矢量? C++

我面临的主要问题是,由于某些原因,在运行代码并打印出矢量结果时,似乎有重复条目。谁能告诉我为什么?

请参阅以下我的代码:

// prompt to continue 
cout << "Would you like to continue? [Y]es, [N]o: "; 
cin >> toContinue; 

while (toContinue == 'Y') 
{ 
    bool isDuplicate = 0; 

    // prompt for product no. 
    cout << "Please enter product number: "; 
    cin >> productB; 

    // Validation check for duplicate entries 
    for (size_t i = 0; i < other.size(); i++) 
    { 
     if (productB == other[i]) 
      isDuplicate = 1; 

     while (isDuplicate == 1) 
     { 
      cout << "You have already entered this product number!" << endl; 
      cout << "Please enter correct product number: "; 
      cin >> productB; 

      if (productB != other[i]) 
       other.push_back(productB); 

      isDuplicate = 0; 
     } 
    } 
    // prompt to continue 
    cout << "Would you like to continue? [Y]es, [N]o: "; 
    cin >> toContinue; 
} 
+7

改为使用'std :: set'。 – user0042

+0

你可能想用调试器来浏览你的程序。提示:当我输入'3 4 3'时会发生什么? – Rakete1111

+5

检测到重复项目后,您要求用户重新输入。您可以根据之前输入的一个号码检查此新号码,但不能对所有先前输入的号码进行检查。所以说,用户再次输入“1”,然后输入“2”,然后输入“1”。您检测到重复并要求重新输入。他们输入'2',你只检查'2!= 1',并愉快地将第二个'2'加到向量中。 –

回答

1

虽然它通常使用std::set独特的元素,如果函数必须返回一个矢量由于某些原因,我用这样的方法:

std::set<int> my_set; 

my_set.insert(1); 
my_set.insert(2); 
my_set.insert(1); 

// ... insert more 

std::vector<int> my_vector(my_set.size()); 
std::copy(my_set.begin(), my_set.end(), my_vector.begin()); 

assert(my_vector.size()==2); 

请注意,矢量my_vector将被排序。

1

一旦你输入一个副本,你让用户重新输入一个数字;那么你只检查新输入的号码是否与之前输入的重复相同;但是您不检查用户是否输入了不同但仍然重复的值。

通常,您将用户输入与程序逻辑混合;分拆这使得代码更具可读性并且不易出错。参见,例如,下面的片段示出了如何人们可以分离这些顾虑:

bool isContained(const vector<int> &v, int value) { 
    // your code to check for duplicates goes here 
} 

int main() { 

    ... 

    while (toContinue == 'Y') { 

     // prompt for product no. 
     cout << "Please enter product number: "; 
     cin >> productB; 

     if (isContained(other, productB)) { 
     cout << "You have already entered this product number!" << endl; 
     } 
     else { 
     other.push_back(productB); 
     } 

     // prompt to continue 
     cout << "Would you like to continue? [Y]es, [N]o: "; 
     cin >> toContinue; 
    } 
} 

而且一般暗示:使用适当的数据结构还可以帮助避免码的不必要的行;例如,避免重复的容器是std::set

1

您可以通过将逻辑组件分解为更小的函数来帮助自己。

我在这里完成的大部分工作都已经整理完毕,但请注意contains函数的封装。

#include <vector> 
#include <iostream> 
#include <algorithm> 

using namespace std; 

bool contains(std::vector<int> const& vals, int val) 
{ 
    return std::count(std::begin(vals), std::end(vals), val) != 0; 
} 

bool shouldContinue() 
{ 
    char toContinue; 
    cout << "Would you like to continue? [Y]es, [N]o: "; 
    cin >> toContinue; 
    return toContinue == 'Y'; 
} 

int getProduct(bool again) 
{ 
    int productB; 
    if (again) 
    { 
     cout << "You have already entered this product number!" << endl; 
    } 
    cout << "Please enter correct product number: "; 
    cin >> productB; 
    return productB; 
} 

void printProducts(std::vector<int> const& vals) 
{ 
    std::cout << "You have selected:"; 
    const char* sep = " "; 
    for(int p : vals) 
    { 
     std::cout << sep << p; 
     sep = ", "; 
    } 
    std::cout << std::endl; 
} 


int main() 
{ 
    std::vector<int> other; 

    while (shouldContinue()) 
    { 
     int productB = getProduct(false); 
     while(contains(other, productB)) 
     { 
      productB = getProduct(true); 
     } 
     other.push_back(productB); 
    } 

    printProducts(other); 
}