5 Replies - 577 Views - Last Post: 09 October 2012 - 10:10 PM Rate Topic: -----

#1 michaelespiritu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-October 12

Having a problem in reading files.

Posted 09 October 2012 - 08:21 AM

Hi, im having trouble in reading files from notepad.
it seems like there\'s a problem with my code but i can\'t figure it out.

if i compare empcode to code from file nothing happens.

here\'s the code: (sorry little messy.)

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream.h>
#include <stdio.h>

void newemp();
void search();
void dtr();
void quit();


int main(){
	int choice;
	clrscr();
	cout<<"[1] Enter a new Eployee\n";
	cout<<"[2] Search\n";
	cout<<"[3] Daily time record\n";
	cout<<"[4] Quit\n";
	cin>>choice;

	
	switch (choice){
		case 1:
			newemp();
			break;
		case 2:
			void search();
			break;
		case 3:
			dtr();
			break;
		case 4:
			quit();
			break;
	}
	getch();
	return 0;
}

void newemp(){
	clrscr();
	char name[100], code[100];
	int level;
	ofstream emp("newemp.txt", ios::app);

	cout <<"Enter Employee Name: \n";
	cin>>name;

	cout<<"Enter Employee Code: \n";
	cin>>code;

	cout<<"Enter Employee Salary Level: \n";
	cin>>level;

	emp << name <<"," << code <<"," << level<<endl;
	emp.close();

	cout<<"Employee Recorded!";

	main();
}


double totalregW =0, totalotW=0, totalHolW=0, totalUnderW=0, totalLateW=0, pay, gross, gross1, gross2, sss, net, tax, late, under,sssR, taxR;
char holiday,y, empcode[50];
char days[6][10] = {"","Monday","Tuesday","Wednesday","Thursday","Friday"};
void dtr()
{
    system("cls");
    system("TITLE Payroll");
    system("COLOR 0F");

	Computation CompClass;
    char in[10],out[10],ot_in[10],ot_out[10], record[100],name[100], code [100];
    double h_in, m_in, h_out, m_out, OTh_in, OTm_in, OTh_out,OTm_out,totalH, totalM, total, totalOT,totalOTH, totalOTM,totalHoliday, totaldeducL, totaldeducU;
    int level;

    do{

	    system("cls");
	    clrscr();

			[color="#FF0000"][font="Arial Black"]cout<<"Enter Employee Code: \n";
			cin>>empcode;

	    ifstream emp("newemp.txt");


			while ( !emp.eof() ){
					emp>>record;
			       //	cout<<record;
			char *rec =strtok(record,",");
						strcpy (name,rec);
						rec = strtok(NULL, ",");
						strcpy (code, rec);
						rec = strtok(NULL, ",");
						level = atoi(rec);

				if ((empcode == code)==0){
				cout<<name<<endl;
				cout<<code<<endl;

					if (level == 1){
					pay = 350/8.0;
					sssR = .01;
					taxR = .10;
					}else if (level == 2){
					pay = 450/8.0;
					sssR = .015;
					taxR = .10;
					}else if (level == 3){
					pay = 550/8.0;
					sssR = .02;
					taxR = .10;
					}
				break;
				}else {
				cout<<"No record!";
				main();
				}
			}
[/font][/color]


here's the info from notepad
DelaCruz_Juan,A02-112,3
Smith_John,A03-113,2

Please give Feedbacks.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Having a problem in reading files.

#2 Coding Mash  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 26
  • Joined: 29-September 12

Re: Having a problem in reading files.

Posted 09 October 2012 - 08:32 AM

I could not really understand your problem.
Here is a similar question and it has useful links in it given.
Maybe it will help you.

I could not really understand your problem.
Here is a similar question and it has useful links in it given.
Maybe it will help you.
http://www.dreaminco...om-a-file-in-c/
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,858
  • Joined: 25-December 09

Re: Having a problem in reading files.

Posted 09 October 2012 - 08:41 AM

I noticed a couple of problems. First in a C++ program it is illegal to call main() from any function. Two you seem to have quite a few global variables declared in the middle of your code. These variables are only available for functions below them they can't be used by any functions defined above them. I suggest you move these declarations into main() and learn to properly pass variables to and from your functions.

Jim
Was This Post Helpful? 2
  • +
  • -

#4 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Having a problem in reading files.

Posted 09 October 2012 - 09:15 AM

It looks like you are comparing string by using == on character pointers on line 101. Essentially that line boils down to (pointer==pointer)==0 -> (false==0) -> true, universally, which is probably not what you want. Have you considered using std::string for your input? If you do that you get string comparison via ==, otherwise, you'll have to use strcmp (which is unsafe) or strncmp.

I'm assuming the compiler you are using is pretty old and probably Borland Turbo C++ or something. Unless you're stuck with that for some reason, I would seriously consider upgrading to any of a number of free modern C++ compilers--Visual C++ Studio Express, GCC, etc.
Was This Post Helpful? 1
  • +
  • -

#5 michaelespiritu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-October 12

Re: Having a problem in reading files.

Posted 09 October 2012 - 09:46 PM

im still using turbo c++ because thats what the school requires.
Here's whole main function:
int main()
{
    system("cls");
    system("TITLE Payroll");
    system("COLOR 0F");

	Computation CompClass;
    char in[10],out[10],ot_in[10],ot_out[10], name[100], code [100], record[100], empcode[50];
    double h_in, m_in, h_out, m_out, OTh_in, OTm_in, OTh_out,OTm_out,totalH, totalM, total, totalOT,totalOTH, totalOTM,totalHoliday, totaldeducL, totaldeducU;
    double level;
    //char name, code, empcode;

    do{

	    system("cls");

        ifstream emp("newemployee.txt");

        cout<<"Enter Employee Code: "<<endl;
        cin>>empcode;

        /*while ( emp  >> name >> code >> level){
            if ( empcode == code){
            cout<<name<<code<< level;
            if (level == 1){
            pay = 350/8.0;
            sssR = .01;
            taxR = .10;
            }else if (level == 2){
            pay = 450/8.0;
            sssR = .015;
            taxR = .10;
            }else if (level == 3){
            pay = 550/8.0;
            sssR = .02;
            taxR = .10;
            }
            }
        }*/
		while ( !emp.eof()){
			emp>>record;
			//cout<<record;
			
            char *rec =strtok(record,",");
            strcpy (name,rec);
            rec = strtok(NULL, ",");
            strcpy (code, rec);
            rec = strtok(NULL, ",");
            level = atof(rec);
            // The problem is here, it seems that my code is not scanning or reading the whole file. 
What i want to happen is that, the empcode will scan the whole file and if it finds a match, 
the program will use that info. but in this code nothing happens.//
            if (empcode == code){
		clrscr();
                cout<<"Name      : "<<name;
		cout<<"Emp. Code : "<<code;
            
				if (level == 1){
				pay = 350/8.0;
				sssR = .01;
				taxR = .10;
				}else if (level == 2){
				pay = 450/8.0;
				sssR = .015;
				taxR = .10;
				}else if (level == 3){
				pay = 550/8.0;
				sssR = .02;
				taxR = .10;
				}
			}else{
				cout<<"No record found!";
			}
		}

            for (int x = 1; x <=2; x++){
            cout    <<"\n|:::::::::::::::::::::::::::::::::::::::::::::::::|"<<endl;
            cout    <<"\nEnter Time in for "<<days[x]<<"\t\t";
            cin     >> in;

                h_in    = CompClass.workH(in);
                m_in    = CompClass.workM(in);
                    //resets the time to 8:00 if the user's in is earlier than 8:00
                    if (h_in <8){
                        h_in = 8;
                        m_in = 00;
                        }
                    //resets the time to 13:00 if the user's in is earlier than 13:00
                    if (h_in >= 12 && h_in<13){
                        h_in = 13;
                        m_in = 00;
                    }
                    /*//grace period of 15 for the 8:00 in
                    if (h_in <= 8 && m_in <=15){
                        m_in = 00;
                    }*/

            cout    <<"Enter Time out for "<<days[x]<<"\t\t";
            cin     >> out;

                h_out   = CompClass.workH(out);
                m_out   = CompClass.workM(out);
                //resets the time to 17:00 if the user's out is more than 17:00
                if (h_out>=17&&h_out>=13){
                    h_out = 17;
                    m_out = 00;
                }
                //resets the time to 12:00 if the user's out is more than 12:00
                else if (h_out>=12&&h_out<13){
                    h_out = 12;
                    m_out = 00;
                }

            cout    <<"Is "<<days[x]<<" a holiday?(y or n)\t";
            cin     >>holiday;

		    if (holiday == 'y'||holiday == 'Y'){

                        totalH        = CompClass.h_comp(h_out,h_in);
                        totalM        = CompClass.h_comp(m_out,m_in);
                        totalOT       = CompClass.tHour(totalH, totalM, h_out, h_in);
                        total         = 0;
                        //deduction for late
                        totaldeducL = CompClass.hLate(h_in, m_in, h_out, totalOT);
                        //deduction for undertime
                        totaldeducU = CompClass.hUnder(h_in, h_out, m_out, totalOT);

                            if (totalOT ==0){
                                cout<<"\n\t\t\tABSENT\n";
                            }else{
                            cout<<"\nTotal working hours for "<<days[x]<<"(HOLIDAY):  \t"<<total;
                            cout<<"\nTotal Overtime hours for "<<days[x]<<"(HOlIDAY): \t"<<totalOT<<endl;
                            }

                        } else {

                            cout    <<"Enter Overtime in for "<<days[x]<<"\t";
                            cin     >>ot_in;

                            OTh_in    = CompClass.workH(ot_in);
                            OTm_in    = CompClass.workM(ot_in);

                            cout    <<"Enter Overtime out for "<<days[x]<<"\t";
                            cin     >>ot_out;

                            OTh_out     = CompClass.workH(ot_out);
                            OTm_out     = CompClass.workM(ot_out);

                                if (OTh_in <= 17 && OTm_in <= 30 && OTh_out>=20){
                                    OTh_in = 17;
                                    OTm_in = 30;
                                }

                                if (OTh_out >= 20 && OTm_out >=30){
                                    OTh_out = 20;
                                    OTm_out = 30;
                                }

                            totalH      = CompClass.h_comp(h_out,h_in);
                            totalM      = CompClass.h_comp(m_out,m_in);

                            totalOTH    = CompClass.h_comp(OTh_out, OTh_in);
                            totalOTM    = CompClass.h_comp(OTm_out, OTm_in);

                            total       = CompClass.tHour(totalH, totalM, h_out, h_in);
                            totalHoliday = 0;

                            //deduction for late
                            totaldeducL = CompClass.hLate(h_in, m_in, h_out, total);

                            //deduction for undertime
                            totaldeducU = CompClass.hUnder(h_in, h_out, m_out, total);

                                 if (total == 0){
                                cout<<"\n\t\t\tABSENT\n";
                                totalOT = 0;
                                }else{
                                cout<<"\nTotal working hours for "<<days[x]<<":  \t"<<total;
                                totalOT        = totalOTH + totalOTM / 60;
                                cout<<"\nTotal Overtime hours for "<<days[x]<<": \t"<<totalOT<<endl;
                                }
                        }
                //resets the overall working hours
                totalregW   += total;
                totalotW    += totalOT;
                totalHolW   += totalHoliday;
                totalUnderW += totaldeducU;
                totalLateW  += totaldeducL;


                gross1 = pay*totalregW;
                gross2 = pay*totalotW;

                late = pay*totalLateW;
                under = pay*totalUnderW;

		gross = gross1 + gross2;

		sss = gross * sssR;

		tax = gross * taxR;

		net = gross - (sss + tax);
            }
        cout    <<"\n|=================================================|";
        cout    <<"\n|:::::::::::::::::::::::::::::::::::::::::::::::::|";
        cout    <<"\n|=================================================|";
        cout<<"\n\nTotal working hours for this week: \t"<<totalregW<<endl;


        if (totalotW !=0){
        cout<<"Total Overtime for this week: \t   \t"<<totalotW<<endl;
        }

        /*if (totalHolW !=0){
        cout<<"Total Holiday hours for this week: \t"<<totalHolW<<endl;
        }*/

        if (totalUnderW !=0 ){
        cout<<"Total Undertime for this week:     \t"<<totalUnderW<<endl;
        }

        if (totalLateW !=0 ){
        cout<<"Total late for this week:          \t"<<totalLateW<<endl;
        }
        cout<<"Total Gross pay:                   \t"<<gross<<endl;
        cout<<"Total Dedction (SSS):"<<endl;
        cout<<"\tSSS / GSIS: \t"<<sss<<endl;
        cout<<"\tTAX:        \t"<<tax<<endl;
        if (late != 0){
        cout<<"\tLate:       \t"<<late<<endl;
        }
        if (under != 0){
        cout<<"\tUndertime:  \t"<<under<<endl;
        }
        cout<<"Total Net pay:                     \t"<<net<<endl;

        cout<<"\n\n\tWANT TO QUIT? (y or n): \n\t";
        cin>>y;

		if (y == 'y' || y == 'Y'){
                cout<<"\n\t\tGoodbye!\n\n";
                }

            else{
            totalregW = 0;
            totalotW = 0;
            totalHolW = 0;
            totalUnderW =0;
            totalLateW =0;
            }

	}while(y == 'n' || y == 'N');

    //getch ();
    return 0;
}


newemp.txt includes
DelaCruz_Juan,A02-112,3
Smith_John,A03-113,2

Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,858
  • Joined: 25-December 09

Re: Having a problem in reading files.

Posted 09 October 2012 - 10:10 PM

First I recommend that you use a combination of getline() and the extraction operator>> to parse your string instead of the strtok() function. First extract the name using getline() using the optional third parameter, then use getline() again with the optional third parameter to extract the code, and finally use the extraction operator to extract the level. Use the comma as the third parameter of getline():

cin.getline(name,NameSize, ','); // NameSize is the size of your C-string name.
cin.getline(code,CodeSize, ',');
cin >> level;


I also suggest that you read the entire file into an array of a structure that holds these three items.

By the way your code is so messy that it is almost impossible for anyone not familiar with your requirements to make any sense of the code. You need to think about breaking your large main into several smaller functions and find an indentation style you like and use it consistently.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1