write a program that display a menu

write a program that display a menu
menu(chicken fish , prime rib ...)


#include "stdafx.h"
#include <iostream>


//declerations for 5 functions called from the menu
void Chicken();

void Fish();

void PrimeRib();

void Steak();

void Vegeterian();




using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

int selection = 0;
bool done= false;

while(!done)
{

// while statement that repeat selections for menu
std::cout << "1. Chicken " << std::endl;
std::cout << "2. Fish " << std::endl;
std::cout << "3. Prime Rib " << std::endl;
std::cout << "4. Steak " << std::endl;
std::cout << "5. Vegeterian " << std::endl;
std::cout << "6. Quit " << std::endl;

std::cout << "Select a dinner " << std::endl;

// get input from the user

std::cin >> selection;

// switch case statement used to handle selection

switch (selection)
{
case 1:
Chicken();
break;

case 2:
Fish();
break;

case 3:
PrimeRib();
break;
case 4:
Steak();
break;

case 5:
Vegeterian();
break;

case 6:
done = true;

break;

default:
std::cout << "Invalid entry" << std::endl;
break;

}

}

return 0;
}

void Chicken()
{


}
void Fish()
{


}
void PrimeRib()
{


}
void Steak()
{


}

void Vegeterian()
{


}

Array

// Midterm Q2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


#define NUMBER_OF_INPUTS 10
#define VERY_LARGE_NUMBER 10000000
#define VERY_SMALL_NUMBER -1000000

int findMinimum(int* Array);

int findMaximum(int* Array);`

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

int Array[NUMBER_OF_INPUTS];

// ask the user to enter 10 integers
for (int i = 0; i < NUMBER_OF_INPUTS; i++)
{
std::cout<< " Input #" << std::endl;
std::cin >> Array[i];


//call findMinimum
int minimum = findMinimum(Array);


//call finMaximum
int maximum = findMaximum(Array);

//dsplay the minimum and maximum
std::cout<<" Minimum is " << minimum << " Maximum is " << maximum << std::endl;




system("pause");
return 0;
}

int findMinimum(int* Array)

{

int minimum = VERY_LARGE_NUMBER;
for (int index = 0; index < NUMBER_OF_INPUTS; Index++ )

if (Array[index] < minimum)
{
minimum = Array[index]
}

return (minimum);
}

int findMaximum(int* Array)
{

int maximum = VERY_SMALL_NUMBER;
for (int index = 0; index < NUMBER_OF_INPUTS; index++)
if (Array[index] > maximum)
{
maximum = Array[index]
}

return (maximum);
}

Strings

// ReviewMidtermQ3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string.h>

using namespace std;

int main()
{
char string1[1000] = ""; // Have to initialize the strings with equal sign and quotation marks
char string2[1000] = "";
char string3[1000] = "";
char concatenatedString[3000] = "";

// ask the user to input 3 strings
cout << "Enter 3 strings" << endl;
cin.getline(string1, 1000);
cin.getline(string2, 1000);
cin.getline(string3, 1000);

// concatenate them into 1 string
strcat_s(concatenatedString, string1);
strcat_s(concatenatedString, string2);
strcat_s(concatenatedString, string3);

// display the concatenated string
cout << " The concatenated string is " << concatenatedString << endl;

// display the length of each input string
cout << "string1 length is " << strlen(string1) << endl;
cout << "string2 length is " << strlen(string2) << endl;
cout << "string3 length is " << strlen(string3) << endl;

return 0;
}



Last edited on
Hello procppgram.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Just loaded up your program to what is happening and ran into a problem with "_TCHAR". Not sure where that is defined, which header file, but I do know that it is not working for me right now.

"_tmain" may work for a windows program, maybe, but not for straight C++ program. Also I see no use of "agrc" or "argv" in your main file. If you are nor going to use them they are not need. a simple int main() will work.

Just a suggestion here. I would put the menu in its own function returning the users choice. Inside this function after displaying the menu get the users choice and verify that it is valid before ending say a do/while loop and returning a usable choice.

After that I am not sure what you are looking for here. You have posted code, but no real question/ Do not make people guess at what you need or you may be waiting for a long time.

Hope that helps,

Andy
Hello procppgram.

When I started working with the program I had to make some changes. This is what I came up with:

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
#include <iostream>

//using namespace std;  // <--- Best not to use, but if you feel that you have to it should be first;

//declerations for 5 functions called from the menu
void Chicken();
void Fish();
void PrimeRib();
void Steak();
void Vegeterian();
int MainMenu();


int main()
{

	int selection = 0;
	bool done = false;

	while (!done)
	{

		// while statement that repeat selections for menu
		selection = MainMenu();

        // rest of code here

        return 0;
}


And the Function I used is:
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
#include <iostream>

int MainMenu()
{
	int selection{};
	bool cont{ true };

	do
	{
		std::cout << "\n1. Chicken " << std::endl;
		std::cout << "2. Fish " << std::endl;
		std::cout << "3. Prime Rib " << std::endl;
		std::cout << "4. Steak " << std::endl;
		std::cout << "5. Vegeterian " << std::endl;
		std::cout << "6. Quit " << std::endl;
		std::cout << "Select a dinner ";

		// get input from the user
		std::cin >> selection;

		if (selection > 0 && selection < 7)
			cont = false;
		else
		{
			std::cout << "\n Invalid choice. Choose between 1 and 6." << std::endl;
		}
	} while (cont);

	return selection;
}

You could do the same in main, but I like this way because it is more flexable at times.

At this time I do not know what you want or need, so this is the best I can do for now.

Hope that helps,

Andy
Topic archived. No new replies allowed.