+1 (315) 557-6473 

C++ Program to Create Menu Display Assignment Solution.


Instructions

Objective
Write a program to create menu display in c++.

Requirements and Specifications

Begin with a Visual Studio project file that has been set up correctly to work with both C++ and Python.
Remember to be sure you are working in Release mode, rather than Debug mode. Then add the CS210_Starter_CPP_Code and CS210_Starter_PY_Code files, linked in the Supporting Materials section, to their appropriate tabs within the project file so that C++ and Python will be able to effectively communicate with one another. After you have begun to code, you will also wish to access the CS210_Project_Three_Input_File, linked in the Supporting Materials section, to check the functionality and output of your work.
As you work, continue checking your code’s syntax to ensure your code will run. Note that when you compile your code, you will be able to tell if this is successful overall because it will produce an error message for any issues regarding syntax. Some common syntax errors are missing a semicolon, calling a function that does not exist, not closing an open bracket, or using double quotes and not closing them in a string, among others.
  1. Use C++ to develop a menu display that asks users what they would like to do. Include options for each of the three requirements outlined in the scenario and number them 1, 2, and 3. You should also include an option 4 to exit the program. All of your code needs to effectively validate user input.
  2. Create code to determine the number of times each individual item appears. Here you will be addressing the first requirement from the scenario to produce a list of all items purchased in a given day along with the number of times each item was purchased. Note that each grocery item is represented by a word in the input file. Reference the following to help guide how you can break down the coding work.
    1. Write C++ code for when a user selects option 1 from the menu. Select and apply a C++ function to call the appropriate Python function, which will display the number of times each item (or word) appears.
    2. Write Python code to calculate the frequency of every word that appears from the input file. It is recommended that you build off the code you have already been given for this work.
    3. Use Python to display the final result of items and their corresponding numeric value on the screen.
    4. Create code to determine the frequency of a specific item. Here you will be addressing the second requirement from the scenario to produce a number representing how many times a specific item was purchased in a given day. Remember an item is represented by a word and its frequency is the number of times that word appears in the input file. Reference the following to help guide how you can break down the coding work.
      1. Use C++ to validate user input for option 2 in the menu. Prompt a user to input the item, or word, they wish to look for. Write a C++ function to take the user’s input and pass it to Python.
      2. Write Python code to return the frequency of a specific word. It will be useful to build off the code you just wrote to address the first requirement. You can use the logic you wrote but modify it to return just one value; this should be a fairly simple change (about one line). Next, instead of displaying the result on the screen from Python, return a numeric value for the frequency of the specific word to C++.
      3. Write a C++ function to display the value returned from Python. Remember, this should be displayed on the screen in C++. We recommend reviewing the C++ functions that have already been provided to you for this work.
      4. Create code to graphically display a data file as a text-based histogram. Here you will be addressing the third requirement from the scenario: to produce a text-based histogram listing all items purchased in a given day, along with a representation of the number of times each item was purchased. Reference the following to help guide how you can break down the coding work:
        1. Use C++ to validate user input for option 3 in the menu. Then have C++ prompt Python to execute its relevant function.
        2. Write a Python function that reads an input file (CS210_Project_Three_Input_File, which is linked in the Supporting Materials section) and then creates a file, which contains the words and their frequencies. The file that you create should be named frequency.dat, which needs to be specified in your C++ code and in your Python code. Note that you may wish to refer to work you completed in a previous assignment where you practiced reading and writing to a file. Some of your code from that work may be useful to reuse or manipulate here. The frequency.dat file should include every item (represented by a word) paired with the number of times that item appears in the input file. For example, the file might read as follows:
          • Potatoes 4
          • Pumpkins 5
          • Onions 3

        Write C++ code to read the frequency.dat file and display a histogram. Loop through the newly created file and read the name and frequency on each row. Then print the name, followed by asterisks or another special character to represent the numeric amount. The number of asterisks should equal the frequency read from the file. For example, if the file includes 4 potatoes, 5 pumpkins, and 3 onions then your text-based histogram may appear as represented below. However, you can alter the appearance or color of the histogram in any way you choose.

        • Potatoes ****
        • Pumpkins *****
        • Onions ***
      5. Apply industry standard best practices such as in-line comments and appropriate naming conventions to enhance readability and maintainability. Remember that you must demonstrate industry standard best practices in all your code to ensure clarity, consistency, and efficiency. This includes the following:
        1. Using input validation and error handling to anticipate, detect, and respond to run-time and user errors (for example, make sure you have option 4 on your menu so users can exit the program)
        2. Inserting in-line comments to denote your changes and briefly describe the functionality of the code
        3. Using appropriate variable, parameter, and other naming conventions throughout your code
      6. Programming Languages Explanation
      7. Consider the coding work you have completed for the grocery-tracking program. You will now take the time to think more deeply regarding how you were able to combine two different programming languages, C++ and Python, to create a complete program. The following should be completed as a written explanation.
        1. Explain the benefits and drawbacks of using C++ in a coding project. Think about the user-focused portion of the grocery-tracking program you completed using C++. What control does this give you over the user interface? How does it allow you to use colors or formatting effectively?
        2. Explain the benefits and drawbacks of using Python in a coding project. Think about the analysis portions of the grocery-tracking program you completed using Python. How does Python allow you to deal with regular expressions? How is Python able to work through large amounts of data? What makes it efficient for this process?
        3. Discuss when two or more coding languages can effectively be combined in a project. Think about how C++ and Python’s different functions were able to support one another in the overall grocery-tracking program. How do the two function well together? What is another scenario where you may wish to use both? Then, consider what would happen if you added in a third language or switched Python or C++ for something else. In past courses, you have worked with c++ as a possible example. What could another language add that would be unique or interesting? Could it help you do something more effectively or efficiently in the grocery-tracking program?
      What to Submit
      To complete C++ assignment, you must submit the following:
      Grocery-Tracking Program
      Submit your completed work as a ZIP file, including all Visual Studio project files that are required to run the program. Reference the Visual Studio Export Tutorial, linked in the Supporting Materials section, for guidance on how to download the necessary ZIP folder.
      Programming Languages Explanation
      Submit your completed explanation as a one-page Microsoft Word document with 12-point Times New Roman font, double spacing, and one-inch margins. Any sources should be cited according to APA style.

      Source Code

      #include

      #include

      #include

      #include

      #include

      using namespace std;

      /*

      Description:

      To call this function, simply pass the function name in Python that you wish to call.

      Example:

      callProcedure("printsomething");

      Output:

      Python will print on the screen: Hello from python!

      Return:

      None

      */

      void CallProcedure(string pName)

      {

      char* procname = new char[pName.length() + 1];

      std::strcpy(procname, pName.c_str());

      Py_Initialize();

      PyObject* my_module = PyImport_ImportModule("PythonCode");

      PyErr_Print();

      PyObject* my_function = PyObject_GetAttrString(my_module, procname);

      PyObject* my_result = PyObject_CallObject(my_function, NULL);

      Py_Finalize();

      delete[] procname;

      }

      /*

      Description:

      To call this function, pass the name of the Python functino you wish to call and the string parameter you want to send

      Example:

      int x = callIntFunc("PrintMe","Test");

      Output:

      Python will print on the screen:

      You sent me: Test

      Return:

      100 is returned to the C++

      */

      int callIntFunc(string proc, string param)

      {

      char* procname = new char[proc.length() + 1];

      std::strcpy(procname, proc.c_str());

      char* paramval = new char[param.length() + 1];

      std::strcpy(paramval, param.c_str());

      PyObject* pName, * pModule, * pDict, * pFunc, * pValue = nullptr, * presult = nullptr;

      // Initialize the Python Interpreter

      py_Initialize();

      // Build the name object

      pName = PyUnicode_FromString((char*)"PythonCode");

      // Load the module object

      pModule = PyImport_Import(pName);

      // pDict is a borrowed reference

      pDict = PyModule_GetDict(pModule);

      // pFunc is also a borrowed reference

      pFunc = PyDict_GetItemString(pDict, procname);

      if (PyCallable_Check(pFunc))

      {

      pValue = Py_BuildValue("(z)", paramval);

      PyErr_Print();

      presult = PyObject_CallObject(pFunc, pValue);

      PyErr_Print();

      }

      else

      {

      PyErr_Print();

      }

      //printf("Result is %d\n", _PyLong_AsInt(presult));

      Py_DECREF(pValue);

      // Clean up

      Py_DECREF(pModule);

      Py_DECREF(pName);

      // Finish the Python Interpreter

      Py_Finalize();

      // clean

      delete[] procname;

      delete[] paramval;

      return _PyLong_AsInt(presult);

      }

      /*

      Description:

      To call this function, pass the name of the Python functino you wish to call and the string parameter you want to send

      Example:

      int x = callIntFunc("doublevalue",5);

      Return:

      25 is returned to the C++

      */

      int callIntFunc(string proc, int param)

      {

      char* procname = new char[proc.length() + 1];

      std::strcpy(procname, proc.c_str());

      PyObject* pName, * pModule, * pDict, * pFunc, * pValue = nullptr, * presult = nullptr;

      // Initialize the Python Interpreter

      Py_Initialize();

      // Build the name object

      pName = PyUnicode_FromString((char*)"PythonCode");

      // Load the module object

      pModule = PyImport_Import(pName);

      // pDict is a borrowed reference

      pDict = PyModule_GetDict(pModule);

      // pFunc is also a borrowed reference

      pFunc = PyDict_GetItemString(pDict, procname);

      if (PyCallable_Check(pFunc))

      {

      pValue = Py_BuildValue("(i)", param);

      PyErr_Print();

      presult = PyObject_CallObject(pFunc, pValue);

      PyErr_Print();

      }

      else

      {

      PyErr_Print();

      }

      //printf("Result is %d\n", _PyLong_AsInt(presult));

      Py_DECREF(pValue);

      // Clean up

      Py_DECREF(pModule);

      Py_DECREF(pName);

      // Finish the Python Interpreter

      Py_Finalize();

      // clean

      delete[] procname;

      return _PyLong_AsInt(presult);

      }

      /*

      Description:

      To call this function, pass the name of the Python functino you wish to call and the two string parameters you want to send

      Example:

      int x = callIntFunc("PrintMe","Test1","Test2");

      */

      int callIntFunc(string proc, string param1, string param2)

      {

      char* procname = new char[proc.length() + 1];

      std::strcpy(procname, proc.c_str());

      char* paramval1 = new char[param1.length() + 1];

      std::strcpy(paramval1, param1.c_str());

      char* paramval2 = new char[param2.length() + 1];

      std::strcpy(paramval2, param2.c_str());

      PyObject* pName, * pModule, * pDict, * pFunc, * pValue1 = nullptr, * pValue2 = nullptr, * presult = nullptr;

      // Initialize the Python Interpreter

      Py_Initialize();

      // Build the name object

      pName = PyUnicode_FromString((char*)"PythonCode");

      // Load the module object

      pModule = PyImport_Import(pName);

      // pDict is a borrowed reference

      pDict = PyModule_GetDict(pModule);

      // pFunc is also a borrowed reference

      pFunc = PyDict_GetItemString(pDict, procname);

      if (PyCallable_Check(pFunc))

      {

      pValue1 = Py_BuildValue("(z)", paramval1);

      pValue2 = Py_BuildValue("(z)", paramval2);

      PyObject* args = PyTuple_New(2);

      PyTuple_SetItem(args, 0, pValue1);

      PyTuple_SetItem(args, 1, pValue2);

      PyErr_Print();

      presult = PyObject_CallObject(pFunc, args);

      PyErr_Print();

      }

      else

      {

      PyErr_Print();

      }

      //printf("Result is %d\n", _PyLong_AsInt(presult));

      Py_DECREF(pValue1);

      Py_DECREF(pValue2);

      // Clean up

      Py_DECREF(pModule);

      Py_DECREF(pName);

      // Finish the Python Interpreter

      Py_Finalize();

      // clean

      delete[] procname;

      delete[] paramval1;

      delete[] paramval2;

      return _PyLong_AsInt(presult);

      }

      /*

      * Function to display all menu options and get user option

      * */

      int menu() {

      // Display options

      cout << "1) Determine the number of times each individual items appears" << endl;

      cout << "2) Determine the number of times an specific item was purchased" << endl;

      cout << "3) Determine the number of times an specific item was purchased" << endl;

      int option;

      while (true) {

      cout << "Enter option: ";

      cin >> option;

      if (option >= 1 && option <= 3) {

      break;

      }

      else {

      cout << "Invalid option." << endl;

      }

      }

      return option;

      }

      void main()

      {

      /*CallProcedure("printsomething");

      cout << callIntFunc("PrintMe", "House") << endl;

      cout << callIntFunc("SquareValue", 2);*/

      // Begin program

      bool fileRead = false; // if this variable is false, it means that the

      // file has not been read and then the other options cannot be executed

      int option;

      string filename, itemname;

      while (true) {

      option = menu();

      if (option == 1) // read file

      {

      // Ask for file

      cout << "Enter name of file: ";

      cin >> filename;

      // Now, call the python function passing this filename as argument

      callIntFunc("ParseItemsFile", filename);

      fileRead = true;

      }

      else if (option == 2) // search for specific item

      {

      if (!fileRead) {

      cout << "Please use option 1 to read the items data first." << endl;

      }

      else {

      cout << "Enter name of item: ";

      cin >> itemname;

      // Call function

      int count = callIntFunc("SearchItem", filename, itemname);

      cout << "There are " << count << " of " << itemname << endl;

      }

      }

      else if (option == 3) {

      // Call the function that creates the .dat file and the histogram

      if(!fileRead) {

      cout << "Please use option 1 to read the items data first." << endl;

      }

      else {

      // Call function

      int result = callIntFunc("CreateHistogram", filename);

      }

      }

      cin.ignore();

      }