Question

I need help in my C++ code regarding outputting the enums in string chars. I created...

I need help in my C++ code regarding outputting the enums in string chars. I created a switch statement for the enums in order to output words instead of their respective enumerated values, but an error came up regarding a "cout" operator on my "Type of Item" line in my ranged-based for loop near the bottom of the code. I tried casting "i.type" to both "i.GroceryItem::Section::type" and "i.Section::type", but neither worked. Simply put, if a user inputs 1 as their "choice" for the type of item when the program runs, I want to output that value as "produce", not as 0 according to how my enum was constructed. My C++ code is below:

#include <iostream>
#include <vector>
using namespace std;

struct Location {
   int aisle;
   int section;
   int shelf;
};

struct GroceryItem {
   enum class Section { produce, dairy, meat, canned_goods, frozen, bakery, drinks };
   Section type;
   float price;
   int quantity;
   string name;
   Location location;
};

int main() {
   vector<GroceryItem>store;
   char decision{ 'y' };
   while (!(decision == 'N' || decision == 'n')) {
       cout << "Do you have a grocery item to add to your list? (y)es or (n)o" << endl;
       cin >> decision;

       if (decision == 'Y' || decision == 'y') {
           store.push_back(GroceryItem());

           cout << "What is the name of your item?" << endl;
           cin >> store.back().name;

           cout << "What is the price of your item" << endl;
           cin >> store.back().price;

           cout << "How many of that item do you have?" << endl;
           cin >> store.back().quantity;

           cout << "What aisle, section, and shelf is this item located at?" << endl;
           cin >> store.back().location.aisle >> store.back().location.section >> store.back().location.shelf;

           cout << "What type of food is this?" << endl;
           cout << "\n1. produce" << "\n2. dairy" << "\n3. meat" << "\n4. canned goods" << "\n5. frozen"
               << "\n6. bakery" << "\n7. drinks" << endl;
           int choice;
           cin >> choice;

           if (choice == 1) {
               store.back().type = GroceryItem::Section::produce;
           }
           else if (choice == 2) {
               store.back().type = GroceryItem::Section::dairy;
           }
           else if (choice == 3) {
               store.back().type = GroceryItem::Section::meat;
           }
           else if (choice == 4) {
               store.back().type = GroceryItem::Section::canned_goods;
           }
           else if (choice == 5) {
               store.back().type = GroceryItem::Section::frozen;
           }
           else if (choice == 6) {
               store.back().type = GroceryItem::Section::bakery;
           }
           else if (choice == 7) {
               store.back().type = GroceryItem::Section::drinks;
            }
          

            enum class Section { produce, dairy, meat, canned_goods, frozen, bakery, drinks };
           Section type;
           switch (type) {
           case Section::produce:
               cout << "Produce" << endl;
               break;

           case Section::dairy:
               cout << "Dairy" << endl;
               break;

           case Section::meat:
               cout << "Meat" << endl;
               break;

           case Section::canned_goods:
               cout << "Canned Goods" << endl;
               break;

           case Section::frozen:
               cout << "Frozen" << endl;
               break;

           case Section::bakery:
               cout << "Bakery" << endl;
               break;

           default:
               cout << "Drinks" << endl;
           }
       }
   }
       for (auto i: store){
       cout << "Item name: " << i.name << endl;
       cout << "Price of item: " << i.price << endl;
       cout << "Quantity of item: " << i.quantity << endl;
       cout << "Type of item: " << i.type << endl; /* Here is where the error shows up*/
       cout << "Aisle number: " << i.location.aisle << endl;
       cout << "Section number: " << i.location.section << endl;
       cout << "Shelf number: " << i.location.shelf << endl;
       cout << endl;
   }

   return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. I just implemented a method called to_string that accepts a Section object as parameter and returns the string representation of that enum. And then updated the line containing error to call this method instead. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

#include <iostream>

#include <vector>

using namespace std;

struct Location {

   int aisle;

   int section;

   int shelf;

};

struct GroceryItem {

   enum class Section { produce, dairy, meat, canned_goods, frozen, bakery, drinks };

   Section type;

   float price;

   int quantity;

   string name;

   Location location;

};

//method accepting a GroceryItem::Section object as argument, returns the

//type as a string.

string to_string(GroceryItem::Section type){

                switch (type) {

           case GroceryItem::Section::produce:

               return "Produce";

           case GroceryItem::Section::dairy:

               return "Dairy";

           case GroceryItem::Section::meat:

               return "Meat";

           case GroceryItem::Section::canned_goods:

               return "Canned Goods";

           case GroceryItem::Section::frozen:

               return "Frozen";

           case GroceryItem::Section::bakery:

               return "Bakery";

           default:

               return "Drinks";

           }

}

int main() {

   vector<GroceryItem>store;

   char decision{ 'y' };

   while (!(decision == 'N' || decision == 'n')) {

       cout << "Do you have a grocery item to add to your list? (y)es or (n)o" << endl;

       cin >> decision;

       if (decision == 'Y' || decision == 'y') {

           store.push_back(GroceryItem());

           cout << "What is the name of your item?" << endl;

           cin >> store.back().name;

           cout << "What is the price of your item" << endl;

           cin >> store.back().price;

           cout << "How many of that item do you have?" << endl;

           cin >> store.back().quantity;

           cout << "What aisle, section, and shelf is this item located at?" << endl;

           cin >> store.back().location.aisle >> store.back().location.section >> store.back().location.shelf;

           cout << "What type of food is this?" << endl;

           cout << "\n1. produce" << "\n2. dairy" << "\n3. meat" << "\n4. canned goods" << "\n5. frozen"

               << "\n6. bakery" << "\n7. drinks" << endl;

           int choice;

           cin >> choice;

           if (choice == 1) {

               store.back().type = GroceryItem::Section::produce;

           }

           else if (choice == 2) {

               store.back().type = GroceryItem::Section::dairy;

           }

           else if (choice == 3) {

               store.back().type = GroceryItem::Section::meat;

           }

           else if (choice == 4) {

               store.back().type = GroceryItem::Section::canned_goods;

           }

           else if (choice == 5) {

               store.back().type = GroceryItem::Section::frozen;

           }

           else if (choice == 6) {

               store.back().type = GroceryItem::Section::bakery;

           }

           else if (choice == 7) {

               store.back().type = GroceryItem::Section::drinks;

            }

       }

   }

       for (auto i: store){

       cout << "Item name: " << i.name << endl;

       cout << "Price of item: " << i.price << endl;

       cout << "Quantity of item: " << i.quantity << endl;

       //calling to_string() to convert i.type to a string and printing it.

       cout << "Type of item: " << to_string(i.type) << endl;

     cout << "Aisle number: " << i.location.aisle << endl;

       cout << "Section number: " << i.location.section << endl;

       cout << "Shelf number: " << i.location.shelf << endl;

       cout << endl;

   }

   return 0;

}

/*OUTPUT*/

Do you have a grocery item to add to your list? (y)es or (n)o

y

What is the name of your item?

hamburger

What is the price of your item

2

How many of that item do you have?

5

What aisle, section, and shelf is this item located at?

1 4 3

What type of food is this?

1. produce

2. dairy

3. meat

4. canned goods

5. frozen

6. bakery

7. drinks

3

Do you have a grocery item to add to your list? (y)es or (n)o

n

Item name: hamburger

Price of item: 2

Quantity of item: 5

Type of item: Meat

Aisle number: 1

Section number: 4

Shelf number: 3

Add a comment
Know the answer?
Add Answer to:
I need help in my C++ code regarding outputting the enums in string chars. I created...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • this code below is not the full code. I need help outputting both first name and...

    this code below is not the full code. I need help outputting both first name and last name. Can you please fix my code. I use getline but it says sometthing is wrong with my if statement our whatever cout<<" -=| ADDING STUDENT |=-" << endl; cout<<"Please enter the student’s name: "; cin>>s_name; if(g.add_std(s_name)) cout<<"\n"<<s_name<<" was successfully added to the gradebook!"; else cout<<"\nStudents cannot be added because the gradebook is full!"; break;

  • I need to be able to input first and last name. My code only outputs first...

    I need to be able to input first and last name. My code only outputs first when you put 2 names. This is not my full code. Any type of help wou;d be great! int main() { int main_choice; int grade_choice; int std_id; int new_grade; char g_name[100]; char s_name[100]; float a,b,c; gradebook g; do { cout <<endl; cout<< " -=| MAIN MENU |=-" << endl; cout<< "1. Add a student" << endl; cout << "2. Remove a student" << endl;...

  • C++ programming I need at least three test cases for the program and at least one...

    C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT