program stopped running error

I am trying to make a Go Fish program in C++, and am currently a student who is trying to better my knowledge of this language on my own. Actually I am done with my C++ classes and will be moving on to Java next quarter.

My code is below, all I am trying to do is output the vector so that I can verify that the shuffle works. When I run the program, then select 1 to Play, the program will display the first 20 elements of the vector then a window pops up saying "GoFish.exe has stopped running" and gives me 3 options: search online for a solution, close the program, or debug the program. I cannot debug, because I haven't learned how to implement that. By that I mean, I can read what it says but I do not understand what it means.

#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int selection;
int main()
{
cout << "Welcome to Go Fish" << endl << endl;
cout << "1 - Play" << endl;
cout << "2 - Exit" << endl << endl;
cout << "Please make your selection: ";
cin >> selection;

do
{
vector<string> deck; //used to store cards in deck
deck.push_back("BLUE_RHINO");
deck.push_back("YELLOW_RHINO");
deck.push_back("GREEN_RHINO");
deck.push_back("RED_RHINO");
deck.push_back("BLUE_ELEPHANT");
deck.push_back("YELLOW_ELEPHANT");
deck.push_back("GREEN_ELEPHANT");
deck.push_back("RED_ELEPHANT");
deck.push_back("BLUE_OSTRICH");
deck.push_back("YELLOW_OSTRICH");
deck.push_back("GREEN_OSTRICH");
deck.push_back("RED_OSTRICH");
deck.push_back("BLUE_GIRAFFE");
deck.push_back("YELLOW_GIRAFFE");
deck.push_back("GREEN_GIRAFFE");
deck.push_back("RED_GIRAFFE");
deck.push_back("BLUE_LION");
deck.push_back("YELLOW_LION");
deck.push_back("GREEN_LION");
deck.push_back("RED_LION");

srand(time(0)); //initialize the randomizer - need this when using any random variable

random_shuffle(deck.begin(), deck.end()); //shuffles deck vector

for (int i = 0; i < 21; i++)
{
cout << deck[i] << endl;
//system("PAUSE");
}

}while (selection != 2);//end do while

return 0;
}





So, I opened a new project and just put this in it to test my FOR loop to make sure my logic was right. This code worked fine, so I was still stumped.

#include <iostream>
using namespace std;

int main()
{
for( int = 0; i < 21; i++)
{
cout << i << endl;
}

return 0;
}

I also had just a WHILE loop, and changed it to the DO WHILE loop because I thought maybe the PRE/POST test had something to do with it. Didn't change the error.

And lastly I changed the loopContinuationCondition to i < 21. Originally I had for ( int i = 0; i < deck.size(); i++) and it created an infinite loop without any errors. That is why I put the "PAUSE" line in, so I could stop it. So I decided to use i < 21 since I have 20 elements in the vector. Now I am getting the error I am asking about.

The code I have written is by no means proper in a "real world" environment, so please excuse my lack of work experience. It is not finished, I just like to constantly test along the way.

Thanks for your time,
Charlene

Your while loop will run forever since selection is only modified outside it, not inside it. Also, you only need to call srand() once.
You're... moving ON to Java? Weird system... I would've thought Java first, then C++.

Anyways, as firedraco said, it's your while loop. You probably want to put your selection menu inside of the loop, since that would be the easiest solution to the problem. It might not be the most elegant solution, but since you're just testing to see if it works, it doesn't matter.
Thanks to you both firedraco and NGen!

firedraco:
I understand what you are saying, and I didn't even think about that line being inside of the loop.

NGen:
With the selection variable - I was just following the "format" of other programs I have written.

I have learned Python, VB .net, C++, and SQL. It also seemed odd to me the order in which we learned them. Everyone besides me hated C++, and barely passed the courses. However we only did one program using vectors, and I want to fully understand their capabilities. So thanks again for help with my program, and I will probably need more help along the way.

Charlene
If you're still reading this, I suggest you ask your teacher to rethink the order in which they teach things. VB... I don't even know why they would teach that. It's so pointless, and the syntax doesn't resemble any other languages. It's about as valuable as MUMPS (the language, look it up... not fun). Python isn't exactly 'pointless', but the syntax issue is the same (as far as I know, I only took a quick look at it and thought "No. Just... No."). Java and C++ are similar enough, but Java is simpler, the main difference syntax-wise being pointers and templates. I would think that SQL would be last.
Last edited on
Topic archived. No new replies allowed.