Array Help

I need help,, with this
I want to Stop Users from Entering the Same ID Number for 2 or more people...

1
2
3
4
5
6
7
8
9
 int n,k,key;    
    
    for (n = 0; n < N_STUDENT;n++)
    {
       	while((cout<<"Enter Student Number: ")&&(!(cin>>record[n].student_number)||record[n].student_number<0)){
            cout<<"Invalid Input!"<<endl;
            cin.clear();
            cin.ignore(1000,'\n'); 
		}


Last edited on
A set could be useful here. When the user enters an ID, try to insert it to the set.
You can tell wether or not the operation was successful by looking at its return value.

http://cplusplus.com/reference/stl/set/
http://cplusplus.com/reference/stl/set/insert/

EDIT: Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <set>
using namespace std;

int main()
{
    set<int> numbers;
    int n;

    while (true)
    {
        cout << "enter a number (0 to quit) -> ";
        cin >> n;

        if (n==0) break;

        if (numbers.insert(n).second==false)
            cout << "you've already entered ";
        else
            cout << "this is the first time you enter ";

        cout << n << endl;
    }

    cout << "\n(hit enter to quit...)";
    cin.get();
    cin.get();

    return 0;
}
Last edited on
Yes or you could avoid cin.clear and cin.ignore() by entering the student ID into a vector or array and then looping through the container to find a duplicate and then accept or reject.
i have them going into an array
Topic archived. No new replies allowed.