Thread: Printing words to console screen

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    21

    Printing words to console screen

    hello, i am writing a payroll system or a attempt at it anyway. i have put in records in the system via a addrecords function then when to edit it, i want the user to be able to see which record they will be changing after choosing the number to edit, I am very new to programming, just looking for some advice for this.

    heres the code for the part of the edit function i have a problem with excluding the rest of the program
    Code:
    		printf("Enter record number to be editted\n");
    		scanf("%d", &employee.num);
    
    		//moving file pointer to position of record user requested
    		fseek(writeptr, (employee.num-1) * sizeof(struct employee_data), SEEK_CUR);
    		
    		fscanf(writeptr, "%-10d%-15s%-15s%-12d%-12.2lf%-8d%-12.2lf%-12.2lf%-12.2lf%-12.2lf%-14.2lf%-12.2lf\r\n" , employee.num, employee.first_name, employee.last_name, employee.id_number, employee.pay_rate, 
    		employee.year, employee.sthours, employee.one_and_half_hour, employee.double_hour, employee.gross_pay, employee.deductions, employee.net_pay);
    		
    		printf("%-10s%-15s%-15s%-12s%-10s%-10s%-12s%-12s%-12s%-12s%-14s%-12s\r\n", "number", "First name", "Last name", "id no.", "pay rate", 
    		"year", "sthours", "1.5 hours", "2x hours", "Gross pay", "deductions", "net Pay");
    		printf("%-10d%-15s%-15s%-12d%-12.2lf%-8d%-12.2lf%-12.2lf%-12.2lf%-12.2lf%-14.2lf%-12.2lf\r\n" , employee.num, employee.first_name, employee.last_name, employee.id_number, employee.pay_rate, 
    		employee.year, employee.sthours, employee.one_and_half_hour, employee.double_hour, employee.gross_pay, employee.deductions, employee.net_pay);
    the problem is that it reads the records from the file and then prints it to the screen but it does not print the last and first name , it prints all the only numerical fields. it writes the information fine to the file with the first and last name there but not when reading from file. Can anyone help me with this? if needed i can upload the entire code so you can look through it or the entire edit function but it is pretty long...

    thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You aren't using the & in front of your number input - you need to fix that.

    I am unfamiliar with, your use of the formatting numbers, being in the fscanf(). Those are useful for printing, but all those minus signs are very strange looking in an fscanf() call. Outside of making your fscanf() line of code, very long and odd looking, they are not necessary. That is for output.

    If that isn't the problem, post back enough code and input example, so we can run it and see what's doing, there.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    Quote Originally Posted by Adak View Post
    You aren't using the & in front of your number input - you need to fix that.

    I am unfamiliar with, your use of the formatting numbers, being in the fscanf(). Those are useful for printing, but all those minus signs are very strange looking in an fscanf() call. Outside of making your fscanf() line of code, very long and odd looking, they are not necessary. That is for output.

    If that isn't the problem, post back enough code and input example, so we can run it and see what's doing, there.
    ok which number input? i have one before the employee.num, thats the only one necessary right?

    i saw in a C book i have, they use the scanf() like that with all the minuses,will try removing them and see if they are useless.

    i will just upload my code, its too much to paste into a post. the part of the code i pasted is from the edit function, you have to use the create and add functions to use the edit function.

    thanks for your help and quick reply.
    Last edited by Shadow20; 04-07-2010 at 06:32 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Stop!

    Your problem is just with the lack of an ampersand '&' in your scanf() for numbers. All numbers need the address of operator, before their name in a scanf() line of code.

    Strings do not - the string name is it's own address.

    scanf("%d", &employee.num);

    That is right. The fscanf() calls for numbers, which lack an ampersand, are wrong.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    but i dont have any problems with the numbers its the strings i have a problem with... the strings wont output to the screen.
    ok will add the & to the fscanf() thanks.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Shadow20 View Post
    but i dont have any problems with the numbers its the strings i have a problem with... the strings wont output to the screen.
    ok will add the & to the fscanf() thanks.
    Could be just luck, I'm not sure, but I KNOW that you can't change a variable with scanf() or fscanf() without having passed the address to those functions.

    If you just want to print out or compare a variable, you don't have to have it's address. But if you want to change it's value, using the scanf() family of functions, you must have it's address.

    I can only guess as to why it's working for you right now, somewhat.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    I added in the ampersands in the fscanf here:
    Code:
    fscanf(writeptr, "%10d%15s%15s%12d%12.2lf%8d%12.2lf%12.2lf%12.2lf%12.2lf%14.2lf%12.2lf\r\n" , &employee.num, employee.first_name, employee.last_name, &employee.id_number, &employee.pay_rate, 
    		&employee.year, &employee.sthours, &employee.one_and_half_hour, &employee.double_hour, &employee.gross_pay, &employee.deductions, &employee.net_pay);
    but it still gives the error of not showing the output for the strings last name and first name... any idea why the strings arent printed?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Add this, and tell me what it prints:

    printf("%d", sizeof(employee.first_name));

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    I run it and it output 8.. i temporarily removed my printf statements to see it clear
    i placed it right after the fscanf statement.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Ok, so you've got room for 7 bytes and 1 byte for the end of string char. Is the "writeptr" OK to be reading from this file?

    Post up first 3 lines of your input file, please.

    Note that instead of something like this:
    fscanf(writeptr, "%10d%15s%15s%12d%12.2lf%8d%12.2lf%12.2lf%12.2lf%1 2.2lf%14.2lf%12.2lf\r\n" , etc);

    you can use:
    fscanf(writeptr, "%d%15s%15s%d%lf%d%lf%lf%lf%lf%lf%lf\r\n" , etc.);

    All those 10's and 12.2 and other digits for numerical input, are meaningless. You don't limit the input size of numbers. Only strings. You don't format the numbers with scanf(), you format them with your printf or other output statement.
    Last edited by Adak; 04-07-2010 at 07:05 PM.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    not sure what you mean by input file, sorry
    but here is the first 3 lines from the edit function
    Code:
            int editchoice=1;
    	
    	//initialize file pointer to edit files
    	FILE *writeptr;
    	
    	//define struct variable employee
    	struct employee_data employee;

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You ARE reading in data from a file, correct?

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    yes, i made the file with another part of the program then read from it in this function. the file is named payroll.txt

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I need the first 3 lines of that file

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    there is only two lines, its how i wrote the program to create a formatted file with the headings then create a record.

    Code:
    number    First name     Last name      id no.      pay rate  year      sthours     1.5 hours   2x hours    Gross pay   deductions    net Pay     
    1         p              w              1           1.00        1       1.00        1.00        1.00        4.50        0.43          4.07
    was just using random data to test it.

    Quote Originally Posted by Adak View Post

    Note that instead of something like this:
    fscanf(writeptr, "%10d%15s%15s%12d%12.2lf%8d%12.2lf%12.2lf%12.2lf%1 2.2lf%14.2lf%12.2lf\r\n" , etc);

    you can use:
    fscanf(writeptr, "%d%15s%15s%d%lf%d%lf%lf%lf%lf%lf%lf\r\n" , etc.);

    All those 10's and 12.2 and other digits for numerical input, are meaningless. You don't limit the input size of numbers. Only strings. You don't format the numbers with scanf(), you format them with your printf or other output statement.
    wow, thanks, that will save me some trouble, always thought i had to put all those numbers!
    Last edited by Shadow20; 04-07-2010 at 07:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. no. printing in words
    By rits in forum C Programming
    Replies: 2
    Last Post: 09-09-2009, 10:11 AM
  2. Faster way of printing to the screen
    By cacophonix in forum C Programming
    Replies: 16
    Last Post: 02-04-2009, 01:18 PM
  3. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  4. question about printing on screen and not overwriting text...
    By revelation437 in forum C Programming
    Replies: 2
    Last Post: 12-14-2002, 02:48 PM
  5. Console Screen Resolution!!!!
    By Perica in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2002, 07:45 AM