Reputation: 11
So the question is to store id of 11 students and store their runs in a cricket match the runs must be greater then 10 and the id number of students must be unique i am only facing problem in creating a condition that when a user inputs same value the programm should tell that the value exists already and add a different value.l
#include <iostream>
using namespace std;
void main() {
int id[5];
int run[5];
int a, b, c, d, e, f;
for (int i = 0; i < 5; i++) {
for (int j = i; j < 5; j++) {
cout << "Enter id" << i << endl;
cin >> id[i];
if (id[i] == id[j]) {
cout << "same id exists";
cin >> id[i];
}
cout << "Enter runs of student \n";
do {
cout << "Runs must be greater or equal to 10 \n";
cin >> run[i];
} while (run[i] < 10);
}
}
system("pause");
Upvotes: 0
Views: 216
Reputation: 27577
You want to store the ids in a std::set
(or std::unordered_set
):
#include <unordered_set>
std::unordered_set<id_type> ids;
// . . .
if (auto [iter, is_inserted] = ids.insert(new_id); !is_inserted) {
// take care of repeated id
}
And as 463035818-is-not-a-number points out if you need a student associated with each id
use a std::map
(or std::unordered_map
):
#include <unordered_map>
std::unordered_map<id_type, stdent_type> students;
// . . .
if (auto [iter, is_inserted] = students.emplace(new_id, new_student); !is_inserted) {
// take care of repeated id
}
Upvotes: 2