c++ function

closed account (oj2wbRfi)
I have these words ( burger, hamburger, chips, Cola, Sprite), I need to write a function to take print like these:

Chose your order from (0 - 4)

0: Burger
1: Hamburger
2: Chips
3: Cola
4: Sprite

Thanks, Your order is complete!

and your c++ issue is? Can you use std::cout to display the menu? Use std::cin to accept the order number?

Have you first designed the program so that you can code from the design?

What have you attempted so far?

Printing text is quite trivial: http://www.cplusplus.com/doc/tutorial/basic_io/
header only menu tool kit, menu_toolkit.hpp:
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
// "easy" menu utility

#ifndef MENU_TOOLKIT__
#define MENU_TOOLKIT__

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

namespace MTK // (Menu Tool Kit)
{
   inline void DisplayMenu(const std::vector<std::string>& stringArray)
   {
      std::cout << stringArray[0] << '\n';

      for (size_t i { 1 }; i < stringArray.size(); i++)
      {
         std::cout << " " << i << ") " << stringArray[i] << '\n';
      }
      std::cout << ": ";
   }

   inline unsigned short Menu(const std::vector<std::string>& stringArray)
   {
      unsigned short response { };
      size_t         size     { stringArray.size() - 1 };
      std::string    input    { };

      while (response < 1 || response > size)
      {
         DisplayMenu(stringArray);

         while (std::getline(std::cin, input))
         {
            std::istringstream is { input };

            if ((is >> response) && !(is >> input)) // re-using 'input' to test for extra stuff after the number
            {
               break; // done, we got what we wanted
            }
            std::cerr << "\n** INVALID CHOICE! **\n";
            DisplayMenu(stringArray);
         }

         if (response < 1 || response > size)
         {
            std::cerr << "\n** INVALID CHOICE! **\n";
         }
      }
      return response;
   }
}

#endif 

menu tool kit tester source:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <vector>

#include "menu_toolkit.hpp"

int main()
{
   std::vector<std::string> example = { "Menu choices are:",
                                        "Burger", "Hamburger",
                                        "Chips", "Fries",
                                        "Cola", "Sprite" };

   unsigned short choice = MTK::Menu(example);

   std::cout << "\nYou chose: " << example[choice] << '\n';
}
Menu choices are:
 1) Burger
 2) Hamburger
 3) Chips
 4) Fries
 5) Cola
 6) Sprite
: a

** INVALID CHOICE! **
Menu choices are:
 1) Burger
 2) Hamburger
 3) Chips
 4) Fries
 5) Cola
 6) Sprite
: 4a

** INVALID CHOICE! **
Menu choices are:
 1) Burger
 2) Hamburger
 3) Chips
 4) Fries
 5) Cola
 6) Sprite
: 4

You chose: Fries
Can you clarify that you just want to print this output only or also want to store it?
If you just want to print it out then you can use switch statement and I can provide the code if you want.
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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std::string_literals;

void DisplayMenu(const auto& stringArray) {
	if (stringArray.size() < 2)
		std::cout << "No menu to display!\n";
	else {
		for (size_t i {}; const auto& s : stringArray)
			if (i++)
				std::cout << " " << i - 1 << ") " << s << '\n';
			else
				std::cout << '\n' << s << '\n';

		std::cout << "Enter option: ";
	}
}

auto Menu(const auto& stringArray) {
	unsigned short	response {};
	const auto	size {stringArray.size() - 1};

	do {
		std::istringstream is;
		std::string input {};

		do {
			DisplayMenu(stringArray);

			std::getline(std::cin, input);
			is.clear();
			is.str(input);

		} while ((!(is >> response) || (is >> input)) && (std::cout << "Invalid input\n"));
	} while ((response < 1 || response > size) && (std::cout << "Invalid choice\n"));

	return response;
}

int main() {
	const std::vector example {"Menu choices are:"s,
				 "Burger"s, "Hamburger"s,
				 "Chips"s, "Fries"s,
				 "Cola"s, "Sprite"s};

	const auto choice {Menu(example)};

	std::cout << "\nYou chose: " << example[choice] << '\n';
}

Last edited on
Topic archived. No new replies allowed.