You are here:

C++/C++ Programming and Problem Solving

Advertisement


Question
Hi,
 I need to write a simple program that puts each question into a function. It calls the function to solve the question. I need to set up a menu to call each function also. I'm using C++ Compiler.
1.
do
cout << "Enter 1,2,or 3:";
cin >> response;
} while(response < 1|| resonse > 3);

2.
cin >> ch;
while (cin)
{
cout << ch;
cin >> ch;
I'm really not sure how to get started so I would really appreciate your help if you can help me.

Answer
Dejuan,
Here is some code from one of my programs. This is the 'menu' function:

//Menu Function
void menu() {
cout << "\n\n***************************" << endl
    << "1 - Enter a new song" << endl
    << "2 - Display stats of a specific song" << endl
    << "3 - Display all entered songs" << endl
    << "4 - Exit program" << endl
    << "***************************" << endl
    << "Please enter an option: ";
}

As you can see, this is just a 'void' function. The easiest way to display a menu is to have a function like mine. It doesn't take any values as an input, and just displays the options. So in the main part of your program, you can call the function in a 'do-while' loop over and over. After the function is called, you ask the user for their input option. You then take the user's input and use that value in a switch statement:

//User input detemines function called
switch(choice){

case 0:
user_file();
break;
case 1:
new_song();
break;
case 2:
display_one_song();
break;
case 3:
display_all_songs();
break;
case 4:
run_program = 0; //stops program
break;
default:
cout << "That's not a choice. Choose again!" << endl;
break;
} //ends switch

The switch statement can be placed within a 'for', 'while', or 'do-while' loop, depending on how you want to write the code. I hope this helps you get started. If you need any clarification on this code, please let me know.

Best Of Luck,
Alan

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Alan F.

Expertise

Having problems with any basic or intermediate parts of C++? I can answer those questions for you using simple English. I have been taking C++ in school for a couple years, so I still remember what it was like to be a beginner (especially with C++, which is a difficult language)!

Experience

I have a C++ web page that discuses the basics of C++. The address is: http://www.afweb.net/complang/

©2016 About.com. All rights reserved.