While loop works in the strange way!Need help!

Hey guys, Im new here and i have a "problem" with while loop.
I was writting my first game then i came across a problem which , indeed is very strange.


I have a code which lets to choose from 5 diffrent things.
Second option lets you to buy some fortifications.[War game]
Third option lets you to buy some troops.

I wrote these two options with the same code, using while loop , but strangely third option have messed up.

Principle is what you have a certain amount of money and you can spend it
among nine options.If you spend all your money and writte
" i want to buy again "
Program itself buys the same thing to the point then while loop condition is false,without asking the player if he wants to buy something more or not.

I have identical code with second option , but somehow it works and asks every time the player if he wants to buuy something or not

What i want to do is if the amount of money goes to 0 or to - , it goes to other menu where you dont have this choice ( buy stuff ).Second option ( fortification ) does this thing without a problem.


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
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

int main()
{


int Simulation_1_time_before_war_options2_watermines  = 10000;
int Simulation_1_time_before_war_options2_antiaircraftenplacments = 27000;
int Simulation_1_time_before_war_options2_bunkerenplacments       = 35000;
int Simulation_1_time_before_war_options2_rocketenplacments       = 45000;
int Simulation_1_time_before_war_options2_Civilpurposebunkers     = 25000;
int Simulation_1_time_before_war_options2_Artilleryenplacments    = 40000;
int Simulation_1_time_before_war_options2_aerodromes              = 20000;
int Simulation_1_time_before_war_options2_defensivelines          = 75000;
int Simulation_1_time_before_war_options2_minefields              = 18000;

int Simulation_1_time_before_war_options2_watermines_quantity              = 1;
int Simulation_1_time_before_war_options2_antiaircraftenplacments_quantity = 1;
int Simulation_1_time_before_war_options2_bunkerenplacments_quantity       = 1;
int Simulation_1_time_before_war_options2_rocketenplacments_quantity       = 1;
int Simulation_1_time_before_war_options2_Civilpurposebunkers_quantity     = 1;
int Simulation_1_time_before_war_options2_Artilleryenplacments_quantity    = 1;
int Simulation_1_time_before_war_options2_aerodromes_quantity              = 1;
int Simulation_1_time_before_war_options2_defensivelines_quantity          = 1;
int Simulation_1_time_before_war_options2_minefields_quantity              = 1;

int Simulation_1_time_before_war_options2_buy_quantity;


////  Variables ( troops )

int Simulation_1_time_before_war_options3_tanks          = 50000;
int Simulation_1_time_before_war_options3_jets           = 65000;
int Simulation_1_time_before_war_options3_helicopters    = 45000;
int Simulation_1_time_before_war_options3_army_of_people = 70000;
int Simulation_1_time_before_war_options3_artillery      = 35000;
int Simulation_1_time_before_war_options3_navy           = 68000;
int Simulation_1_time_before_war_options3_supply_force   = 28000;
int Simulation_1_time_before_war_options3_transport      = 25000;
int Simulation_1_time_before_war_options3_cyber_team     = 30000;

int Simulation_1_time_before_war_options3_tanks_quantity          = 1;
int Simulation_1_time_before_war_options3_jets_quantity           = 1;
int Simulation_1_time_before_war_options3_helicopters_quantity    = 1; 
int Simulation_1_time_before_war_options3_army_of_people_quantity = 1;
int Simulation_1_time_before_war_options3_artillery_quantity      = 1; 
int Simulation_1_time_before_war_options3_navy_quantity           = 1;
int Simulation_1_time_before_war_options3_supply_force_quantity   = 1;
int Simulation_1_time_before_war_options3_transport_quantity      = 1;
int Simulation_1_time_before_war_options3_cyber_team_quantity     = 1;

int Simulation_1_time_before_war_options3_buy_quantity  = 1;


////  Menu,where you can choose what to do

if( Simulation1_choice_player_state == "JK" || Simulation1_choice_player_state == "UK" )
cout << "2"  << "Build fortifications  << endl;   // second option [WORKING]
cout << "3"  << "Buy troops" << endl;            // third option [NOT WORKING]

cin  >> Simulation1_time_before_war_option_UK;  // your choice
									
system("CLS");




 ////  Second option while loop


while( Simulation1_time_before_war_option2_UK_totalmoney > 0 && Simulation1_time_before_war_option_UK == '2' &&  Simulation1_time_before_war_option2_UK_buychoice_another == 'Y')
if ( Simulation1_time_before_war_option_UK == '2' )
 {

cin  >> Simulation1_time_before_war_option2_UK_buychoice; 
////  option what to buy [1-9]

////  Simulation1_time_before_war_option2_UK_totalmoney = 250 K


}
if (Simulation1_time_before_war_option2_UK_buychoice == '1'&& Simulation1_time_before_war_option2_UK_totalmoney > 0)
{
cout << "Iveskite kieky:";
cin  >> Simulation_1_time_before_war_options2_buy_quantity;
						   Simulation1_time_before_war_option2_UK_totalmoney-=Simulation_1_time_before_war_options2_buy_quantity * Simulation_1_time_before_war_options2_watermines_quantity * Simulation_1_time_before_war_options2_watermines;

cout << "Likusi suma:" << Simulation1_time_before_war_option2_UK_totalmoney << endl;
cout << "Ar norite pirkti kazka kita? Iveskite raide Y, jeigu taip,jeigu ne - iveskite raide N:(Y/N)" << endl;
cin  >> Simulation1_time_before_war_option2_UK_buychoice_another;
////  option if you want to buy something else from fortification menu
}

 }



Simulation1_time_before_war_hours = Simulation1_time_before_war_hours-24; 
						   Simulation1_time_before_war_days = Simulation1_time_before_war_days-1;

////  these two makes -1 day from total of 7 and  -24 h from total of 168 hours

}      

////  end of the while loop





 ////  Third option while loop [ buy troops]


  while(Simulation1_time_before_war_option3_UK_totalmoney > 0 && Simulation1_time_before_war_option_UK == '3' &&  Simulation1_time_before_war_option3_UK_buychoice_another == 'Y')
{		 
 if ( Simulation1_time_before_war_option_UK == '3' )
{
cout << "Likes laikas iki karo: " << 
						   


////  now again if statements if u writte 1 and u have money

////  it goes from Simulation1_time_before_war_option3_UK_buychoice == '1'
to '9';

////  Simulation1_time_before_war_option3_UK_total money == 500 k
 }
 if (Simulation1_time_before_war_option3_UK_buychoice == '1' && Simulation1_time_before_war_option3_UK_totalmoney > 0)
{
cout << "Iveskite kieky:";
cin  >> Simulation_1_time_before_war_options3_buy_quantity;
						   Simulation1_time_before_war_option3_UK_totalmoney-=Simulation_1_time_before_war_options3_buy_quantity * Simulation_1_time_before_war_options3_tanks_quantity * Simulation_1_time_before_war_options3_tanks;
cout << "Likusi suma:" << Simulation1_time_before_war_option3_UK_totalmoney << endl;
 cout << "Ar norite pirkti kazka kita? Iveskite raide Y, jeigu taip,jeigu ne - iveskite raide N:(Y/N)" << endl;
cin  >> Simulation1_time_before_war_option3_UK_buychoice_another;

////  Simulation1_time_before_war_option3_UK_buychoice_another

 }
 
Simulation1_time_before_war_hours-24; 
Simulation1_time_before_war_days --; 
						
 }  

 //// while loop ends




} 




Then you type your choice at the start of the third statement
( Simulation1_time_before_war_option3_UK_buychoice; ) and type the amount;
while loop repeats itself until the point of negative amount and with the same choice ( if you chose 1 then it repeats itself with choice 1 until negative amount )

i would really appreciate if someone could help!!!



Last edited on
UP! I really need help , cuz i tried so many times , so many hours , replacing stuff making if statements , making other conditions....
Last edited on
Someone help!?!?
First off, the code you posted does not compile.

Line 61: Simulation1_choice_player_state not defined.
Line 62: Missing "
Line 65: Simulation1_time_before_war_option_UK undefined
Several other undefined variables also.
I forgot to tell what my orgiinal code is too long ot post here so i cut it off a little bit and forgot to put alll variables .
Problem is not Missing stuff.
Problem is about the loop.

All the stuff you pointed i have already in my code!
Topic archived. No new replies allowed.