程序要求用户输入非重复的数字c ++ [英] Program requires user to enter non duplicate numbers c++
问题描述
我在一个程序中编写了一个函数,用于输入唯一的数字,但它不工作。 for循环出了问题。
I have written a function in a program for entering a unique number but its not working. Something is wrong with the for loop.
我需要验证该员工ID是唯一的。
我创建了一个名为employee的结构,emp.id是employee id。当用户输入id时,它不应该匹配用户之前可能已经输入的先前Id。这只是主程序的一个函数,它验证employee id是唯一的。
I need to validate that employee id is unique. I have made a structure named employee and "emp.id" is employee id. When the user inputs an id, it should not match previous Id's which user might have entered before. This is just a function of the main program, which validates that employee id is unique.
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();
}
推荐答案
输入无关紧要,我会做类似于(注意:未测试的):
If the order that the ids are entered doesn't matter, I would do something like (note: untested):
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);
}
这篇关于程序要求用户输入非重复的数字c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!