I have made this program which im really certain would work, it compiles with no errors, but runs continuously after getting the name of the output file.

The program takes a loads of names each with 12 numbers, gets the name using getline, and puts the numbers into an array.

Or that's what it is meant to do, what have i missed?

#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;

bool getInputFilename(char fname[])						
{
	ifstream fin;										

	cout << "Please enter the filename for input : ";
	cin >> fname;										
														
	fin.open(fname, ios::nocreate);						
														
	if (!fin.is_open())
		return false;									 
	
	fin.close();
	return true;										
}


bool getOutputFilename(char fname[])					
{
	ofstream fout;

	cout << "Please enter the filename for output : ";
	cin >> fname;										
														
	fout.open(fname);									

	if (fout.fail())									
		return false;

	fout.close();
	return true;										
}

void getInput(ifstream& fin, char name[], int marks[])
{
	while(!fin.eof())
	{
		fin.get();
		fin.getline(name, 100, '\n');

		for(int j=0; j<12; j++)
		{
			fin >> marks[j];

			

		}

		fin.get(); 
	}

	
}



void main()
{
	ifstream fin;
	ofstream fout;

	char stname[100];
	int marks[12];

	char IFname[20], OFname[20];


	while (!getInputFilename(IFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	while (!getOutputFilename(OFname))
	{
		cout << "Invalid filename try again!\n\n";
	}


	fout.open(OFname);
	fin.open(IFname);

	getInput(fin, stname, marks);
	



	fout.close();
	fin.close();
}

Code in blue is where something is wrong i believe...

Recommended Answers

All 5 Replies

Forgive me as its late and i've been up way too long, how exactly does that link help?

Your code will not compile...ios::nocreate in your code should not work in new compilers

I see what you mean, however after changing it so it does compile i still have the first problem of the program running continuously and never stopping.

It's meant to stop after all data has been read and output to the output file...

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

bool getInputFilename(char fname[])						
{
	ifstream fin;										

	cout << "Please enter the filename for input : ";
	cin >> fname;										
														
	fstream fs(fname, ios_base::in);
	
	if (!fs)
	{
		return false;
	}
	else
	{
		fin.close();
		return true;
	}
}
											

bool getOutputFilename(char fname[])					
{
	ofstream fout;

	cout << "Please enter the filename for output : ";
	cin >> fname;										
														
	fout.open(fname);									

	if (fout.fail())									
		return false;

	fout.close();
	return true;										
}

void getInput(ifstream& fin, char name[], int marks[])
{
	while(!fin.eof())
	{
		fin.get();
		fin.getline(name, 100, '\n');

		for(int j=0; j<12; j++)
		{
			fin >> marks[j];

		}

		fin.get(); 
	}
	
}



void main()
{
	ifstream fin;
	ofstream fout;

	char name[100];
	int marks[12];

	char IFname[20], OFname[20];


	while (!getInputFilename(IFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	while (!getOutputFilename(OFname))
	{
		cout << "Invalid filename try again!\n\n";
	}


	fout.open(OFname);
	fin.open(IFname);

	getInput(fin, name, marks);

        fout << name << endl;
        fout << marks << endl;

	
	fout.close();
	fin.close();
}

>i still have the first problem of the program running continuously and never stopping.
Where does it do this? Have you stepped through your program in a debugger to find out the loop that keeps repeating? There are only four places that this could happen: the two loops in getInput and the two loops in main. I also can't duplicate your problem, which suggests that you aren't giving us enough information. When you're working with a file, supply us with a sample of the file's contents to test with.

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.