Original Post
Hi! I joined today on this site as i saw it was good c++ tutorials and people getting much help. So I wanted to give it a shot. Ok, i'm making a text based game. But i have some problems with generating the price of the sold ores. Here is the code ( i will tell more info under it ) OBS: THIS IS JUST THE GENERATING CODE IN OTHER PROJECT NOT IN MY GAME
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int RandInt(int a,int b)
{
return a + rand() % (b - a + 1);
}
void main()
{
//Seed rand() with current time
srand(unsigned(time(NULL)));
//x will be between 2 and 10, inclusive
int x = RandInt(2,10);
int m = RandInt(5, 7);
int n = RandInt(11, 15);
int Ores = (n);
//cout << "Ores mined: " <<m << endl;
//cout << "Ores mined: " <<x << endl;
cout << "Ores mined: " <<n << endl;
cout << "You sell the ores and you get gold." << endl;
if(
cout << "Gold Given: " << endl;
system("pause");
}
Okey so you see that i have made a generating code right. And it work flawless. But now i want the ores to sell and that i get money(gold). So i want that this code. int Ores = (n); get the number that was generated for this line cout << "Ores mined: " <<n << endl; and take that * 0.50 So basically i this is how i want it. Game generates number. Number goes into cout << "Ores mined: " <<n << endl; and into int Ores = (n); And then int Ores = (n); takes that number and multiple's by 4 So we say i get a random number. Let's say 11. 11 * 4 = 44. And then i get 44 gold. Is it any way to do that? If you dont understand please tell me. ( PS I learned c++ around one week ago. ( I was training and training about 3 - 4 weeks. but i never learned this ) Thanks Here's my game code! PS: Some of the code is from a friend of mine. ( He did some of it but then i took over as i did not understand at that point how to code :) ) #include <iostream>
#include <string>
#include <ctime>
#include <stdlib.h>
using namespace std;
int main()
{
int potion;
int staff;
int gold = 100;
int armour;
int sword;
int pick;
int goldneeded;
int battlepick;
int fight;
int Dragon = 100;
int Dragonremain;
srand(time(NULL));
while(true)
{
srand(static_cast<unsigned int>(time(0)));
system("CLS");
cout << "You come apon a shop" << endl;
cout << "1) Potions 100 gold" << endl;
cout << "2) Staffs 150 gold " << endl;
cout << "3) Armour 500 gold " << endl;
cout << "4) Swords 250 gold " << endl;
cout << "5) Go mine Ores for cash" << endl;
cout << "6) Fight" << endl;
cin >> pick;
// HERE I STARTED TO CODE!
if(pick == 1)
{
if(gold<100)
{
cout << " You don't have enough gold to buy a Potion!\n " << endl;
goldneeded = gold = 100;
cout << " You need 100+ gold ! " << endl;
system("pause");
}
if(gold >= 100)
{
cout << "You just bought a potion for 100 gold." << endl;
gold = gold - 100;
cout << "Gold Left: " << gold << endl;
system("pause");
}
}
if(pick = 2)
{
if(gold < 150)
{
cout << "You dont have enough gold to buy a staff!" << endl;
goldneeded = gold = 150;
cout << "You need 150 gold to buy a staff!" << endl;
system("pause");
}
if(gold >= 150)
{
cout << "You bought a staff for 150 gold!" << endl;
gold = gold - 150;
cout << "Gold left: " << gold << endl;
system("pause");
}
if(pick = 3)
{
if(gold < 500)
{
cout << "You dont have enough gold to buy armour!" << endl;
goldneeded = gold = 500;
cout << "You need 500 gold to buy armour!" << endl;
system("pause");
}
if(gold >= 500)
{
cout << "You have bought armour for 500 gold!" << endl;
gold = gold - 500;
cout << "Gold left: " << gold << endl;
system("pause");
}
if(pick = 4)
{
if(gold < 250)
{
cout << "You dont have enough gold to buy a sword!" << endl;
goldneeded = gold = 500;
cout << "You need 500 gold to buy a sword!" << endl;
system("pause");
}
if(gold >= 250)
{
cout << "You bought a sword for 250 gold!" << endl;
gold = gold - 500;
cout << "Gold left: " << gold << endl;
system("pause");
}
if(pick = 5)
{ // HERE I HAVE NOT STARTED TO CODE AS I NEED TO MAKE A GENERATING CODE FOR THE ORES