0

Hey so this is an assignment and what we have to do is:

Design a class that can be used to represent types of food. A type of food is classified as basic or prepared. Basic foods are further classified as Dairy, Meat, Fruit, Vegetable, or Grain. The services provided by the class include the ability to enter data for new food, change data for new food, display existing data for new food. Create a program that will allow the user to enter data for four food items and display the entered data. Provide a menu for selection of services and allow user input for program rerun and/or termination.

Here is my code:

#include <iostream>
#include <string>

using namespace std;

class Food{
private:
        string foodType;
        string foodName;
        string specType;
public:
        Food ();
        Food (string s1, string s2, string s3);
        void setFoodType(string, string, string);
        void dispFood();
};
Food::Food (){
    foodType = "None";
    foodName = "None";
    specType = "None";
}
Food::Food (string fT, string fN, string sT){
    foodType = fT;
    foodName = fN;
    specType = sT;
}

void Food::setFoodType(string fT, string fN, string sT){
    bool isCorrect = true, isCorrect2 = true;
    cout<<"Enter Food Name: ";
    cin>>foodName;

    while (isCorrect){
    cout<<"Enter Food Type: ";
    cin>>foodType;      
    
     if ((foodType == "basic") || (foodType == "Basic")){
                
      while (isCorrect2){
         cout<<"Enter Basic Food Classification: ";
         cin>>specType;
         cout<<endl;
         
        if ((specType!="Dairy") || (specType!="dairy") || (specType!="Meat") || (specType!="meat") || 
             (specType!="Grain") || (specType!="grain") || (specType!="Vegetable") || (specType!="vegetable") || 
             (specType!="Fruit") || (specType!="fruit")){
             isCorrect2 = true;
             continue;
         }
                    
         else if ((specType=="Dairy") || (specType=="dairy") || (specType=="Meat") || (specType=="meat") || 
             (specType=="Grain") || (specType=="grain") || (specType=="Vegetable") || (specType=="vegetable") || 
             (specType=="Fruit") || (specType=="fruit"))
            isCorrect2 = false;
            continue;
      }
      isCorrect = false;
     }
    
     else if ((foodType == "prepared") || (foodType == "Prepared")){
        specType = "None Applicable";
        cout<<endl;
        isCorrect = false;
     }
    
     else{
        foodType = "None";
        isCorrect = true;
        continue;
     }
    }        
}

void Food::dispFood(){
    cout<<"Food Name: "<<foodName<<endl;
    cout<<"Food Type: "<<foodType<<endl;
    cout<<"Food Class: "<<specType<<endl;
    cout<<endl<<endl;
}

int main(){
    int popList = 0;
    string _fT, _fN, _sT;
    Food foodList[10];
    
    cout<<"Enter Number of Food Items to Enter: ";
    cin>>popList;
    cout<<endl;
    
    for (int i = 0; i<popList; i++){
        foodList[i].setFoodType(_fT, _fN, _sT);
    }
    
    for (int j = 0; j<popList; j++){
        foodList[j].dispFood();
    }
     
    system ("pause");
    return 0;
}

So I haven't done the menus and other miscellaneous things, I just first formulated the basic parts of the program. My problem here is whenever I go to the 'Enter specific food type: ' question it loops infinitely. Please help. Thank you.

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by ticktock
0

I just solved! Now, I feel sorry for wasting posting space here.

EDIT:
I changed the part of my code where it asks for what specific food type to this:

//snippet
while (isCorrect){
    cout<<"Enter Food Type: ";
    cin>>foodType;      
    
     if ((foodType == "basic") || (foodType == "Basic")){
                
      while (isCorrect2){
         cout<<"Enter Basic Food Classification: ";
         cin>>specType;
         cout<<endl;
                    
         if ((specType=="Dairy") || (specType=="dairy") || (specType=="Meat") || (specType=="meat") || 
             (specType=="Grain") || (specType=="grain") || (specType=="Vegetable") || (specType=="vegetable") || 
             (specType=="Fruit") || (specType=="fruit"))
            isCorrect2 = false;
         
         else {
             isCorrect2=true;
             continue;
              }
      }
      
      isCorrect = false;
     }

Edited by ticktock: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.