Search code examples
c++variablescompiler-errorsglobalundeclared-identifier

How to use and make a GLOBAL variable to use in different functions?


I know I need to avoid global variables as much as possible but somehow I need to use certain variables in other functions. This program is a recipe program. Adds, deletes and views recipes. Everything is working. Maybe not the best code but it works. The second part of the task is to make it so that: THE USER INPUTS THE AMOUNT OF PEOPLE HE NEEDS TO SERVE WITH THE RECIPE. THE PROGRAM MUST CHANGE THE RECIPE QUANTITIES TO SUIT HIS NEEDS.

My plan was to make a variable called "multiplier" which is multiplied by each quantity however I don't know whether there is a better way. Unfortunately since the variables I need are in "addRecipe" I cannot possibly access them. I require "int people" and "int quan[]" so I can divide the Number of servings he wants by the number of people the recipe is for. This will give me a multiplier which I then use to multiply each NUMERICAL value within the array. The array is then outputted differently without changing the recipe. This may not be the best plan and I have attempted it several times using parameters but I cannot seem to make it work. The code is below as is my attempts.

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


int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}
// Practice.cpp : Defines the entry point for the console application.
//

// 2a
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
using namespace std;


void deleteRecipe(){
    DIR *dp;
       struct dirent *ep;

       dp = opendir ("C:\\Users\\The\\Documents\\Visual Studio 2010\\Projects\\Recipe\\Recipe");
       if (dp != NULL)
         {
           while (ep = readdir (dp))
             puts (ep->d_name);
           (void) closedir (dp);
         }
       else
         perror ("Couldn't open the directory");
cout<< "Please input the file you would like to delete from the list above: "<<endl;
string dName;
cin>>dName;
remove(dName.c_str());
cout<<"The recipe has BEEN DELETED! " <<endl;

system("pause");

}

int addRecipe(){
    //PART B OF PROBLEM!
    int count = 1;
    string title;
    string item;
    int quan [100];
    string units;
    int people;
    cout<<"What is the title of your new recipe?"<<endl;
    cin.ignore();
    getline(cin, title);
    ofstream outFile(title+".txt", ios::out);
    cout<< "How many people will this recipe serve?"<<endl;
    cin>>people;
    cout<< title << " - Servings: " << people << " people" <<endl;
    cout<<" Please type your ingredients in the format as follows. Be to sure to input a '#'upon completion."<<endl;
    while (count<1000){
        cout<< "Enter Item: "<<endl;
        cin.ignore();
        getline(cin, item,'#');
        cout<< "Enter Quantity: "<<endl;
        cin>>quan[];
        cout<< ("Enter units")<<endl;
        getline(cin, units);
    count = count++;
    }
    cout<< "Thank you for inputting your recipe. To view your recipe, please do nothing."<<endl;
    outFile << title <<endl;
    outFile<< people << " People" <<endl;
    outFile<< item<<endl;
return 0;
}
int viewRecipe(){
     DIR *dp;
       struct dirent *ep;

       dp = opendir ("C:\\Users\\The\\Documents\\Visual Studio 2010\\Projects\\Recipe\\Recipe");
       if (dp != NULL)
         {
           while (ep = readdir (dp))
             puts (ep->d_name);
           (void) closedir (dp);
         }
       else
         perror ("Couldn't open the directory");
       cout<<"Please type in the name file you want to view from the list above: "<<endl;


       string fileName;
       string line;
       cin>> fileName;
       ifstream inData;
       inData.open(fileName.c_str());
       if (inData.is_open())
  {
    while ( getline (inData,line) )
    {
      cout << line << '\n';
    }
    inData.close();
    int multiplier = 0;
    char choice;
    int newAmount;
    cout<<"That was the original. If you would like to view the same recipe with a different amount of servings please press '#'."<<endl;
    // PART A OF PROBLEM!
    if (choice= '#'){
        cout<<"How many servings would you like to adapt the recipe to?"<<endl;
        cin>>newAmount;
        multiplier = newAmount/ addRecipe(0, );

    }

       }
return 0;
}


int menu(){
     int input = 0;
     cout<< "1: View Recipe" <<endl;
     cout<< "2: Add Recipe" <<endl;
     cout<< "3: Delete Recipe" <<endl;
     cout<< "Please select an option"<<endl;
     cin>>input;
     if (input == 1){
         viewRecipe();}
     if (input == 2){
         addRecipe();}
     if (input == 3){
         deleteRecipe();}


    return 0;
}


int main(){
    menu();


system("pause");
cin.get();
return 0;
}

My attempt which does not work:

int addRecipe(int people, int quan[]){
    int count = 1;
    string title;
    string item;
    int quan [100];
    string units;
    int people;
    cout<<"What is the title of your new recipe?"<<endl;
    cin.ignore();
    getline(cin, title);
    ofstream outFile(title+".txt", ios::out);
    cout<< "How many people will this recipe serve?"<<endl;
    cin>>people;
    cout<< title << " - Servings: " << people << " people" <<endl;
    cout<<" Please type your ingredients in the format as follows. Be to sure to input a '#'upon completion."<<endl;
    while (count<1000){
        cout<< "Enter Item: "<<endl;
        cin.ignore();
        getline(cin, item,'#');
        cout<< "Enter Quantity: "<<endl;
        cin>>quan[];
        cout<< ("Enter units")<<endl;
        getline(cin, units);
    count = count++;
    }
    cout<< "Thank you for inputting your recipe. To view your recipe, please do nothing."<<endl;
    outFile << title <<endl;
    outFile<< people << " People" <<endl;
    outFile<< item<<endl;
return 0;
}
int viewRecipe(int multiplier){
     DIR *dp;
       struct dirent *ep;

       dp = opendir ("C:\\Users\\The\\Documents\\Visual Studio 2010\\Projects\\Recipe\\Recipe");
       if (dp != NULL)
         {
           while (ep = readdir (dp))
             puts (ep->d_name);
           (void) closedir (dp);
         }
       else
         perror ("Couldn't open the directory");
       cout<<"Please type in the name file you want to view from the list above: "<<endl;


       string fileName;
       string line;
       cin>> fileName;
       ifstream inData;
       inData.open(fileName.c_str());
       if (inData.is_open())
  {
    while ( getline (inData,line) )
    {
      cout << line << '\n';
    }
    inData.close();
    int multiplier = 0;
    char choice;
    int newAmount;
    int x = addRecipe(people, void)
    cout<<"That was the original. If you would like to view the same recipe with a different amount of servings please press '#'."<<endl;
    if (choice= '#'){
        cout<<"How many servings would you like to adapt the recipe to?"<<endl;
        cin>>newAmount;
        multiplier = newAmount/ addRecipe(, );

    }

Solution

  • Here's an idea for you (because, as you said, you shouldn't use globals unless absolutely necessary, and in your case, it's not necessary):

    1.Create a class ingredient:

    class ingredient {
    public:
        string name;
        string unit;
        double quantity;
    
        ingredient(string n, string u, double q) :
            name(n), unit(u), quantity(q) { }
    }
    

    2.Create a class recipe:

    class recipe {
        vector<ingredient> the_recipe;
    
    public:
        recipe() : the_recipe() {}
    
        void addIngredient(const ingredient& i) {
            the_recipe.push_back(i);
        }
    
        void printProportions(const int servings) {
            printf("For %d servings, you need", servings);
            for(int i = 0; i < the_recipe.size(); ++i) {
                printf("%f %s of %s\n", the_recipe[i].quantity * servings, the_recipe[i].unit, the_recipe[i].name);
            }  
        }
    };
    

    What do you think?

    UPDATE

    3.Use it in your program:

    Here's an example of how you can use it (also note I've modified my struct above. In order to avoid possible confusion, I changed it to a class. They're both the same essentially in C++, with one minor difference). Anyway, here's an example:

    int main() {
        // create the ingredient objects
        ingredient macaroni("macaroni", "grams", 50), cheese("cheese", "grams", 20);
    
        // create a plain recipe
        recipe mac_n_cheese;
    
        // add the ingredients
        mac_n_cheese.addIngredient(macaroni);
        mac_n_cheese.addIngredient(cheese);
    
        // then finally print the proportions using printProportions
        mac_n_cheese.printProportions(2); // for 2 people
    
        return 0;
    }