Deque Problems

I am trying to create a program that will allow you to add up amounts of troops that different people own. To do this, I'm using deques. I keep getting a weird problem while my program is running. It says, "The application has requested the runtime to terminate in an unusual way. Please contact support team blah blah blah."

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
            deque<string> names;
            string nameInput;
            int typeNum;
            int nameCounter = 0;
            int typeCounter = 0;
            int monsterCounter = 0;
            int numberCounter = 0;

            deque<string> monsters;
            string monsterInput;

            deque<int> troopNum;
            int numberInput;

            cout << "Please type the name of who owns the troops: ";
            getline(cin,nameInput);
            names.push_back(nameInput);
            nameCounter++;

            cout << "How many types of troops are to be entered for this user: ";
            cin >> typeNum;
            typeCounter++;

            while(typeCounter < typeNum)
            {
                cout << "Please type the name of the troop or monster: "; // IT CRASHES RIGHT HERE!!!!
                getline(cin,monsterInput);
                monsters.push_back(monsterInput);
                monsterCounter++;
                cout << "Please type the number of " << monsters.at(monsterCounter) << " that " << names.at(nameCounter) << " owns: ";
                cin >> numberInput;
                cin.ignore();

             }
I suppose the error is on line 30. When there is one element in deque, monsterCounter = 1, but to access the element you'd write monsters.at(0) ( or monsters[0] ). Deques, just like arrays, start from 0. Try monsterCounter-1 and nameCounter-1.
YEAH!! Thanks! You fixed the problem!!
1
2
3
4
5
6
7
8
9
10
nameCounter = 0;
            monsterCounter = 0;
            numberCounter = 0;

            for (int nameCounter = 0; nameCounter < names.size(); ++nameCounter)
            {
                troopFile << names.at(nameCounter) << endl;
                for (int i = 0; i < typeNum; ++i)
                    troopFile << monsters.at(monsterCounter) << " - " << troopNum.at(numberCounter) << endl;
            }


Now its crashing at the first for loop.

EDIT: Changed if statement to for loop.
Last edited on
Topic archived. No new replies allowed.