Tax wrote: |
---|
What about writing the program in a one, big loop, as in the code above?
I've tried to do the same thing by creating menu and using functions (code below), but got some problems here. I want to go back to main menu after executing each Case of the switch. How? I've tried to go directly to main function, but it doesn't seem to work well. |
You should never attempt to call function
main()
. Although some compilers may accept this, in the ISO C++ standard it is not permitted, and some compilers will reject it as an error.
The answer is to simply use a loop, most often a
while
loop. You just need to consider some way to exit from the loop, for example as option 0 on the menu.
Also got a problem with second case, connected with initalizing of the array i'm using. I've initialized it in function HMANY, and want to use it in function SHOWNUMBERS. Is it possible? Or is it some local array or sth...? |
The array is indeed local to that function, and is no longer available after the function has ended.
One solution is to use a global variable for the array. Another is define it in function main() and to pass it as a parameter to the other functions.
Either way, there is a problem, the size of the array is dynamically chosen depending upon the user input. To deal with this, you'd need to use the
new
and
delete
operators. I'll illustrate this below.
Another way to deal with an array of variable size, and generally considered the better option, is to use a vector.
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <iostream>
using namespace std;
void hmany(int * &array, int howmany);
void shownumbers(int * array, int howmany);
//*******************************
int main()
{
int * array = 0;
int howmany = 0;
int choice;
do
{
cout << "My first menu, make your choice\n"
<< "1. Choose how many numbers\n"
<< "2. Show chosen numbers\n"
<< "3. Check size of array\n"
<< "4. Calculate sum of elements\n"
<< "5. Which element is the highest one?\n"
<< "0. Exit the program\n";
cin >> choice;
switch (choice)
{
case 1:
cout << "1. Choose how many numbers ";
cin >> howmany;
hmany(array, howmany);
break;
case 2:
cout << "2. Show chosen numbers\n";
shownumbers(array, howmany);
break;
case 3:
case 4:
case 5:
cout << "Not yet implemented.\n";
break;
case 0:
cout << "Exiting...\n";
break;
default:
cout << "invalid option. Please try again\n";
}
} while (choice != 0);
delete [] array;
cin.get();
system("pause");
}
//******************** case1 function
void hmany(int * &array, int howmany)
{
cout << howmany << " numbers chosen\n";
if (array != 0)
delete [] array;
array = new int[howmany];
for (int k=0; k<howmany; k++)
{
cout << "enter " << k <<". number of array ";
cin >> array[k];
}
}
//**************** case 2 function
void shownumbers(int * array, int howmany)
{
for (int m=0; m<howmany; m++)
cout << m << ". number= " << array[m] << " \n\n";
}
|