loop structure

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];
}
}
}
closed account (z05DSL3A)
Have a look at the following code.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <iostream>

const int maxNumberArray = 5; 

const int lowerLimit = 0;
const int 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;
}
Last edited on
thanks a lot...but i was wondering if u could explain the idea of your program...if u dont mind :D and not working :( error on std::
Last edited on
closed account (z05DSL3A)
I have added comment, dose that help?

I am at work, but having a break from coding...well...errm...sort of. :o)

error on std::

You may not need std::, I only use it because I don't use using namespace std;
Last edited on
yes that helps a lot thank you so much...i got the idea now :D
Topic archived. No new replies allowed.