Thread: Need a suggestion on a school project..

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    15

    Need a suggestion on a school project..

    Alright, here's what's up. My teacher assigned us a project that involved us creating a "pay roll" program. This program had to allow him to edit pay rolls, view pay rolls, and add bonuses (sp?). Well, me and a few others are at work on the project. It isn't even close to complete but we're working on it rather slowly. Well, I would just like a suggestion on how to open a file, and have the text displayed in the desired area. Here, let me just show you what we have complete so far. It's sort of working, but not how it's intended to work. Here's what we have so far (BTW: the beginninins section of the code has some commented out lines, those are to write to the files and we haven't quite started on that really):

    Code:
    /*
    Pay Roll Program
    All C++ members
    Work Begin:  9-18-01
    	Status:  Incomplete, MENU SYSTEM AND FILE REWRITE UNDER CONSTRUCTION
    */
    
    #include <stdio.h>	    /*needed for basic printf statement and FILE statement*/
    #include <conio.h>	    /*needed for getch command to work*/
    #include <iostream.h>	/*needed for cout command*/
    #include <stdlib.h>		/*needed for exit(0); statement to work*/
    
    /*Non-working section begin*/					
    //void info()
    //{
    //	int x;
    	/*Begin FILE section*/
    //	FILE *mattb;
    //	FILE *davec;
    //	FILE *curlee;
    //	FILE *timd;
    //	FILE *kurte;
    //	FILE *randym;
    //	FILE *shawnm;
    //	FILE *gennam;
    //	FILE *ryanr;
    //	FILE *carlosr;
    //	FILE *nicks;
    //	FILE *tias;
    //	FILE *ronw;
    //	FILE *austinw;
    //	FILE *fiorella;
    	/*End FILE section*/
    
    	/*Begin file name definitions*/
    //	mattb = fopen("mattb.txt","w");
    //	davec = fopen("davec.txt","w");
    //	curlee = fopen("curlee.txt","w");
    //	timd = fopen("timd.txt","w");
    //	kurte = fopen("kurte.txt","w");
    //	randym = fopen("randym.txt","w");
    //	shawnm = fopen("shawnm.txt","w");
    //	gennam = fopen("gennam.txt","w");
    //	ryanr = fopen("ryanr.txt","w");
    //	carlosr = fopen("carlosr.txt","w");
    //	nicks = fopen("nicks.txt","w");
    //	tias = fopen("tias.txt","w");
    //	ronw = fopen("ronw.txt","w");
    //	austinw = fopen("austinw.txt","w");
    //	fiorella = fopen("fiorella.txt","w");
    	/*End file name definitions*/
    /*End non-working section*/
    //}
    
    void view()
    {
    	int c;
    	/*Begin FILE section*/
    	FILE *mattb;
    	FILE *davec;
    	FILE *curlee;
    	FILE *timd;
    	FILE *kurte;
    	FILE *randym;
    	FILE *shawnm;
    	FILE *gennam;
    	FILE *ryanr;
    	FILE *carlosr;
    	FILE *nicks;
    	FILE *tias;
    	FILE *ronw;
    	FILE *austinw;
    	FILE *fiorella;
    	/*End FILE section*/
    
    	/*Begin file name definitions*/
    	mattb = fopen("files/mattb.txt","r");
    	davec = fopen("files/davec.txt","r");
    	curlee = fopen("files/curlee.txt","r");
    	timd = fopen("files/timd.txt","r");
    	kurte = fopen("files/kurte.txt","r");
    	randym = fopen("files/randym.txt","r");
    	shawnm = fopen("files/shawnm.txt","r");
    	gennam = fopen("files/gennam.txt","r");
    	ryanr = fopen("files/ryanr.txt","r");
    	carlosr = fopen("files/carlosr.txt","r");
    	nicks = fopen("files/nicks.txt","r");
    	tias = fopen("files/tias.txt","r");
    	ronw = fopen("files/ronw.txt","r");
    	austinw = fopen("files/austinw.txt","r");
    	fiorella = fopen("files/fiorella.txt","r");
    	/*End file name definitions*/
    
    	system("CLS");									/*Very cool clear screen command*/
    	cout << "\n\tVIEW PAY ROLL INFORMATION";
    	cout << endl;
    	cout << endl << "\t1.) Matt B.\t2.) Dave C.";
    	cout << endl << "\t3.) Brandon C.\t4.) Tim D.";
    	cout << endl << "\t5.) Kurt E.\t6.) Randy M.";
    	cout << endl << "\t7.) Shawn M.\t8.) Genna M.";
    	cout << endl << "\t9.) Ryan R.\t10.) Carlos R.";
    	cout << endl << "\t11.) Nick S.\t12.) Tia S.";
    	cout << endl << "\t13.) Ron W.\t14.) Austin W.";
    	cout << endl << "\t15.) Fiorella\t16.) Exit the Pay Roll program";
    	cout << endl;
    	cout << endl << "\tPlease make your selection now:";
    	cout << endl;
    	cout << endl << "\t";
    	cin >> c;
    
    /*Start Matt B.*/
    if(c == 1)
    {
    	if(!mattb)
    	{
    		cout << "\n\tTHIS FILE CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(mattb);
    		putchar(c);
    	}
    	while(c != EOF);
    	fclose(mattb);
    }
    /*End Matt B.*/
    /*Start Dave C.*/
    else if(c == 2)
    {
    	if(!davec)
    	{
    		cout << "\n\tMATT'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(davec);
    		putchar(c);
    	}
    	while(c != EOF);
    	fclose(davec);
    }
    /*End Dave C.*/
    /*Start Curlee*/
    else if(c == 3)
    {
    	if(!curlee)
    	{
    		cout << "\n\tCURLEE'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(curlee);
    		putchar(c);
    	}
    	while(c != EOF);
    	fclose(curlee);
    }
    /*End Curlee*/
    /*Start Tim D.*/
    else if(c == 4)
    {
    	if(!timd)
    	{
    		cout << "\n\tTIM'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(timd);
    		putchar(c);
    	}
    	while(c != EOF);
    	fclose(timd);
    }
    /*End Tim D.*/
    /*Start Kurt E.*/
    else if(c == 5)
    {
    	if(!kurte)
    	{
    		cout << "\n\tKURT'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(kurte);
    		putchar(c);
    	}
    	while(c != EOF);
    	fclose(kurte);
    }
    /*End Kurt E.*/
    /*Start Randy M.*/
    else if(c == 6)
    {
    	if(!randym);
    	{
    		cout << "\n\tRANDY'S PAY ROLL CANNOT BE FOUND.\n\n\t";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(randym);
    		putchar(c);
    	}
    	while (c != EOF);
    	fclose(randym);
    }
    /*End Randy M.*/
    /*Start Shawn M.*/
    else if(c == 7)
    {
    	if(!shawnm);
    	{
    		cout << "\n\tSHAWN'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(shawnm);
    		putchar(c);
    	}
    	while (c != EOF);
    	fclose(shawnm);
    }
    /*End Shawn M.*/
    /*Start Genna M.*/
    else if(c == 8)
    {
    	if(!gennam);
    	{
    		cout << "\n\tGENNA'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(gennam);
    		putchar(c);
    	}
    	while (c != EOF);
    	fclose(gennam);
    }
    /*End Genna M.*/
    /*Start Ryan R.*/
    else if(c == 9)
    {
    	if(!ryanr)
    	{
    		cout << "\n\tRYAN'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(ryanr);
    		putchar(c);
    	}
    	while (c != EOF);
    	fclose(ryanr);
    }
    /*End Ryan R.*/
    /*Start Carlos R.*/
    else if(c == 10)
    {
    	if(!carlosr)
    	{
    		cout << "\n\tCARLOS'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(carlosr);
    		putchar(c);
    	}
    	while (c != EOF);
    	fclose(carlosr);
    }
    /*End Carlos R.*/
    /*Start Nick S*/
    else if(c == 11)
    {
    	if(!nicks)
    	{
    		cout << "\n\tNICK'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(nicks);
    		putchar(c);
    	}
    	while (c != EOF);
    	fclose(nicks);
    }
    /*End Nick S.*/
    /*Start Tia S.*/
    else if(c == 12)
    {
    	if(!tias)
    	{
    		cout << "\n\tTIA'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(tias);
    		putchar(c);
    	}
    	while (c != EOF);
    	fclose(tias);
    }
    /*End Tia S.*/
    /*Start Ron W.*/
    else if(c == 13)
    {
    	if(!ronw)
    	{
    		cout << "\n\tRON'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(ronw);
    		putchar(c);
    	}
    	while (c != EOF);
    	fclose(ronw);
    }
    /*End Ron W.*/
    /*Start Austin W.*/
    else if(c == 14)
    {
    	if(!austinw)
    	{
    		cout << "\n\tAUSTIN'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(austinw);
    		putchar(c);
    	}
    	while (c != EOF);
    	fclose(austinw);
    }
    /*End Austin W.*/
    /*Start Fiorella*/
    else if(c == 15)
    {
    	if(!fiorella)
    	{
    		cout << "\n\tFIORELLA'S PAY ROLL CANNOT BE FOUND.\n\n";
    		exit(0);
    	}
    
    	do				/*Start screwed up "loop"*/
    	{
    		c = fgetc(fiorella);
    		putchar(c);
    	}
    	while (c != EOF);
    	fclose(fiorella);
    }
    /*End Fiorella*/
    else if(c == 16)
    	exit(0);
    }
    
    void main()
    {
    	int menu;
    
    	cout << "\t\tPAY ROLL PROGRAM\b";
    	cout << endl << "\n\tSelect 1 to enter pay roll information";
    	cout << endl << "\tSelect 2 to view pay roll information";
    	cout << endl << "\tSelect 3 to exit the pay roll program";
    	cout << endl;
    	cout << endl << "\tPlease make your selection now:";
    	cout << endl;
    	cout << endl << "\t";
    	cin >> menu;
    
    //	if (menu == 1)  /*The dreaded if statement, displays function info if 1 is pressed*/
    //		info();
    	/*else*/ if (menu == 2)
    		view();             
    	else if (menu == 3)			/*Exits program when 3 is pressed*/
    		exit(0);
    	else if (menu == 4)
    		cout << "\n\tThat is not a proper selection";
    }
    
    
    
    /*Pay Roll Setup
    	1								2								3
    1.) Matt B.		2.) Dave C.			<-								Thank you...
    3.) Brandon C.	4.) Tim D.			<-									Goodbye
    5.) Kurt E.		6.) Randy M.		<-
    7.) Shawn M.	8.) Genna M.		<-
    9.) Ryan R.		10.) Carlos R.		<-
    11.) Nick S.	12.) Tia S.			<-
    13.) Ron W.		14.) Austin W.		<-
    15.) Fiorella D. 16.) Exit 			<-
    
    
      1.) MB							2.) MB
    	  Wages - $15/hr					Wages - $15/hr
    	  Funds - $0.00						Funds - $0.00
    	  Hours - 0hrs						Hours - 0hrs
    	  Press Enter to Return				Press 1 to add/subtract $
    										Press 2 to add/subtract hours
    										Press 3 to exit
    
    										1.)  Enter funds to add/subtract
    										2.)  Enter hours to add/subtract
    */
    We're all pretty new to programming and this is sort of our first sort of serious program. Our other ones were simple phone number conversion programs and the well knowned C to F conversion program. If you like to make any other suggestions that may make things easier for us, that's cool too. Sorry if the code looks a little unusual. Hopefully someone can help us out.


    --Screwz Luse

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    For a start try creating a structure to hold the file details

    ie
    File handle(return of fopen CreateFile() ect)
    string for name of file ie "MattB.txt"


    and anything else for the file handling.

    then declare an array of the struct and hash define some int indexes

    ie
    #define MAX_FILES 15
    #define MATTB 0
    #define DAVEC 1

    then you can use for loops to open/validate the files. Use the string in error msg's ect.

    If you need a file you can call it with the define
    Code:
    if(iChoice==MATTB)
    {
        FileArray[MATTB].fFile = fopen(  FileArray[MATTB].sFileName ,"w" );
        if(FileArray[MATTB].fFile==NULL)
            sprintf(sErrorBuffer, "File %s has failed to open.", FileArray[MATTB].sFileName );
    //ect
    This will reduce the length of code and increase readability.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    In addition, decide whether this is going to be in C or C++

    And use int main

    And don't open all the files at once - most operating systems have fairly small limits on the number of files you can have open at the same time, and your existing approach will not extend well at all.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    15
    Thanx for the suggestions. I'll deffinately bring it up during the next class and we'll cut down on the size and how many files are open at a time.

    This is going to be in C. We're using Microsoft Visual C++ to write the program.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    15
    Originally posted by novacain
    For a start try creating a structure to hold the file details

    ie
    File handle(return of fopen CreateFile() ect)
    string for name of file ie "MattB.txt"


    and anything else for the file handling.

    then declare an array of the struct and hash define some int indexes

    ie
    #define MAX_FILES 15
    #define MATTB 0
    #define DAVEC 1

    then you can use for loops to open/validate the files. Use the string in error msg's ect.

    If you need a file you can call it with the define
    Code:
    if(iChoice==MATTB)
    {
        FileArray[MATTB].fFile = fopen(  FileArray[MATTB].sFileName ,"w" );
        if(FileArray[MATTB].fFile==NULL)
            sprintf(sErrorBuffer, "File %s has failed to open.", FileArray[MATTB].sFileName );
    //ect
    This will reduce the length of code and increase readability.
    I know this may sound dumb, but how exactly would you implement that? I mean, where would you put:
    Code:
    if(iChoice==MATTB)
    {
        FileArray[MATTB].fFile = fopen(  FileArray[MATTB].sFileName ,"w" );
        if(FileArray[MATTB].fFile==NULL)
            sprintf(sErrorBuffer, "File %s has failed to open.", FileArray[MATTB].sFileName );
    and get it working? Sorry if I'm a bit confused on the subject.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Declare the struct in a header.
    I would make the array global or local to the file operations file if in a multi file project. Depends on WHERE you want it. If local write functions to return the value you need given an index and type of element (use a switch/case)

    Start by stacking/init the array. ie fill in the strings, set the FILE elements to -1 [-1 is not a possible return from fopen() ]

    The idea is to group the data. Make it easier to find.
    If you need data all you have to pass into a function is the array index. All the info relates to this element (a structure) in the array.

    Take your
    Code:
    if(c == 1)
     {
       if(!mattb)
       {
           cout << "\n\tTHIS FILE CANNOT BE FOUND.\n\n";
           exit(0);
       }
       do      /*Start screwed up "loop"*/
       {
          c = fgetc(mattb);
          putchar(c);
       }
       while(c != EOF);
       fclose(mattb);
    }
    if(c == 2)
    ect 
    ect
    ect
    and use (assuming the user entered info into the int var 'iChoice' rather than your 'c'.
    Code:
    //now the user has made the choice open the file for READING not WRITING
    FileArray[iChoice].fFile = fopen(   FileArray[iChoice].sFileName , "r" );
    if(FileArray[iChoice].fFile==NULL)
    {
       //write the error, %s is where the file name will be printed
       sprintf(sErrorBuffer, "\n\t %s FILE CANNOT BE FOUND.\n\n", FileArray[iChoice].sFileName );
       cout << sErrorBuffer; //can you do this with cout??
       Exit(0);
    }
    else
    {
       do      /*Start screwed up "loop"*/
       {
          c = fgetc(FileArray[iChoice].fFile);
          putchar(c);
       }
       while(c != EOF);
       //return should be 0 if closed properly
       FileArray[iChoice].fFile = (FILE*) fclose(FileArray[iChoice].fFile);
    }
    This code replaces your 15 long if/else.

    I does not matter which was chosen as long as the iChoice matches the array indexes you have defined.
    ie
    #define MATTB 0
    then the menu should return 0 if the user chooses Matt B. All of MATTB's info is in the zero element of the array ie FileArray[MATTB] (the same as FileArray[0])

    Try and put all the things into the struct you can. The FILE pointer, the file name, the menu strings and even a union/pointer containing the info read.

    Clear as mud?

    One last thing. I think it is best to open and close a file in the same function. Data is more likely to be corrupted in file left open at close of the exe. The exe will fail if it tries to open an open file ect.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Please, suggest a project ….
    By Dragon227Slayer in forum Tech Board
    Replies: 1
    Last Post: 06-12-2004, 10:48 AM
  3. I've been slacking off in high school, am I going to hell
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 06-01-2003, 01:00 PM
  4. Question about going to a technical school
    By Goalie35 in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:34 AM