i try to make a structure so that you insert n numbers from the keyboard but i want to show a error message if the number is <0, >m or is equal to a previous number...i tried but still cant solve it pls help me ...
this is my try:
for(i=1;i<=n;i++)
{
cout<<" No "<<i<<" -> ";
cin>>nc[i];
for(j=1;j<=n;j++)
if(nc[i]==nc[j]&&i!=j)
{
cout<<" Error. The number need to be different"<<endl;
cout<<" No "<<i<<" -> ";
cin>>nc[i];
}
while(nc[i]<=0||nc[i]>m)
{
while(nc[i]<0)
{
cout<<" Error. The number needs to be positive"<<endl;
cout<<" No "<<i<<" -> ";
cin>>nc[i];
}
while(nc[i]>m)
{
cout<<" Error. The number needs to be below "<<m<<endl;
cout<<" No "<<i<<" -> ";
cin>>nc[i];
}
}
}
#include <stdio.h>
#include <iostream>
constint maxNumberArray = 5;
constint lowerLimit = 0;
constint upperLimit = 10;
int main(int argc, char *argv[])
{
// Initialise the values to an out of bounds value.
// This means that the check to see if the value is already there
// will not fail because of ‘junk’ in the array
int numberArray[maxNumberArray] = { -1};
// Rather than put a value directly into the array create a temp
// validate the temp and finaly add it to the array when happy and
// move on to the next input value
int currentInput = 0;
do // This is the main loop it will stop after it has maxNumberArray
{ // of valid intput
std::cout << "Please enter a number:" << std::endl;
std::cout << "numberArray[" << currentInput << "] = ";
//
int temp = 0;
std::cin >> temp;
// This just cleans up the input stream if there was an error
std::cin.clear();
std::cin.ignore();
//
//
if( (temp >= lowerLimit) && (temp <= upperLimit))
{
// input is within the bound set
// just need to check if it is already in the array.
bool isInArray = false;
for (int i = 0; i < maxNumberArray; i++)
{
if(numberArray[i] == temp) isInArray = true;
}
if(isInArray)
{
std::cout << "Error: input is not unique" << std::endl;
}
else
{
// we have a valid input
// add it to the array any increment the array index
numberArray[currentInput] = temp;
++currentInput;
}
}
else // input is out of boudns so display an error meeasage
{ // and let the program loop around again for another input
std::cout << "Error: input out of bounds" << std::endl;
std::cout << "the number should be between " << lowerLimit << " and " << upperLimit << std::endl;
}
}while (currentInput != maxNumberArray);
return 0;
}