For some reason, when I do an output.put, it is not doing anything.

#include "stdafx.h"
#include <fstream>
#include <Windows.h>
using namespace std;
void main()
{
	fstream input, output;
	int in1=0, in2=0, x=0, k=0, l=0, max=0;
	input.open("input.txt", ios::in);
	output.open("output.txt", ios::out);
	while(input.good())
	{
		input>>in1;
		input>>in2;
		for(x=in1; x<in2; x++)
		{
			k=x; 
			l=0;
			while(k!=1)
			{
				if(k%2==0)k=k/2;
				if(k%2==1)k=3*k+1;
				l++;
			}
			if(l>max)max=l;
		}
		output.put('a');
		output.put((in1-(in1%10))/10-48);
		output.put((in1%10)-48);
		output.put(32);
		output.put((in2-(in2%10))/10-48);
		output.put((in2%10)-48);
		output.put(32);
		output.put((max-(max%10))/10-48);
		output.put((max%10)-48);
		output.put(10);
	}
	Sleep(1000);
}

The output.put('a') was for debugging purposes, and even that is not doing anything. What's going on?

Recommended Answers

All 6 Replies

Are you writing in a directory where you have permission?
What happens if you trap exceptions?

This worked for me (with an existing directory called c:\science):

#include <fstream>
using namespace std;

int main(void)
{
   fstream output("c:\\science\\fOut.txt", ios::out);
   output.put('a');
   output.close();
   return 0;
}

Well, I'm writing it to a file that's in the exact same folder, so I thing I am.

Can you explicitly specify the path to make sure?

C:\Users\The Masroors\Desktop\msvcpp2010projects\3np1\3np1\input.txt and
C:\Users\The Masroors\Desktop\msvcpp2010projects\3np1\3np1\output.txt.

If you take my code example and \\ escape the slashes (entering the name of your output file), do you get an error?

Umm... i think there is an error for the while condition...
Try using the following :

while(input) instead of while(input.good())

Coz i've never come across that, please correct me if im wrong, I'd like to know the correct way !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.