Weird Loop of Infiniteness :)

I'm making a program that stores numbers in a text file. Whenever my code reaches a section in the code that has getline() and cout statements it goes into an infinite loop and displays random symbols on the console window. Afterwards, it crashes.... :(
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
      case 'c':{
            troopFile.open("Troops Number.txt");

            int monParam = 0;
            int nameParam = 0;
            int numParam = 0;
            string name[30];
            int typeNum;
            string monster[monParam];               
            int troopNum[numParam];

            cin.ignore();
            cout << "Please type the name of who owns the troops: ";
            getline(cin,name[nameParam]);
            cout << "How many types of troops/monsters are you going to enter for " << name[nameParam] << "? ";
            cin >> typeNum;
            cin.ignore();

            for (int i = 0; i < typeNum; ++i)
            {
            cout << "Please type the name of the troop/monster: ";
            getline(cin,monster[monParam]);
            ++monParam;
            cout << "Please type the number of " << monster[monParam] << " that " << name[nameParam] << " owns: ";
            cin >> troopNum[numParam];
            ++numParam;
            }

            
            troopFile << name[nameParam] << endl;
            for (int i = 0; i < typeNum; ++i)
                troopFile << monster[monParam] << " - " << troopNum[numParam] << endl;
            ++nameParam;

            
            break;
        }



If you want the full code, its in this pastebin link: http://pastebin.com/iru1supP
I may be wrong about this, but when you declare an array, isn't the size set at that time? I don't think that arrays get resized the way that you're doing it.

If I'm right, then the monster[] and the troopNum[] arrays would both be of size zero. This may cause problems when you try to use the array outside of its bounds, which would basically be any use of the array.

PS- Using a linked list may allow you to have the growing-array-like-thing that you're looking for.
Last edited on
You might want to do something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::deque<string> monster;
// You should check for errors to ensure that a number is entered.
while( !(cin >> typeNum))
{
     std::cout << "That's not a number; ";
     std::cin.clear();
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

// I think that you need to do this again in order to ensure that the next getline works.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

// Now you have the data to properly resize your sequence container (dynamic array).
monster.resize(typeNum);

@chemical engineer: I took out the /m's and it still crashes.

@kempofighter: What is deque??? I'm not exactly sure what you did in this code. I know what it does, but I don't understand what each line is doing.


@EVERYONE!!::: I noticed that the crash happens on the first for loop. It starts its weird routine directly after I hit 'ENTER' when I type in the name of the troop.
closed account (D80DSL3A)
I would expect it to crash then. I believe that chemical engineer was trying to point out that your array bounds are being exceeded.
You have:
1
2
3
4
int monParam = 0;
int numParam = 0;
string monster[monParam];// why does a variable work here?
int troopNum[numParam];// I thought a const int was required for array dimension 

Thus, monster and troopNum are arrays with zero elements. In the following for loop you are referencing the arrays out of bounds - even monster[0] and troopNum[0] are out of bounds!

It looks like you need your arrays to have typeNum elements each. Since this is variable I'd suggest either using vectors (instead of arrays) or go with dynamic allocation (use of new and delete).
The second approach goes like this (for the integer array). Following input of a value for typeNum, do this
int* troopNum = new int[typeNum];
Now you have the number of elements you need for your program. Don't forget to delete this memory when you are done with the array:
delete [] troopNum; Do this with the other arrays as well to get the memory space needed for them.
oh okay. now i see wut you guys meant.
thanks for the help guys.
deque is a dynamic sequence container. Since you are trying to use an array sized on userinput you need a dynamic sequence container.
Topic archived. No new replies allowed.