View Single Post
Old Jan 28th, 2006, 9:31 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You don't need a function do do something as trivial as out << num << std::endl;. I'd personally do it like this:
#include <iostream>
#include <fstream>

int writeNumbers(std::string filename, int start, int end);

int main ()
{
	std::string filename;
	int start, end;
	
	std::cout << "Enter the name of the file you want to write to: ";
	std::cout.sync(); // flushes the output buffer to make sure the string is shown
	std::getline(cin, filename); // gets the entire line and stores it in "filename"
	
	std::cout << "Enter the starting value: ";
	std::cout.sync();
	std::cin >> start; // gets the starting value and stores it in "start"
	
	std::cout << "Enter the ending value: ";
	std::cout.sync();
	std::cin >> end;
	
	writeNumbers(filename, start, end);
	
	return 0;

}

bool writeNumbers (std::string filename, int start, int end)
{
	std::ostream out(filename);
	
	if (start < end)
	{
		for (int i = start; i <= end; i++)
		{
			out << i << std::endl;
		}
	}
	else
	{
		for (int i = start; i >= end; i--)
		{
			out << i << std::endl;
		}
	}
	
	out.close();
}
I'll let you add the error-checking.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote