Stuck on while loop paramaters

Any Ideas on the parameters I should have for the while loop below. I need the armies to continue until there is no soldiers left. I have tried !armyOne.empty() and others but can't get it to work right. Also I do not know if my random soilders generator are in the correct format or used correctly I should say. Any help would be greatly appreciated. Thank 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
56
57
58
59
60
61
62
63
#include <iostream>
#include <queue>
#include "CinReader.h"

CinReader reader;
using namespace std;

int main()
{
    queue<int> armyOneQueue;
    int armyOne;

    queue<int> armyTwoQueue;
    int armyTwo;

//do not know if the random coding below is right
    int armyOneSoldiers = rand()%50;
    int armyTwoSoldiers = rand()%50;
    int armyOneWins = 0;
    int armyTwoWins = 0;
    int armiesTied = 0;

    cout << " How Many Soldiers Would You Like In Your Armies (1-100):  ";
    armyOne,
    armyTwo = reader.readInt(1,100);


    armyOneQueue.push (armyOne);
    armyTwoQueue.push (armyTwo);

    while (//Parameters here are what I am unsure of)
    {
        if(armyOneSoldiers = armyTwoSoldiers)
        {
            cout << "The Battle is a Draw!!!" << endl;;
            armiesTied ++;
        }
        else if(armyOneSoldiers > armyTwoSoldiers)
        {
            cout << "Army Ones Soilder has Won!!!" << endl;
            armyOneWins ++;
        }
        else
        {
            cout << "Army Twos Soilder has Won!!!" << endl;
            armyTwoWins ++;
        }

            armyOneQueue.pop();
            armyTwoQueue.pop();
    }

    cout << "*************************" << endl;
    cout << "**RESULTS OF THE BATTLE**" << endl;
    cout << "*************************" << endl << endl;
    cout << "Army One Defeated Army Two " << armyOneWins << " Times" << endl;
    cout << "Army Two Defeated Army One " << armyTwoWins << " Times" << endl;
    cout << "The Battle was a Draw " << armiesTied << " Times" << endl;



    return 0;
}
It's not called a parameter. It's called a condition. Also, here's some pseudo-code to give you a general idea of what to do:

1
2
3
4
5
6
int soldiersleft = 50;

while(soldiersleft != 0) {
    /* Your code here. Certain conditions should 
        modify soldiersleft, or else it will be an endless loop.*/
}
if(armyOneSoldiers = armyTwoSoldiers)

Did you mean this?

if(armyOneSoldiers == armyTwoSoldiers)
is my queue looking right at least. I do not want to have to butcher my code if I do not have to. It usually gets me into trouble.
I think there are a few errors with the code. Try making a flowchat of what you want to happen, because right now, you push and pop the queues, but you don't do anything with what you are popping. You are using the queue for nothing. !armyOne.empty() is giving you an error because armyOne isn't a queue, it's just an int, if line 31 was while(!armyOneQueue.empty() && !armyTwoQueue.empt(), that would repeat the loop until either army queue was empty, which in your code, would be after 1 iteration.

What I think you should do is add this after line 32:
1
2
armyOneSoldiers = armyOneQueue.front();
armyTwoSoldiers = armyTwoQueue.front();



This will allow you to push any number of int values onto the queue, representing an army's strength, then go down the queue comparing those values. As it is now, with never changing armyOneSoldiers or armyTwoSoldiers inside the loop, each if statement will always evaluate to be the same as last iteration.
Last edited on
To Randomize work has expected you need to include:
1
2
3
#include <stdio.h>
#include <stdlib.h>
#include <time.h> 


and also the first thing to include in your main:
1
2
3
4
5
int main ()
{
     srand ( time(NULL) );
     //the rest of your code..... now the rand functions will now work
}

mpruisu, those header files are deprecated. He should follow the following format:

1
2
3
4
5
6
7
8
9
#include <ctime>
using std::srand;
using std::rand;

int main(int argc, char** argv) {
    srand((unsigned)time(0));
    int num = rand()%10+1;
    return 0;
}
Hmm..... but still he needed to srand or the randomizing will give the same number over and over again even if he recomiles it... happened to me XD
Topic archived. No new replies allowed.