0

Sorry for the spaghetti code, I don't have much experience with C++ (or coding in general for that matter) but here is my code... Also first time posting on stack overflow so apologies for improper formatting. I have been scouring the internet for the last 4 hours. Please tell me this is a simple fix. I have tried using std::vector ingredients.

//Program_2.h

#pragma once
#include <vector>
#include <string>
#include <iostream>


namespace sandwich
{
    class SandwichMaker
    {
    public:
        vector<string> ingredients;//<-- causing a few errors
        int s = 0;
        int Control();
        void Menu(int &s, vector<string> &vec);
        void PrintSandwich(vector<string> &vec);


    private:


    };
}

//Program_2.cpp

#include "Program_2.h"

using namespace std;
using namespace sandwich;


int SandwichMaker::Control()
{
    SandwichMaker swm;
    //vector<string> ingredients;
    swm.Menu(swm.s, swm.ingredients);


    while (swm.s > 0)
    {
        swm.Menu(swm.s, swm.ingredients);
    }

    system("PAUSE");
    return 0;
}

void SandwichMaker::Menu(int &s, vector<string>& vec)
{
    SandwichMaker swm;
    cout << "Please choose what you would like to do and type the ingredient (1 and 2 only)\n (0 quit, 1 add ingredient, 2 remove ingredient, 3 make sandwich): ";
    cin >> s;

    string tempIngredient = "";

    switch (s)
    {
    case 1://add
        //cout << "Enter the ingredient you are going to add.\n";
        cin.ignore();
        getline(cin, tempIngredient);
        vec.push_back(tempIngredient);
        cout << " Ingredient has been added\n";
        break;
    case 2://remove
        if (!vec.empty())
            vec.pop_back();
        else
            cout << " Nothing has been added yet\n";
        break;
    case 3: //make the sandwich
        swm.PrintSandwich(vec);
        break;
    default: 
        break;
    }
}

void SandwichMaker::PrintSandwich(vector<string>& vec)
{
    cout << "Your sandwich contains: ";
    for (size_t i = 0; i < vec.size(); i++)
    {
        cout << i << ", " << vec[i] << endl;
    }
}

cant post an images soo link? https://i.stack.imgur.com/yYvS0.jpg of all the errors that are getting spit out because of vector.

  • 3
    `vector` -> `std::vector` – Yksisarvinen May 28 '20 at 21:08
  • Hmmm, the code is confused. Unless you've been told that you absolutely have to use classes and namespaces I would just forget about them and just write normal functions, until you've got a little more experience. Learn one thing at a time, not three. – john May 28 '20 at 21:10
  • I've learned the basic. This is part of a multi program program. I still need to learn how exactly classes work but I pretty much know basic functions. – prosvade1337 May 28 '20 at 21:26

1 Answers1

4

Your using namespace std comes after your inclusion of Program_2.h, so the SandwichMaker class is defined without using namespace std being in effect.

You thus need to write std::vector, not vector, and std::string, not string.

Note: Do not try to "fix" this by applying using namespace std earlier - that's a bad idea and poor programming. See:

Why is "using namespace std;" considered bad practice?

PS:

  • You're repeating the word "Sandwich" a whole lot of times: sandwich::SandwichMaker{}.PrintSandwich() - aren't these too many sandwiches?
  • I know a guy who's a sandwich maker and he never prints sandwiches.
  • If you have a class named WhateverMaker - you often don't really need the class at all, and could settle for a nice Whatever class, and some functions which return Whatevers. See also this.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • This did the trick. when I searched about using std::vector, I never thought about putting std in front of string. thank you so much. I have read about not putting the std namespace which is why it was never there. Also I will look into cutting down class names to be shorter. einpoklum you rock! – prosvade1337 May 28 '20 at 21:23