8 Replies - 465 Views - Last Post: 15 February 2012 - 09:24 PM Rate Topic: -----

#1 clyde1993  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 15-February 12

Why is program crashing after the first part?

Posted 15 February 2012 - 08:16 PM

Why is my program crashing? This is my first time using Vectors and Iterators, and I'm relatively new to C++ so I may be missing something small.
Code:
//A Program that allows a user to maintain a list of his favorite video games.
//Using Vectors and Iterators

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

int main()
{
	string game;
	string choice;
	
	vector<string>::iterator myIterator;
	vector<string>::const_iterator iter;
	
	cout << "\t\t\tWelcome to Game Manager 3.0\n\n\n";
	vector<string> games;
	cout<< "Add a Video Game to your list: ";
	cin >> game;
	games.insert(games.begin(), game);
	cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
	cin >> choice;
	while (choice != "quit")
		if (choice == "1")
			cout << "Add a game: ";
			cin >> game;
			games.insert(games.begin(), game);
			cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
			cin >> choice;			
		if(choice == "2")
			for ( iter = games.begin(); iter != games.end(); ++iter)
			{
				cout << *iter << endl;
			}
			cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
			cin >> choice;
		if (choice == "3")
			cout<< "Select the game you want to remove: ";
			cin>> game;
			if (false)
				cout<<"That game is not in the list";
			else
				games.erase(find(games.begin(), games.end(), game));
				cout<<"Your list is now: \n";
				for ( iter = games.begin(); iter != games.end(); ++iter)
			{
				cout << *iter << endl;
			}
			cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
			cin >> choice;
			


	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Why is program crashing after the first part?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,441
  • Joined: 25-December 09

Re: Why is program crashing after the first part?

Posted 15 February 2012 - 08:22 PM

Have you tried running your program through your debugger? Your debugger should be able to tell you which line where the error is detected, and then you can look at the values of the variables at the time of the crash and see if some value looks out of place.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 clyde1993  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 15-February 12

Re: Why is program crashing after the first part?

Posted 15 February 2012 - 08:25 PM

I have tried. The program runs but two of the options freeze and the third enters an infinite loop. The debugger only tells me this:
'Beginninng C++.exe': Loaded 'C:\Users\User\Documents\Visual Studio 2010\Projects\Beginninng C++\Debug\Beginninng C++.exe', Symbols loaded.
'Beginninng C++.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
'Beginninng C++.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
'Beginninng C++.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
'Beginninng C++.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
The thread 'Win32 Thread' (0x1fa4) has exited with code -1073741510 (0xc000013a).
The program '[4644] Beginninng C++.exe: Native' has exited with code -1073741510 (0xc000013a).
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Why is program crashing after the first part?

Posted 15 February 2012 - 08:32 PM

Begin by putting braces around the lines that belong together in your while loop and if statements. Otherwise, only the first line following a while or an if is controlled by that statement.
Was This Post Helpful? 1
  • +
  • -

#5 clyde1993  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 15-February 12

Re: Why is program crashing after the first part?

Posted 15 February 2012 - 08:43 PM

Thanks r.stiltskin, that fixed the infinite loop with the first if statement. Do you have any thoughts on the other two? Whenever I run the program and use either of the other two if statements, the program freezes.


//A Program that allows a user to maintain a list of his favorite video games.
//Using Vectors and Iterators

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

int main()
{
	string game;
	string choice;
	
	vector<string>::iterator myIterator;
	vector<string>::const_iterator iter;
	
	cout << "\t\t\tWelcome to Game Manager 3.0\n\n\n";
	vector<string> games;
	cout<< "Add a Video Game to your list: ";
	cin >> game;
	games.insert(games.begin(), game);
	cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
	cin >> choice;
	while (choice != "quit")
	{
		if (choice == "1")
			cout << "Add a game: ";
			cin >> game;
			games.push_back(game);
			cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
			cin >> choice;	
	}
	{
		if(choice == "2")
			for ( iter = games.begin(); iter != games.end(); ++iter)
			{
				cout << *iter << endl;
			}
			cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
			cin >> choice;
	}
	{
		if (choice == "3")
			cout<< "Select the game you want to remove: ";
			cin>> game;
			if (false)
				cout<<"That game is not in the list";
			else
			{
				games.erase(find(games.begin(), games.end(), game));
				cout<<"Your list is now: \n";
				for ( iter = games.begin(); iter != games.end(); ++iter)
			{
				cout << *iter << endl;
			}
			cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
			cin >> choice;
			}
	}


	return 0;
}





Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Why is program crashing after the first part?

Posted 15 February 2012 - 08:48 PM

Your braces still aren't right.

while (choice != "quit")
	{
		if (choice == "1")
			cout << "Add a game: ";
			cin >> game;
			games.push_back(game);
			cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
			cin >> choice;	
	}


The way you have it now, that is the full extent of the while loop, and that if statement only controls the cout << "Add a game "; and nothing else.

The next pair of braces (in the next segment of code following the code above) does essentially nothing.

The braces for an if statement begin after the closing parenthesis of the control condition.
Was This Post Helpful? 1
  • +
  • -

#7 clyde1993  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 15-February 12

Re: Why is program crashing after the first part?

Posted 15 February 2012 - 08:56 PM

Okay, so I have fixed the braces, and yet the second and third if statement still freeze the program, like it doesn't know what to do. Are my for statements incorrect?


//A Program that allows a user to maintain a list of his favorite video games.
//Using Vectors and Iterators

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

int main()
{
	string game;
	string choice;
	
	vector<string>::iterator myIterator;
	vector<string>::const_iterator iter;
	
	cout << "\t\t\tWelcome to Game Manager 3.0\n\n\n";
	vector<string> games;
	cout<< "Add a Video Game to your list: ";
	cin >> game;
	games.insert(games.begin(), game);
	cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
	cin >> choice;
	while (choice != "quit")
	
		if (choice == "1")
		{
			cout << "Add a game: ";
			cin >> game;
			games.push_back(game);
			cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
			cin >> choice;	
		}
	
		if(choice == "2")
		{
			for ( iter = games.begin(); iter != games.end(); ++iter)
			{
				cout << *iter << endl;
			}
			cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
			cin >> choice;
		}
	
		if (choice == "3")
		{
			cout<< "Select the game you want to remove: ";
			cin>> game;
			if (false)
			{
			cout<<"That game is not in the list";
			}
			else
			{
				games.erase(find(games.begin(), games.end(), game));
				cout<<"Your list is now: \n";
				for ( iter = games.begin(); iter != games.end(); ++iter)
			{
				cout << *iter << endl;
			}
			cout<< "Press 1 to add another game\nPress 2 to list your current games\nPress 3 to remove the last game\nEnter 'quit' to exit\n\n ";
			cin >> choice;
			}
		}


	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Why is program crashing after the first part?

Posted 15 February 2012 - 09:14 PM

Now you don't have any braces for the while loop, so the only thing in the loop is the first if statement.

When you enter "2", choice isn't "quit" and choice isn't "1", and the only thing in the loop is if(choice == "1) so you have an infinite loop again.
Was This Post Helpful? 1
  • +
  • -

#9 clyde1993  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 15-February 12

Re: Why is program crashing after the first part?

Posted 15 February 2012 - 09:24 PM

Thank you so much for bearing with me. That fixed it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1