Hello!
I'm new to c++. For the last few days, I've been doing some very basic programs, such as a calculator, a tic tac toe game, a type save app and a stock exchange game.
I got the programs to work, I was able to run them without any problems. Last night I decided to alter the stock game so it could have a little more options.
Basically, game has 3 different stocks with different prices, you have a set amount of money, you buy or sell them and end the day. When the day ends, prices gets randomized and you start over again. Very simple program.
I wanted to add another stock to the game. To do this, I copied the exact code I did for others, replaced the stock and price values with proper ones. At first it seemed to work, but further testing showed that I couldn't sell any of the recently added stock. No matter what I do, it gives me "You cannot sell what you don't own" error, even if I remove that from that particular section.
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
#include <iostream>
using namespace std;
void main ()
{
int mX = 0;
int mY = 0;
int mZ = 0;
int mQ = 0; // new
int fX = 25;
int fY = 50;
int fZ = 75;
int fQ = 150; // new
int Total = 5000;
bool bValid(true);
cout << "Test app v0.20";
do {
cout << "\n\nToplam Para: " << Total << endl;
cout << "\n Stock name" << " Price " << " amount" << endl;
cout << "----------------------------------------" << endl;
cout << " Alanya Kagit (k) | " << fX << " | " << mX << "" << endl;
cout << " Estaskoop (t) | " << fY << " | " << mY << "" << endl;
cout << " Eczacibasi (z) | " << fZ << " | " << mZ << "" << endl;
cout << " Microsoft (m) | " << fQ << " | " << mQ << "" << endl; // new
cout << "----------------------------------------" << endl;
cout << "\nBuy (b)\nSell (s)\nEnd Day (e)\n\nSelect: " << endl;
char bse_select;
cin >> bse_select;
if (bse_select == 'b') {
do {
bValid = true;
char buy_select;
int buy_amount;
cout << "\nBuy (k/t/z/m): ";
cin >> buy_select;
if (buy_select == 'k' && bse_select == 'b') {
cout << "\nBuy Amount: ";
cin >> buy_amount;
if (buy_amount * fX > Total)
cout << "Insufficiant Funds.";
else {
mX = buy_amount + mX;
Total = Total - (fX * buy_amount);
}
}
else if (buy_select == 't' && bse_select == 'b') {
cout << "\nBuy Amount: ";
cin >> buy_amount;
if (buy_amount * fY > Total)
cout << "Insufficiant Funds.";
else {
mY = buy_amount + mY;
Total = Total - (fY * buy_amount);
}
}
else if (buy_select == 'z' && bse_select == 'b') {
cout << "\nBuy Amount: ";
cin >> buy_amount;
if (buy_amount * fZ > Total)
cout << "Insufficiant Funds.";
else {
mZ = buy_amount + mZ;
Total = Total - (fZ * buy_amount);
}
}
else if (buy_select == 'm' && bse_select == 'b') { // copy pasted this section from others
cout << "\nBuy Amount: ";
cin >> buy_amount;
if (buy_amount * fQ > Total)
cout << "Insufficiant Funds.";
else {
mQ = buy_amount + mQ;
Total = Total - (fQ * buy_amount);
}
}
else {
cout << "Invalid Selection.";
bValid = false;
}
} while (!bValid);
}
else if (bse_select == 's'){
do {
bValid = true;
int sell_amount;
char sell_select;
cout << "\nSat (k/t/z/m): ";
cin >> sell_select;
if (sell_select == 'k' && bse_select == 's') {
cout << "\nSell Amount: ";
cin >> sell_amount;
if (mX - sell_amount < 0)
cout << "\nYou can't sell what you don't own.\n";
else {
mX = mX - sell_amount;
Total = Total + (fX * sell_amount);
}
}
else if (sell_select == 't' && bse_select == 's') {
cout << "\nSell Amount: ";
cin >> sell_amount;
if (mY - sell_amount < 0)
cout << "\nYou can't sell what you don't own.\n";
else {
mY = mY - sell_amount;
Total = Total + (fY * sell_amount);
}
}
else if (sell_select = 'z' && bse_select == 's') {
cout << "\nSell Amount: ";
cin >> sell_amount;
if (mZ - sell_amount < 0)
cout << "\nYou can't sell what you don't own.\n";
else {
mZ = mZ - sell_amount;
Total = Total + (fZ * sell_amount);
}
}
else if (sell_select = 'm' && bse_select == 's') { // copy pasted this section from others
cout << "\nSell Amount: ";
cin >> sell_amount;
if (mQ - sell_amount < 0)
{
cout << "\nYou can't sell what you don't own.\n";
}
else {
mQ = mQ - sell_amount;
Total = Total + (fQ * sell_amount);
}
}
else {
cout << "Invalid Selection.";
bValid = false;
}
} while (!bValid);
}
else if (bse_select == 'e') {
cout << "\n\n\n\n\n\n A New Day has begun!";
if (fX >= 25) {
int random;
random = rand() % 20;
fX = fX - random;
}
if (fX <= 25) {
int random;
random = rand() % 20;
fX = fX + random;
}
if (fY >= 50) {
int random;
random = rand() % 50;
fY = fY - random;
}
if (fY <= 50) {
int random;
random = rand() % 30;
fY = fY + random;
}
if (fZ >= 75) {
int random;
random = rand() % 50;
fZ = fZ - random;
}
if (fZ <= 75) {
int random;
random = rand() % 110;
fZ = fZ + random;
}
if (fQ >= 150) { // again, copy pasted from others
int random;
random = rand() % 224;
fQ = fQ + random;
}
if (fQ <= 150) {
int random;
random = rand() % 350;
fQ = fQ + random;
}
}
else
cout << "Invalid Selection.";
} while (Total < 25000);
}
|
Where is the problem? I tried using switch-case in place of if and else if's, but that only made it worse as no matter what I chose, it got stuck on an infinite loop.
When I select buy, and enter gibberish characters such as "asfasd" it produces the invalid selection message perfectly. But when I enter any gibberish on sell section, it goes to an infinite loop, and alters one random stock amount to a very high number.
I've been looking at numerous websites, but can't find anything that could point me towards the problem.
Anyway, sorry for the wall of text, any help is appreciated! :)
Note: Text and selections was in my native tongue, I translated them so it makes sense to you guys, so any selection error might be because of the translation.