C++, End Of File Markers, etc. (again, help appreciated)

Joined
Aug 29, 2003
Messages
2,874
Reaction score
2
I have created a crude piece of code that is to take inputs from the user until they wish to exit.

I've reached a snag, and it's 12:30am over here in Canada, and I can't quite seem to get the End Of File Marker to work properly.

Code:
#include <stdio.h>
void main()
{
	FILE *empl;
	long idnum;
	int i, num;
	char edtype;
	float salary;	
	empl = fopen("C:\\Documents and Settings\\MDG\\Assignment 3\\salary.txt", "w");	
	printf("Press any key to begin or CTRL-Z to exit");
	i = scanf("%d", num);
	fflush(stdin);
	while (i != EOF)
	{
	while(!feof(empl))
	{
		printf("Student Number => ");
		scanf("%ld", &idnum);
		fflush(stdin);
		printf("Education Type ('c', 'u', 'h') => ");
		scanf("%c", &edtype);
		fflush(stdin);

		do{
			if (!(edtype == 'c' || edtype == 'u' || edtype == 'h'))
					{
						printf("Input is invalid.\n");
						scanf("%c", &edtype);
					}
			else
				break;
			} while (!(edtype == 'c' || edtype == 'u' || edtype == 'h') || EOF);

		printf("Salary => ");
		scanf("%f", &salary);
		fprintf(empl, "%-c %ld %g\n", edtype, idnum, salary);
		} 
	}
	fclose(empl);
	empl = fopen("C:\\Documents and Settings\\MDG\\Assignment 3\\salary.txt", "r");
	fscanf(empl, "%c", edtype);
	printf("Student Number		Education Type		Salary\n");
	printf("------------------------------------------------------\n");
	while(!feof(empl))
	{
		fscanf(empl, "%ld %c %f", &idnum, &edtype, &salary);
		printf("%ld	%-c	$%7.2f\n", idnum, edtype, salary);
		fscanf(empl, "%c", edtype);
	}
	fclose(empl);
}

As with the last time, it's likely to be something trivial. I just can't see the problem now. I'll keep working on it until I do find a fix, but suggestions would be helpful. :)
 
Hmm how would you reach an EOF if you a writing to a file, unless you are waiting to run out of hard disk space ? Or are you trying to read a control z as the EOF ? if that's the case you should be looking at stdin not the file you are writing to.
 
I'm not much of a C++ coder, but I don't get how you program works... num looks like it's used before it's initialized, and it looks as if you are doing an EOF comparison for the output file. Either I'm confused, or somethings out of whack here.
 
It doesn't need to be explicitly initialized.
The first thing he does with it, is to assign something to it. Should he have read it, that would have caused a problem!
 
c++ version of your program <3

#include <iostream>
#include <fstream>
using namespace std;
void main()
{
ifstream infile;
int userInput;

infile.open("salary.txt");
cout<<"Input salary to find:"<<endl;
cin>>userInput;
while (!infile.eof())
{
int temp;
infile>>temp;
if(temp != userInput)
{
///do nothing
}
else
{
cout<<"Salary found!"<<endl;
break;
}
}
infile.close();
return 0;
}


/*
Of course in c++ there are a good 5 different ways to do things. This however is a very, VERY simple approach to file input and end of file looping. However the content of the loop could be applied to anything from simple integers all the way to objects of classes within dynamically created nodes on heap memory. Lest we all forget, for every 'new' in your code there better be a 'delete' or ur creating memory leaks. hahha hope this helped
*/
 
I think it's silly teaching people 'C++' with all this messy C. Sure give em a start with C, it's quite useful to get familiar with scanf/printf etc. But C++ generally eliminates all that messyness.
 
so glad i never was taught C ... and regret i havent either tho
 
I've had C 2 years ago, which was quite nice. Atm having Java, I couldn't go back to C, missing all those fancy features Java has :)

We'll see what C++ gives, I'm taking quite a thorough course starting Februari.
 
Knowledge of pointers is a real help, even in managed code. Just the underlying knowledge of what is going on really makes a difference.
 
c++ is majorly important and useful not only because of pointers but because of "inheritance" being the ability to inherit properties from one(base) class to another(derived) class. <3
 
togi said:
c++ is majorly important and useful not only because of pointers but because of "inheritance" being the ability to inherit properties from one(base) class to another(derived) class. <3

Inheritance came about years ago with languages like Smalltalk which is 30 years old!! Although yes C++ first brought into mainstream use.
 
Back
Top