I can't figure out why it won't print the last if statement
/*Use a single-subscripted array to solve the following problem. Read in 20 numbers,
each of which is between 10 and 100, inclusive. As each number is read, print it
only if it is not a duplicate of a number already read. Provide for the “worse case”
in which all 20 numbers are different. Use the smallest possible array to solve this
problem.*/
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int slot[20], m;
for (int i = 0; i <= 19; i++)
{
int k = i + 1;
printf("\nEnter number %d between 10 and 100: ", k);
cin >>slot[i];
while (slot[i] < 10)
{
printf("Did not choose a number between 10 and 100");
printf("\nEnter number %d between 10 and 100: ", k);
cin >>slot[i];
}
while (slot[i] > 100)
{
printf("Did not choose a number between 10 and 100");
printf("\nEnter number %d between 10 and 100: ", k);
cin >>slot[i];
}
for (m = 0; m < i; m++)
{
if (slot[m] == slot[i])
{
break;
}
}
if (slot[m] != slot[i])
{
printf("%i", slot[i]);
}
}
}
If I were you I would write a class that dealt with the storage of the numbers as you read them. Have a function that returns a Boolean to check if you already have the number i.e. its a duplicate. If the function returns false then store it and then print it.
constint max_numbers = 20;
class CNumbers
{
public:
CNumbers()
: m_index(0)
{
}
~CNumbers()
{
}
bool IsAlreadyHeld(int number)
{
bool ret(false);
// need to write the code that check if number
// is already held in the array 'm_numbers'
return ret;
}
void Add(int number)
{
// neeed to write code that adds number at
// the next index and then increments 'm_index'
// ready for next number to be added
// also make sure we don't go beyold the array
// boundary
}
void Print()
{
// need to write the code to iterate through
// array and output to stdout
}
private:
int m_numbers[max_numbers];
int m_index;
};