how to create convertable variables?

I dont know how to make my program to write 1gold and 0 silver when I input 1000 value of silver coins? Help me please !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Gold and silver!
#include <iostream>
using namespace std;

int main ()
{
	int silver (0);
	int gold (0);
	cout <<"Write number of silver coins! ";
	cin >> silver; cout <<endl;
	cout <<"You have gold " <<gold <<" and silver " <<silver <<endl;
	cin.get (); cin.get ();
	return 0;
}
The key is to think about how you did the maths yourself.

I expect your internal algorithm ran something like this:

Is amount of silver bigger than 1000?
If no, then it's just silver. Not gold.
If yes, then there is a gold value.
How many thousands of silver are there?
There is ONE thousand, with none left over.
So I have ONE gold, and NO silver.


Then just take each piece and write the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Is amount of silver bigger than 1000?
if (silver>1000)
{
  // If yes, then there is a gold value.
  // How many thousands of silver are there?
  int numberOfThousandsOfSilver = silver/1000;
  gold = numberOfThousandsOfSilver;
  // How much silver left over?
  silver = silver -  (numberOfThousandsOfSilver * 1000);
}
else
{
  // If no, then it's just silver. Not gold.
}
  cout <<"You have gold " <<gold <<" and silver " <<silver <<endl;
}


Last edited on
You have to assign the values yourself. For example, after reading the number of silver coins:
1
2
gold = silver/1000;
silver = silver%1000;
Topic archived. No new replies allowed.