Reputation: 31
I've been trying to make my c++ program check for duplicates in an array, as I need this for a program I am writing. But, it doesn't work, and I think I know how what the issue is but I have no idea how to fix it.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
int asknum(int ask);
int check(int checking);
bool error = false;
int test [10] = {};
int i = 0;
int gen = 0;
int main() {
//int gen = 0;
for (i=0; i<10; i++) {
gen = asknum(gen);
}
for (int b=0; b<10; b++) {
cout << test [b] << endl;
}
return 0;
}
int asknum(int ask) {
ask = 0;
gen = 0;
int nouse;
cout << "Please enter a value: ";
cin >> ask;
error = 0;
nouse = check(ask);
}
int check(int checking) {
int n = 0;
for (n=0; n<10; n++) {
int temp = test [n];
if (temp == checking) {
cout << "Value is the same. Enter another value.\n";
error = 1;
gen = asknum(gen);
}
}
if (n == 10) {
if (error == 0) {
gen = 0;
test [i] = checking;
}
}
}
What's wrong with my code? The first time I wrote the code it worked perfectly. But when I noticed a flaw, which was that if you constantly entered a present number, it would stop asking for a new number, I decided to move the code into functions, but now it doesn't work properly! Here is the output:
Please enter a number: 1
Please enter a number: 2
Please enter a number: 3
Please enter a number: 3
That number already is used. Please type in another value.
Please enter a number: 4
Please enter a number: 5
Please enter a number: 6
Please enter a number: 7
Please enter a number: 8
Please enter a number: 9
Please enter a number: 10
1
2
3
3
5
6
7
8
9
10
I think there's something wrong with the communication. Please help!
Thanks!
Upvotes: 0
Views: 3172
Reputation: 260
Okey, so what you want to do is ask the user for an integer and if that integer does not exist in your container you want to add it to the container.
In c++ I would suggest avoiding c-style arrays and instead look to use one of the c++ containers instead, for example std::set
.
So what does the reference pages tell us about std::set
?
Sets are containers that store unique elements following a specific order.
Sounds just like what we want!
The big advantage is that std::set
has built in functionality to for example, find an existing element based on the value of the element.
An example of how this could look:
void getNumberFromUser(set<int>& numbers);
int main()
{
set<int> numbers;
for (int i = 0; i < 10; ++i) {
getNumberFromUser(numbers);
}
for (auto& value : numbers) {
cout << value << endl;
}
return 0;
}
void getNumberFromUser(set<int>& numbers)
{
int value;
cout << "Please enter a value: ";
cin >> value;
auto result = numbers.insert(value);
while (!result.second()) {
cout << "That number is already used. Please type in another value.\n";
cout << "Please enter a value: ";
cin >> value;
result = numbers.insert(value);
}
}
Since set::insert
return a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the equivalent element already in the set. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent element already existed.
all we have to do is check if the second value of the returned pair is true or false to know if the value was already in use or not.
I hope this helped a bit and don't be afraid to look at the c++ reference pages, they are not as scary as you might think!
Upvotes: 1
Reputation: 637
After adding the duplicate 3 the program continues from the gen = asknum(gen);
with error being assigned equal to 0 in function asknum(int ask)
. That is why it passes the if condition to assign ith element in the array. Remove error=0
from asknum(int ask)
and put it after if condition in check
:
EDIT : checked it above solution was still giving same output however error value was same due to calling of asknum within asknum. to make it work change for loop for cin in main to:
for (i=0; i<10; i++) {
gen = asknum(gen);
if(error==1)
{
i--;
error=0;
}
}
And check to:
int check(int checking) {
int n = 0;
for (n=0; n<10; n++) {
int temp = test [n];
if (temp == checking) {
cout << "Value is the same. Enter another value.\n";
error = 1;
//fflush(stdout);
//cin.clear();
//gen = asknum(gen);
}
}
//cout<<error<<"\n";
if (error == 0) {
gen = 0;
test [i] = checking;
cout<<checking<<" "<<i<<"\n";
}
//error=0;
}
Please check if this helps.
Upvotes: 1