how to share a int with a function

i want to share the selection in main with the down function and get de comfirmation of my selection including the 2 cout (in main and selection)

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
  #include <iostream>
#include <string>
using namespace std;


void choice(int listchoice[],int size)
{


cout << "Please select an item number" << endl;



for(int x=0;x<size;x++)
{
  cout << listchoice[x] << endl;
}

}

int selection(int number); //function prototype



int main()
{


int listchoiceEdit[24]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
 
 choice(listchoiceEdit,24);
 
 int selection,number; //function is called
 
 cin >> selection;
 
if (selection<1 || selection>24)
{
    cout << "make a choice between 1 and 24" << endl;
    cin >> selection;
}
else
{
cout << "your choice is " << selection << endl;
}

number = selection;
}

int selection(int number)
{
      cout << "your choice is still" << number << endl;  
}

item()
{
    
}
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
#include <iostream>

int choice( int min_value, int max_value )
{
    std::cout << "Please select an item number [" << min_value << ',' << max_value << "]: " ;

    int item ;
    if( std::cin >> item ) // if the user entered a number
    {
        // return it if it is within the valid range
        if( item >= min_value && item <= max_value ) return item ;

        // not a valid choice; inform the user about the error
        else std::cout << "invalid choice. value is out of range. " ;
    }

    else // the user entered a non-numberic value eg. abcd
    {
        std::cout << "non numeric input. please enter a number. " ;
        std::cin.clear() ; // clear the failed state of the stream
        std::cin.ignore( 1000, '\n' ) ; // and throw the bad input away
    }

    std::cout << "try again\n" ;
    return choice( min_value, max_value ) ; // input failed, ask the user again for the choice
}

int main()
{
    const int selection = choice( 1, 24 ) ;
    std::cout << "your selection is " << selection << '\n' ;
}
Topic archived. No new replies allowed.