Weve been given this example to work with and Im not sure about the line 9 char cont = 'n'; - is there even a need for it? The rest of the program I understand.

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	int no = 1;
	FILE *nofile = NULL;
	char cont = 'n';

	// read numbers from file
	if ( (nofile = fopen( "numbers.dat", "rb" )) == NULL )
	{
		cout << "Cannot open numbers file" << endl;
		exit( -1 );
	}

	cout << endl;
	cout << "Stored numbers are" << endl;
	fread( &no, sizeof( no ), 1, nofile );
	while ( feof( nofile ) == 0 )
	{
		cout << no << endl;
		fread( &no, sizeof( no ), 1, nofile );
	}
	fclose( nofile );
	cin.ignore();
	cin.get();
}

Recommended Answers

All 2 Replies

Weve been given this example to work with and Im not sure about the line 9 char cont = 'n'; - is there even a need for it? The rest of the program I understand.

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	int no = 1;
	FILE *nofile = NULL;
	char cont = 'n';

	// read numbers from file
	if ( (nofile = fopen( "numbers.dat", "rb" )) == NULL )
	{
		cout << "Cannot open numbers file" << endl;
		exit( -1 );
	}

	cout << endl;
	cout << "Stored numbers are" << endl;
	fread( &no, sizeof( no ), 1, nofile );
	while ( feof( nofile ) == 0 )
	{
		cout << no << endl;
		fread( &no, sizeof( no ), 1, nofile );
	}
	fclose( nofile );
	cin.ignore();
	cin.get();
}

The "cont" variable is never used in the code you posted after line 9, so it's unnecessary unless this is only some snippet of a larger piece of code where cont actually IS used. But if this is the whole program, you can safely delete that line. It has no effect on anything.

commented: Cheers! +1

Thanks mate for clearing that up, I suspected as much and yes it is the whole program.

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.