Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,002 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,381 people online right now. Registration is fast and FREE... Join Now!




if statements to read strings

 
Reply to this topicStart new topic

if statements to read strings

Daisygirl
16 Jan, 2008 - 02:04 AM
Post #1

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 11

hi i would like to ask whether anyone knows how to create an if statement to read a word and then set a count for every time the word appears. it works perfectly fine with integers but it won't read the word that i want it to.

CODE
#include <stdio.h>
#include <conio.h>

char name[20];
int empno;
char dept;
float gpay;
float npay;
float tax;
float ni;
float pension;
float taxtotal;
float hours;
float rate;
int admin;
int sales;
int accounts;
int emps;
float gpays, npays, taxes, nis, pensions;

FILE *empsPtr;

void main()
{
clrscr();

if((empsPtr=fopen("e:employee.dat","r"))==NULL) {
    printf("File is empty\n");
    }
else{
    textcolor(BLUE);
    cprintf("\t\tABC Company Employee Report");
    printf("\n");
    printf("\n");
    cprintf("%-10s%15s%8s%12s%6s%10s%10s%8s\n","Name","Employee No.","Depart","Gross Pay","Tax","Health","Pension","Net Pay");
    printf("-------------------------------------------------------------------------------\n");
    fscanf(empsPtr,"%s%d%s%f%f",name,&empno,dept,&hours,&rate);

    while(!feof(empsPtr)){

        gpay=hours*rate;
        tax=gpay*0.155;
        ni=gpay*0.03;
        pension=gpay*0.02;
        taxtotal=tax+ni+pension;
        npay=gpay-taxtotal;
        gpays+=gpay;
        npays+=npay;
        taxes+=tax;
        nis+=ni;
        pensions+=pension;
        printf("%-13s\t%d\t%8s\t%5.2f\t%3.2f\t%3.2f\t%3.2f\t%5.2f\n",name,empno,dept,gpay,tax,ni,pension,npay);
        fscanf(empsPtr,"%s%d%s%f%f",name,&empno,dept,&hours,&rate);
        }
if(dept=="Accounts") //important stuff apparently starts here
      { accounts++;
       }
else if(dept=="Sales")
       { sales++;
       }
else { admin++;
       }//important stuff apparently ends here
        textcolor(RED);
        printf("\n");
        printf("--------------------------------------------------------------------------------\n");
        printf("\t\t\t\t\t%5.2f\t%4.2f\t%4.2f\t%4.2f\t%5.2f\n",gpays, taxes, nis, pensions, npays);
        printf("\n");
        printf("Total number from Admin Department: %d\n",admin);
        printf("Total number from Sales Department: %d\n",sales);
        printf("Total number from Accounts Department: %d\n",accounts);
        printf("\n");
        printf("Total number of employees: %d\n",sales+accounts+admin);

        }
        fclose(empsPtr);

        }



*mod edit - added code tags and removed a few other random bbcode tags
User is offlineProfile CardPM
+Quote Post

MorphiusFaydal
RE: If Statements To Read Strings
16 Jan, 2008 - 02:13 AM
Post #2

D.I.C Lover
Group Icon

Joined: 12 May, 2005
Posts: 1,098



Thanked: 8 times
Expert In: Hardware, Networking

My Contributions
Please put your code in code.gif tags - makes it about one thousand times easier to read.

And, without compiling it (I am without compiler, ATM), your dept variable is declared as type char. You will either need to declare it as a C-style string, rather than a normal char. You're already using a C-style string for name, so declare and fill it just like that. To compare it, you can't use ==, you need to use strcmp().

Example:

CODE
char[6] = "Allan";

if( strcmp( name, "John") == 0 ) {
    printf("Hey dude.\n");
}
else if( strcmp( name, "Allan" ) == 0 ) {
    printf("Good morning.\n");
}
else {
    printf("Who on Earth are you?\n");
}


HTH, Chris

This post has been edited by MorphiusFaydal: 16 Jan, 2008 - 02:17 AM
User is offlineProfile CardPM
+Quote Post

Daisygirl
RE: If Statements To Read Strings
20 Jan, 2008 - 04:34 PM
Post #3

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 11

its not working.

when i set department name to Accounts for instance......everything is read as accounts.
remember that this program reads from a file that is created from another program also created in c.

is there any other way??
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 12:24PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month