Question (not sure about title)

Hello,

I'm working on a generator and I've put the code (see code below) at every item and I come on a final ammount of 18k lines of code. I want to short this, since I need to make a lot of code more. Is there some way to put this code above or down the file and make a command look back at it?


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
if (commerceS1 == 1) {
                                     cout << "M240\n";
                                     cout << "Choose your currency: 1. Silver, 2. Silver10oz, 3. Gold and 4. Gold10oz\n";
                                     cin>> commerceS2;
                                          if (commerceS2 == 1) {
                                                         cout << "You chose Silver, how much?\n";
                                                         cin>> commerceS3;
                                                            if (commerceS4 == 1) {
                                                                           cout << "You chose 1 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }
                                                            if (commerceS3 == 2) {
                                                                           cout << "You chose 2 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }
                                                            if (commerceS3 == 3) {
                                                                           cout << "You chose 3 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }  
                                                            if (commerceS3 == 4) {
                                                                           cout << "You chose 4 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           } 
                                                            if (commerceS3 == 5) {
                                                                           cout << "You chose 5 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }
                                                            if (commerceS3 == 6) {
                                                                           cout << "You chose 6 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }
                                                            if (commerceS3 == 7) {
                                                                           cout << "You chose 7 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }  
                                                            if (commerceS3 == 8) {
                                                                           cout << "You chose 8 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }
                                                            if (commerceS3 == 9) {
                                                                           cout << "You chose 9 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }  
                                                            if (commerceS3 > 9) {
                                                                           cout << "Invalid number, programm closes\n";
                                                                           }                                                           
                                                         }
Put it in a function and call the function?
How can I do this easy?

Like:

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
52
53
54
int silver ()
{
                                       cout << "Choose your currency: 1. Silver, 2. Silver10oz, 3. Gold and 4. Gold10oz\n";
                                     cin>> commerceS2;
                                          if (commerceS2 == 1) {
                                                         cout << "You chose Silver, how much?\n";
                                                         cin>> commerceS3;
                                                            if (commerceS4 == 1) {
                                                                           cout << "You chose 1 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }
                                                            if (commerceS3 == 2) {
                                                                           cout << "You chose 2 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }
                                                            if (commerceS3 == 3) {
                                                                           cout << "You chose 3 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }  
                                                            if (commerceS3 == 4) {
                                                                           cout << "You chose 4 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           } 
                                                            if (commerceS3 == 5) {
                                                                           cout << "You chose 5 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }
                                                            if (commerceS3 == 6) {
                                                                           cout << "You chose 6 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }
                                                            if (commerceS3 == 7) {
                                                                           cout << "You chose 7 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }  
                                                            if (commerceS3 == 8) {
                                                                           cout << "You chose 8 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }
                                                            if (commerceS3 == 9) {
                                                                           cout << "You chose 9 Silver\n";
                                                                           cout << "Your code is: ..\n";
                                                                           }  
                                                            if (commerceS3 > 9) {
                                                                           cout << "Invalid number, programm closes\n";
                                                                           }                                                           
                                                         }

}

int main(int argc, char *argv[]) 
{

}


and then?
Last edited on
http://www.cplusplus.com/forum/beginner/130939/

but anyways.. why not just do

1
2
cout << "You chose " << commerceS3 << " Silver\n";
cout << "Your code is: ..\n";
I don't want the code to be to long. This is my current code with ur advice:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
                      cout << "You've chosen: Light Machine Guns, choose your gun --(Page 1/3):\n";
                      cout << "1. M240\n";
                      cout << "2. M249\n";
                      cout << "3. M8_SAW\n";
                      cout << "4. MG36\n";
                      cout << "5. Mk_48\n";
                      cin>> commerceS1;
                      if (commerceS1 == 1) {
                                     cout << "M240\n";
                                     cout << "Choose your currency: 1. Silver, 2. Silver10oz, 3. Gold and 4. Gold10oz\n";
                                     cin>> commerceS2;
                                     if (commerceS2 == 1) {
                                     cout << "You chose Silver, how much?\n";
                                     cin>> commerceS3;
                                     silver ();
                                                    }
                                     if (commerceS2 == 2) {
                                     cout << "You chose Silver10oz, how much?\n";
                                     cin>> commerceS4;
                                     silver10 ();
                                     }

You should consider using arrays and strings.
I'll take a look at arrays and strings. Thank you very much!
So, I'm back. I'm stuck on an another part now. I worked out 1 part of it, but I can't make it generate correctly. Like; I want this line to generate:

('["NAME",3]',10,'[AMMOUNTBUY,"CURRENCYBUY",1]','[AMMOUNTSELL,"CURRENCYSELL",1]',0,-,'trade_weapons').

Name = as above: M240, M249 etc.
AMMOUNTBUY = 1 till 9
CURRENCYBUY = Silver,silver10oz,gold,gold10oz
AMMOUNTSELL = 1 till 9
CURRENCYSELL = Silver,silver10oz,gold,gold10oz

How do I make that code generate? With arrays or ..? Or do I use functions?
Last edited on
Can anyone tell me this? I'm stuck on it for some time and I tried everything, but I can't get it to work..


Thanks,

Darryl
Show us how you have tried.
Also I want to have multiple blocks falling at a time, how would I do that?
I have no idea what this is supposed to be or mean.

To create an array you would do something like:
1
2
3
4
5
6
7
std::size_t const totalWeapons = 2;
std::string const weapons[totalWeapons] = { "M240", "M249" };

//now to access the first element(offset of 0)
weapons[0]
//access second element(offset 1)
weapons[1]


I was mainly referring to the currency part. You have it repeated several times.

So you could either do an array or just 1 string like:
std::string const currency = "Silver, Silver 10 oz...";
I tried:
1. Using if () {} statement for every weapon, but then I end up with a load of lines.
2. Using Array's int weapon1[5] = {1,2,3,4,5}, but it didn't work since it only took the first weapon "M240".
3. Using the 'switch', but same as the first one, I end up with a load of lines.

I'll see if I can make this work, since I'm really a noob. I'm sorry if I annoy you with my lack of information, but I'm not sure which information to give you, since all of my code is almost the same except the weapon names.

Last edited on
Got it to work, but now I run against an another problem.

How do I make it work depending on choice (0,1,2,3,4)? Is there an another way then the:
if () {} statements or/and switch?

Thanks for the code, though Giblit!
Last edited on
Honestly what I think you should do is incorporate some object oriented programming(OOP). It can be something very simple like a plain old data (POD) struct like:
1
2
3
4
5
6
7
8
9
10
enum Currency { silver = 10, silver10oz = 100, gold = 50, gold10oz = 500 }; //what ever they are worth
struct Weapon
{
    std::string name;
    unsigned price;
}

//now you can use the silver, silver10oz, gold, or gold10oz to pay for the weapon (they must add up to the price)

you then want to put the weapons in a container of some sort. Possibly a vector


you could also use a std::map like std::map<std::string, unsigned> Weapons = { {"M240", 1250}, {"M249", 3210"} };
Hmm, I will add that as option 2, since I don't understand anything of that piece of code except "std::string name;".

So, no other way then the old if () {} and switch?
I like to have an uniform design when I code. So something like:

1
2
3
4
5
6
7
8
9
//output the list of weapons

//get user input on the desired weapon

//display the price of the weapon

//ask the user how they want to pay to get the sum (can be a few silver few gold ect..or all silver) if it is the correct sum accept payment, if it is more then give them a refund of lower currency

//give them weapon 


Instead of rewriting the code several times with only minimal changes (the price, name of weapon, ect..)
Last edited on
What do you mean with that, giblit? I really don't understand you, but that's my side, I'm sorry.
Something like this where it doesn't matter what the weapon is. Keep in mind though it is still missing a bunch of stuff I tried to just create a simple example for you.
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
52
53
54
55
#include <iostream>
#include <map>
#include <string>

int main()
{
	enum Currency{ Silver = 10, Gold = 50, Silver10oz = 100, Gold10oz = 500 };
	std::map<std::string, unsigned> weapons
	{
		{"M240", 1250u},
		{"M249", 3150u}
	};
	
	std::cout << "Weapons:" << std::endl;
	for(auto const &it: weapons) //std::pair<std::string, unsigned> instead of auto
	{
		std::cout << it.first << " = $" << it.second << std::endl;
	}
	std::cout << "Which weapon would you like to purcahse? ";
	std::string weaponToPurchase = "";
	std::cin >> weaponToPurchase;
	
	auto selectedWeapon = weapons.find(weaponToPurchase); //auto can be replaced with
	//std::map<std::string, unsigned>::iterator
	
	if(selectedWeapon != weapons.end()) //it was found
	{
		std::cout << "Please enter the number of silver, silver 10 oz, gold, and gold 10 oz"
		<< " you are going to use for the purchase(separated with spaces): ";
		unsigned silver = 0u;
		unsigned silver10oz = 0u;
		unsigned gold = 0u;
		unsigned gold10oz = 0u;
		std::cin >> silver >> silver10oz >> gold >> gold10oz;
		
		unsigned paid = Silver * silver + silver10oz * Silver10oz + 
		Gold * gold + Gold10oz * gold10oz;
		int owed = selectedWeapon->second - paid;
		std::cout << "You gave me $" << paid;
		if(owed > 0)
		{
			std::cout << " and still owe $" << owed << std::endl; //charage them again
		}
		else if(owed < 0)
		{
			std::cout << " which is an extra $" << -owed << std::endl;
		}
		
	}
	else
	{
		std::cout << "That weapon was not found" << std::endl;
	}
	return 0;
}

http://ideone.com/AiBrpH
That might help out a lot, thanks!
Topic archived. No new replies allowed.