So you're problem is that you don't know how to put this into multiple functions? If so, then there's an easy solution to this.
Your first step would be deciding how you want to split the various segments of code into functions. I would recommend making a function for your initial cout statements first(if you prefer to keep them in main, go for it. I would recommend this because if you need to display the statements again for any reason, you can just call the function. Then, I would recommend a function for the last of the initial cout statements that prompts the user to decide on a menu selection and the cin statement following. Afterwards, you can either create a series of if statements, or a switch statement to check for the value of choice. The result will call the another function that I would recommend creating that consists of your for loops. Before I go any further, let me show you how to make another function, since you'll need to know that.
1 2 3 4 5 6 7 8 9 10 11
|
void functionA(); /*this is a forward declaration, also known as a prototype.
It is required to write another function below main. Alternatively, you can just
write your function above main, but it's easier to read the first way I mentioned. */
int main(){
funtionA(); /*this is how you call a function. It basically just runs the code within
the function */
return 0;}
void functionA(){
cout<<"This is a sentence printed from another function."<<endl;}
| |
Now, for some of the functions I would recommend making, you would need to pass in parameters to use them outside of main, assuming you made the variables needed in main. The syntax for that is as follows:
1 2 3 4 5 6 7 8 9 10 11
|
void functionA(int a); /*technically, this can have any name for the
declaration and function parameter. I wouldn't recommend doing so
because it can be confusing */
int main(){
int a = 1;
functionA(a);
return 0;}
void functionA(int a){
cout<<"The number that was passed is "<<a<<endl;}
| |
If you didn't understand something about this, or need me to elaborate, please let me know and I will do so.