Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Heap Corruption question?

$
0
0
at the end of my 2d shooter i store their info to a binary file, then at the beginning of the game i ask if they would like to see it which will read it back in and save it to a text file for them, it was working fine for the bast couple of days. today i got an error stating i had a heap corruption and when i display whet is in the txt file for them i am noticing that it is reading in garbage... what could cause this in the code below.

	using namespace System::Windows::Forms;
	auto btn = MessageBox::Show("Would you like to see past games info?", "Game Info",
		MessageBoxButtons::YesNo, MessageBoxIcon::Question);
	if (DialogResult::Yes == btn){
		unsigned short display;
		cout << "How would you like to see it displayed \n\n";
		cout << "1).Name, ascending\n2).Name, descending\n3).Time, descending\
				\n4).Score, descending\n5).Save to .txt\n6).Play game!\n\nChoice: ___\b\b";
		cin >> display;
		system("cls");
		ifstream input;
		input.open("gameinfo.bin", ios_base::binary);
input.seekg(0, ios_base::end);
			auto filesize = static_cast<unsigned int>(input.tellg());
			auto records = filesize / sizeof GameInfo;
			vector<GameInfo> all(records);
			input.seekg(0);
			input.read((char *)all.data(), filesize);
			input.close();
			ostream_iterator<GameInfo> oi(cout, "\n");
			string txtfile;
						ofstream out;
			ostream_iterator<GameInfo> iout(out, "\n");
		
			switch (display)
			{
			case 1:
				Console::ForegroundColor = ConsoleColor::DarkCyan;
			cout << "Name";
			Console::SetCursorPosition(20, Console::CursorTop);
			cout << "Score";
			Console::SetCursorPosition(30, Console::CursorTop);
			cout  << "Time" << '\n';
			cout << "_____";
			Console::SetCursorPosition(20, Console::CursorTop);
			cout << "______";
			Console::SetCursorPosition(30, Console::CursorTop);
			cout  << "_____" << '\n';

			sort(all.begin(), all.end(), [](GameInfo const &a, GameInfo const &B)/> -> bool {
		return string(a.name) < b.name;
			});
			
			copy(all.begin(), all.end(), oi);
			for_each(all.begin(), all.end(), [](GameInfo const &s){});
			cout << '\n';

				break;

			case 2:
				Console::ForegroundColor = ConsoleColor::DarkCyan;
			cout << "Name";
			Console::SetCursorPosition(20, Console::CursorTop);
			cout << "Score";
			Console::SetCursorPosition(30, Console::CursorTop);
			cout  << "Time" << '\n';
			cout << "_____";
			Console::SetCursorPosition(20, Console::CursorTop);
			cout << "______";
			Console::SetCursorPosition(30, Console::CursorTop);
			cout  << "_____" << '\n';

			sort(all.begin(), all.end(), [](GameInfo const &a, GameInfo const &B)/> -> bool {
		return string(a.name) > b.name;
			});
			
			copy(all.begin(), all.end(), oi);
			for_each(all.begin(), all.end(), [](GameInfo const &s){});
			cout << '\n';
				break;

			case 3:
				Console::ForegroundColor = ConsoleColor::DarkCyan;
			cout << "Name";
			Console::SetCursorPosition(20, Console::CursorTop);
			cout << "Score";
			Console::SetCursorPosition(30, Console::CursorTop);
			cout  << "Time" << '\n';
			cout << "_____";
			Console::SetCursorPosition(20, Console::CursorTop);
			cout << "______";
			Console::SetCursorPosition(30, Console::CursorTop);
			cout  << "_____" << '\n';

			sort(all.begin(), all.end(), [](GameInfo const &a, GameInfo const &B)/> -> bool {
		return (a.time) > b.time;
			});
			
			copy(all.begin(), all.end(), oi);
			for_each(all.begin(), all.end(), [](GameInfo const &s){});
			cout << '\n';
				break;

			case 4:
				Console::ForegroundColor = ConsoleColor::DarkCyan;
			cout << "Name";
			Console::SetCursorPosition(20, Console::CursorTop);
			cout << "Score";
			Console::SetCursorPosition(30, Console::CursorTop);
			cout  << "Time" << '\n';
			cout << "_____";
			Console::SetCursorPosition(20, Console::CursorTop);
			cout << "______";
			Console::SetCursorPosition(30, Console::CursorTop);
			cout  << "_____" << '\n';

			sort(all.begin(), all.end(), [](GameInfo const &a, GameInfo const &B)/> -> bool {
		return (a.score) > b.score;
			});
			
			copy(all.begin(), all.end(), oi);
			
			for_each(all.begin(), all.end(), [](GameInfo const &s){});
			cout << '\n';
				break;

				case 5:
					play = true;
					break;
			default:
				break;
			}
						

				system("pause");
			system("cls");
			cout << "Please enter the name of a text file you would like it saved to\n";
					cout << "____________\b\b\b\b\b\b\b\b\b\b\b\b";
					cin >> txtfile;
					if (txtfile.size() == 0)
					{
						txtfile = "Highscores";
					}
					txtfile += ".txt";
					out.open(txtfile);
			copy(all.begin(), all.end(), iout);
			out.close();

			fin.open(txtfile);
			system("cls");
			cout << "Info save to txt file:\n\n";
			copy(all.begin(), all.end(), oi);
			for_each(all.begin(), all.end(),[](GameInfo const &s){});
			fin.close();
		
			system("pause");


	}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>