Welcome to Dream.In.Code
Become a C++ Expert!

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




How to create multiple pages in C

 
Reply to this topicStart new topic

How to create multiple pages in C

Daisygirl
15 Jan, 2008 - 03:09 PM
Post #1

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 11

I am creating a payslip but I need it to be on more than one page. how do I show the data on more than one page on the screen?? rolleyes.gif
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);
        }
        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 - please post source code using the code tags, like this: code.gif
User is offlineProfile CardPM
+Quote Post

Pontus
RE: How To Create Multiple Pages In C
16 Jan, 2008 - 06:55 AM
Post #2

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 544



Thanked: 4 times
Dream Kudos: 275
My Contributions
What do u mean with a page?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: How To Create Multiple Pages In C
16 Jan, 2008 - 07:09 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
I believe the OP is referring to only displaying a certain amount of information on the screen at the same time - if there are 50 results, display a set of ten, then prompt if the user wants to see the next ten. the concept is called paging.
User is online!Profile CardPM
+Quote Post

Daisygirl
RE: How To Create Multiple Pages In C
16 Jan, 2008 - 07:29 AM
Post #4

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 11

Yes Amadeus, dtas exactly wot I mean.

I need to display around 5 results per page.

how is it done please?
User is offlineProfile CardPM
+Quote Post

Pontus
RE: How To Create Multiple Pages In C
16 Jan, 2008 - 08:52 AM
Post #5

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 544



Thanked: 4 times
Dream Kudos: 275
My Contributions
Well, after u draw all ur lines do this.
CODE

cin.get();//get a enter
system("cls");// clear screen


This will only work in Windows/Dos enviroments!
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: How To Create Multiple Pages In C
16 Jan, 2008 - 09:25 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
Actually, you'll need to encapsulate the display process in a loop. Use a counter to track how many records you have displayed. After a certain number, pause the loop, but do not reset the counter. Prompt for input and continue.
User is online!Profile CardPM
+Quote Post

Daisygirl
RE: How To Create Multiple Pages In C
16 Jan, 2008 - 03:58 PM
Post #7

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 11

It would be easier if the user was inputting data but they are not.

my program reads from a file that has data in it already. so i just want it to display on different 'pages'.

do i have to put the loop in the program that is writing the data???
User is offlineProfile CardPM
+Quote Post

Bench
RE: How To Create Multiple Pages In C
16 Jan, 2008 - 04:33 PM
Post #8

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
It would be even easier if there was no input at all, from files or the user - so why not write your program to start out with, that doesn't take any input, and uses test data hard-coded into your program instead? (I'd suggest storing it in an array)

The advantage of this approach is that it allows you to experiment with your output formatting, so you can get that bit right before you build in the file input.
- This is essentially a divide-and-conquer approach to software development - the main goal is to break the problem into smaller, managable chunks, and solve one easy bit at a time, instead of trying to chew the whole thing at once.



Edit - fixed typing error

This post has been edited by Bench: 16 Jan, 2008 - 04:58 PM
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: How To Create Multiple Pages In C
16 Jan, 2008 - 04:34 PM
Post #9

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,589



Thanked: 12 times
Dream Kudos: 325
My Contributions
Unrelated to what you're asking, but a general suggestion for your code: make and use a payslip structure instead of individual variables.

Example:
CODE

typedef struct
{
    char* name;
    float hours;
} payslip;

payslip tom;

tom.name = "Tom"
tom.hours = 5.0;

User is online!Profile CardPM
+Quote Post

Daisygirl
RE: How To Create Multiple Pages In C
17 Jan, 2008 - 02:02 AM
Post #10

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 11

first of all Bench i have sum sort of idea what you're talkin abt regarding doin little chunks but the rest of what you wrote doesnt really make sense. i mean i didnt understand. Plainer english please and an example would help 2.

my first program writes to a file. i have to enter the employees details.
d second one (which is the one that ive already given d code for) reads from it.
All i need is to be told how to display on more than one 'page'. I didnt think it wud be this hard,altho i am new to C.

if you want 2 see the code for program where it write to a file, den tel me so.



hey Tom i dnt think dis wud work wiv wot I am doing. cos i wud have to insert this into my other program that inputs data into a file.

i think it sort of complicates things more. but den again i dnt know much programming.

QUOTE(Tom9729 @ 16 Jan, 2008 - 05:34 PM) *

Unrelated to what you're asking, but a general suggestion for your code: make and use a payslip structure instead of individual variables.

Example:
CODE

typedef struct
{
    char* name;
    float hours;
} payslip;

payslip tom;

tom.name = "Tom"
tom.hours = 5.0;



User is offlineProfile CardPM
+Quote Post

Bench
RE: How To Create Multiple Pages In C
17 Jan, 2008 - 07:12 AM
Post #11

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(Daisygirl @ 17 Jan, 2008 - 10:02 AM) *

first of all Bench i have sum sort of idea what you're talkin abt regarding doin little chunks but the rest of what you wrote doesnt really make sense. i mean i didnt understand. Plainer english please and an example would help 2.

my first program writes to a file. i have to enter the employees details.
d second one (which is the one that ive already given d code for) reads from it.
All i need is to be told how to display on more than one 'page'. I didnt think it wud be this hard,altho i am new to C.

It doesn't need to be difficult - The hard part in programming is deciding what exactly you want the computer to do for you. it doesn't understand what a 'Page' is, so you need to think in terms of something which it can understand, for example, a pair of blank lines.
- Obviously, you don't want a page after every single record, so a page becomes a special case, which only happens after a certain number of records have been output. One way to do this, is to keep a variable which acts as a line-counter; Starting at zero, for every line you output, add 1 to the counter.
- If the counter is equal to 5, print the blank lines, then reset the counter to zero.


I assume you also want some kind of response from the user, who may decide whether to stop or continue to the next page. This can come at the same time as printing the blank lines.

You can do all of this without having any file input. You might write the code, to just output a line saying "This is a test". Just to make sure that you've got the paging logic working the way you want it.
CODE
int main()
{
    int count = 0;
    int i;

    /*instead of a while loop which reads a file
     * use a for loop to test page breaks */
    for( i=0; i < 15; i++ )
    {
        printf("This is a test\n");
        count++;

        /* Page break after every fifth repetition */
        if( count == 5 )
        {
            /* Reset counter, print 2 newlines */
            count = 0;  
            printf("\n\n");
        }

    }
    return 0;
}

User is offlineProfile CardPM
+Quote Post

Daisygirl
RE: How To Create Multiple Pages In C
20 Jan, 2008 - 07:57 PM
Post #12

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 11

thanks bench

i managed to figure it out.....i had a lil problem with where to place the codin....but it works now

thanks a lot
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 02:36PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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