Sections with valued variables that can be multiplied and output?

Basically I have been searching for a way to create "sections" with variables that have numerical values that can be multiplied and then output.

Here is how I have tried to write out as nicely as possible what I am trying to do:

Let's say I am trying to craft an item several times.
I have an input that says how many of the item I need.
I want to output the resources needed to craft the item the input amount of times.
However, I want it to be easy to swap out "recipes" and be able to do the same thing for another item.

Let's say my input number "input" is "10" and my resources for item "randomitem" are
variable value
wood 10
crystal 4
emerald 2
iron 5

To make it more clear, it would do this:
1
2
3
4
5
6
7
8
input = 10;
randomitem {
wood = 10;
crystal = 4;
emerald = 2;
iron = 5;
}
std::cout << randomitem*input;
and it would output:
wood 100
crystal 40
emerald 20
iron 50

If it were possible to make the recipes a separate file and then pick a particular one, multiply the values inside the file by the input, and then output the variable names with their numerical values that would pretty much be what I would like to do.

I hope this isn't too complicated and actually makes sense.

Thanks in advance for any help,
Zorael
This must help!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//----------------------header files---------------------------------------------
#include <iostream>
#include <cstdlib>
//----------------------namespaces-----------------------------------------------
using namespace std;
//------------globals/functions'n'classes-prototypes-----------------------------
int multiplier(int);

const int wood=10;
const int crystal=4;
const int emerald=2;
const int iron = 5;

//-------------------------code--------------------------------------------------

int main()
{
    int input;

    cout<<"Enter the number of crafts you want to make: ";
    cin>>input;
    if(input==0)
    {
        cout<<"\n\nThen go bed!!! nothing to give you!!!";
        cin.ignore();
        cin.get();
        exit(0);
    }

    if(input>1) multiplier(input);

    else
    {
        cout<<"Amount of items you need: "<<endl
        <<"\tWood: "<<wood<<endl
        <<"\tCrystal: "<<crystal<<endl
        <<"\tEmerald: "<<emerald<<endl
        <<"\tIron: "<<iron<<endl;
    }
    cin.ignore();
    cin.get();
}

int multiplier(int mult)
{
    cout<<"Amount of items you need: "<<endl
        <<"\tWood: "<<wood*mult<<endl
        <<"\tCrystal: "<<crystal*mult<<endl
        <<"\tEmerald: "<<emerald*mult<<endl
        <<"\tIron: "<<iron*mult<<endl;
}
Last edited on
here's a sample code .
let me know if it helped.

#include<iostream>
using namespace std;
//a macro to define dummy funcs
#define DF(INT) cout<<#INT<<"="<<INT
int main()
{
int wood=15;
int crystal=4;
int emerald=2;
int iron=5;
int Arr[4]={wood,crystal,emerald,iron}
int input=10;
for(int i=0;i<4;i++)
{
DF(Arr[i]*input);
cout<<"\n";
}
return 0;
}
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <utility>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>

struct item
{
    using resource = std::pair< std::string, int > ;

    item( std::initializer_list<resource> res ) : resources(res) {}

    const std::vector<resource> resources ;
};

std::string operator* ( const item& it, unsigned int n )
{
   std::ostringstream stm ;
   for( const auto& r : it.resources ) stm << r.first << ' ' << r.second * n << '\n' ;
   return stm.str() ;
}

inline std::string operator* ( unsigned int n, const item& it ) { return it * n ; }

int main()
{
    item random_item = { { "wood", 10 }, { "crystal", 4 }, { "emerald", 2 }, { "iron", 5 } } ;
    std::cout << random_item * 10 << '\n' ;

    item white_russian = { { "vodka", 2 }, { "Tia Maria", 1 }, { "half and half", 1 }  } ;
    std::cout << white_russian * 6 << '\n' ;
}

Output:
wood 100
crystal 40
emerald 20
iron 50

vodka 12
Tia Maria 6
half and half 6

I really like how JLBorges did it but it gives me a lot of errors upon compilation of just what he wrote:
randomitem.cpp:9: error: expected nested-name-specifier before "resource"
randomitem.cpp:9: error: using-declaration for non-member at class scope
randomitem.cpp:9: error: expected `;' before '=' token
randomitem.cpp:9: error: expected unqualified-id before '=' token
randomitem.cpp:11: error: expected `)' before '<' token
randomitem.cpp:13: error: `resource' was not declared in this scope
randomitem.cpp:13: error: template argument 1 is invalid
randomitem.cpp:13: error: template argument 2 is invalid
randomitem.cpp:13: error: ISO C++ forbids declaration of `resources' with no type
randomitem.cpp: In function `std::string operator*(const item&, unsigned int)':
randomitem.cpp:19: error: expected primary-expression before "const"
randomitem.cpp:19: error: expected `;' before "const"
randomitem.cpp:20: error: expected primary-expression before "return"
randomitem.cpp:20: error: expected `;' before "return"
randomitem.cpp:20: error: expected primary-expression before "return"
randomitem.cpp:20: error: expected `)' before "return"
randomitem.cpp: In function `int main()':
randomitem.cpp:27: error: brace-enclosed initializer used to initialize `const int'
randomitem.cpp:27: error: too many initializers for `item'
randomitem.cpp:30: error: brace-enclosed initializer used to initialize `const int'
randomitem.cpp:30: error: too many initializers for `item'
I'm not sure how to fix them, I fiddled around a little but I'm still rather new at this.

@extreme program mer
the program outupts Arr[i]*input instead of the words themselves and I can't fix that.

@Aceix
I like what you did but it'll be difficult or at least time consuming if I want to implement multiple recipes. :P
> I'm not sure how to fix them ...

Compile with: -std=c++11

If that gives errors, make thse changes
1
2
    // using resource = std::pair< std::string, int > ; // line 9
    typedef std::pair< std::string, int > resource ;

1
2
3
   // for( const auto& r : it.resources ) stm << r.first << ' ' << r.second * n << '\n' ; // line 19
   for( std::size_t i = 0 ; i < it.resources.size() ; ++i )
        stm << it.resources[i].first << ' ' << it.resources[i].second * n << '\n' ;

And compile with -std=c++0x
After a lot of trouble to get the the point where I could use -std=c++11 (I had a very old version of gcc), I was able to compile and receive the correct output.

Thank you very much, it all works now.
@JLBorges or anyone who knows

If I prompt a string and the user inputs one of my recipes such as "random_item" or "white_russian", can I get the program to output the value of that pre existing recipe multiplied by a number?

I know I can input it into a string by typing:
1
2
3
4
std::string itemname ;
std::cout << "Prompt itemname; " ;
std::cin >> itemname ;
std::cout << itemname ;
Which would output whatever was input by the user, but I cannot multiply the string and also I am unsure if I can get the program to know that it is referring to the previously defined item "random_item" or "white_russian" when the user inputs those phrases.
Last edited on
> I was able to compile and receive the correct output.

If you haven't done so already, before moving ahead, you should make sure that you understand the present code.

initializer lists: http://www.stroustrup.com/C++11FAQ.html#init-list
type deduction: http://www.stroustrup.com/C++11FAQ.html#auto
range based loop: http://www.stroustrup.com/C++11FAQ.html#for
type alases: http://www.stroustrup.com/C++11FAQ.html#template-alias

Then read up of std::map<> http://www.dreamincode.net/forums/topic/57446-stl-maps/

1
2
3
4
5
6
7
8
9
10
11
struct item
{
    // using resource = std::pair< std::string, int > ;
    typedef std::pair< std::string, int > resource ;

    item( std::initializer_list<resource> res ) : resources(res) {}

    /* const */ std::vector<resource> resources ;
};

std::map< std::string, item > item_map ; // maps name to item 
Topic archived. No new replies allowed.