STL Queue Questions
May 24, 2011 at 6:09am UTC
Coding Below. Queue confusion on this project. I stopped and posted because I do not want to butcher my code anymore. I need to have user input how many soldiers they want in their army. Then each one compares random integers to reveal a winner. Then a soldier is taken away or popped after each cycle. I am lost on how to get this coding correct and my head is starting to spin. Any Advice.
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
#include <ctime>
#include <iostream>
#include <queue>
#include "CinReader.h"
CinReader reader;
using namespace std;
int main()
{
srand ( time(NULL) );
int armyOneSize = 0;
int armyTwoSize = 0;
int armyOneSoldiers = rand()%50;
int armyTwoSoldiers = rand()%50;
int armyOneWins = 0;
int armyTwoWins = 0;
int armiesTied = 0;
queue<int > armyOne;
queue<int > armyTwo;
cout << " How Many Soldiers Would You Like In Your Armies (1-100): " ;
armyOneSize,
armyTwoSize = reader.readInt(1,100);
//do not know if the random coding below is right
armyOne.push(armyOneSize);
armyTwo.push(armyTwoSize);
while (!armyOneSize > 0 && armyTwoSize > 0)
{
if (armyOneSoldiers == armyTwoSoldiers)
{
cout << "The Battle is a Draw!!!" << endl;;
armiesTied ++;
armyOneSize --;
}
else if (armyOneSoldiers > armyTwoSoldiers)
{
cout << "Army Ones Soilder has Won!!!" << endl;
armyOneWins ++;
armyOneSize --;
}
else
{
cout << "Army Twos Soilder has Won!!!" << endl;
armyTwoWins ++;
armyOneSize --;
}
}
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;
armyOne.pop();
armyTwo.pop();
return 0;
}
Last edited on May 24, 2011 at 6:14am UTC
May 24, 2011 at 7:17am UTC
I don't understand what you are trying to make. What is the point of user input if you are comparing random numbers?
By the way, lines 23, 24 don't do what you think. In C++ you may consider comma operator similar to a semicolon.
Topic archived. No new replies allowed.