Sign in to follow this  

File I/O

This topic is 2851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi again, So there's a kid in one of my classes who is always playing games (Age of Empires and Star Wars Galactic Battlegrounds) instead of working, and so me and some of the other students decided to write a program that would run instead of the game. We've done similar things to him before, but this is the first time we're actually writing the program. Usually we just change the target of the executable for the game (we are going to do that this time too, and have the target be the exe of our program). The program will make a lot of message boxes pop up. The first time he tries to play his game, he will be told that he plays it too much and that he should get back to work. If he tries again, the computer will "complain" again. If he runs it a third time, random messages will appear in the message boxes. We have it working up through the first two runs, but when the program is run for a third time, the code for the second run executes. Here is the code:
#include <windows.h>
#include <windowsx.h>
#include <fstream>
#include <string>
#include <ctime>

using std::ofstream;
using std::ifstream;
using std::string;

ofstream outFile;
ifstream inFile;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	srand(time(0));

	LPCWSTR text[10];

	text[0] = L"Performing clodhopper hotcha upwardly";
	text[1] = L"Unhobbling annihilator housebroke containedly";
	text[2] = L"Before the nest degenerates a functioning movie.";
	text[3] = L"The diesel offends his drained aardvark.";
	text[4] = L"How can the newsletter implement the rebuilt pet?";
	text[5] = L"How does the chemist laugh?";
	text[6] = L"Syssssssssstem f@ilyre imminnnennt.....rebooot...reboot...reboot";
	text[7] = L"Repellent casserole manure aboard";
	text[8] = L"The Zebra is a lie!";
	text[9] = L"Have some floorcaek (and go eat rope)!";

	bool opened = false;
	bool openedTwice = false;

	inFile.open("opened.txt");
	inFile >> opened;
	inFile.close();

	outFile.open("opened.txt");
	outFile.clear();
	outFile << 1;
	outFile.close();

	if (!opened)
	{
		MessageBox(NULL, L"I will NOT play Age of Empires any more! I refuse!", L"No!", MB_OK);
		if (IDOK)
		{
			if (MessageBox(NULL, L"You play it all the time! Wouldn't you agree?", L"You Play Too Much", MB_YESNO) == IDYES)
			{
				MessageBox(NULL, L"Admitting that there's a problem is the first step\ntowards recovery. Now get back to work.", L"Was That So Hard?", MB_OK);
			}
			else
			{
				MessageBox(NULL, L"Liar! My mouse is hurting from all of your incessant clicking!\nGood day, sir!", L"Liar", MB_OK);
			}
		}
	}
	else
	{
		inFile.open("opened2.txt");
		inFile >> openedTwice;
		inFile.close();

		if (!openedTwice)
		{
			MessageBox(NULL, L"Seriously, I won't run it!", L"Take A Hint!", MB_OK);
			if (IDOK)
			{
				if (MessageBox(NULL, L"All of this playing is putting a tremendous strain on my processor.", L"It's Killing Me", MB_OK) == IDOK)
				{
					MessageBox(NULL, L"I'm quickly losing my ability to correctly process inf0r@3tioN pr&pppper11y...", L"Quickly Fading...", MB_OK);
				}
			}
			outFile.open("opened2.txt");
			outFile.clear();
			outFile << 1;
			outFile.close();
		}
		else
		{
			int max = rand() % 10;
			int i = 0;

			while (i < max)
			{
				if (MessageBox(NULL, text[i], L"", MB_OK) == IDOK)
					i++;
			}
		}
	}
}

I think the problem is with openedTwice. When I output the value to a file to see what it is, the value is always , even though I'm pretty sure the code should change the value to a 1. A (somewhat) related question: how can I take a variable (such as an int) and output it in a MessageBox? Does it have something to do with _T and TEXT()? Thanks.

Share this post


Link to post
Share on other sites
Why are you using multiple files, where a single file would suffice? Just store the number of times the program has been run in the file. If the file doesn't exist, it must be the first run, if it does exist, it will contain the number of times the program has been run before.

Either way, what does your debugger tell you? What do the files contain after a first and after a second run?

Share this post


Link to post
Share on other sites

This topic is 2851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this