Thread: Preparation for coding exam: I'm having issues trying to print data correctly.

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    6

    Preparation for coding exam: I'm having issues trying to print data correctly.

    I've been learning C for a few months now in college and exams are coming up so I thought I'd look at some past papers.
    I'm trying to get the variable YTH to print properly but at the moment it just displays zeros. If anyone could fix this and explain what is wrong that would be of great help.
    The question just asks for the tax to be calculated and that the memory be updated with the new information.

    This is the code I have made: income.txt2011b.c

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The file that you attached appears to contain input or output rather than C code. I suggest that you post your code directly in [code][/code] bbcode tags.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    It's look that there is only one link, but there are two links.
    Anyway, here is the code from him/her:
    Code:
    #include <stdlib.h>
    #include <stdio.h>    //included libraries
    #include <string.h>
    
    typedef struct wages //variables used
    {
      	char surname[20];
      	char first[20];
      	int emp_income; //Employee income per year
        int taxcred; //tax credit
      	int YIT; //yearly income tax
        int YTH; //yearly take home 
    
    } tax_info;
    
    void file_output(tax_info tax);
    int calc_YIT(int emp_income);
    int calc_YTH(int emp_income, int YIT, int taxcred);
    int user_menu();
    
    int main () //main function
    {
    	FILE* fp;
    	int tax_no = 0;
    	char ch;
    	tax_info wages[10000];
    	int option;
    
    	option = user_menu();
    	while (option != 4)
    	{
    		if (option == 1) //when user presses one, this initiates
    		{
    			fp = fopen("income.txt", "r"); //opens file with name indata.txt
    
    			while(!feof(fp)) //scans through the file
    			{
    				fscanf(fp, "%s%c%s%c%d%c%d%c", &wages[tax_no].surname, &ch, &wages[tax_no].first, &ch,
                        &wages[tax_no].emp_income, &ch, &wages[tax_no].taxcred, &ch);
    
    								printf("%s %s %d %d\n", wages[tax_no].surname,
                    wages[tax_no].first, wages[tax_no].emp_income,
                    wages[tax_no].taxcred);
    
                            //accesses get_offer function to calculate new price
                            wages[tax_no].YIT = calc_YIT( wages[tax_no].emp_income);
    				                        	tax_no++;
                                      wages[tax_no].YTH = calc_YTH( wages[tax_no].emp_income, wages[tax_no].YIT, wages[tax_no].taxcred);
              				                        	tax_no++;
    			}
    			fclose(fp); //closes
    		}
    		else
    		{
          int i;
    			for(i = 0; i < tax_no; i++) //structured array
    			{
    				if (option == 2) //when user presses 2, this initiates
    				{
                    			printf("%s %s EUR %d EUR %d EUR %d \n", wages[i].surname,
                                  wages[i].first, wages[i].emp_income, wages[i].YIT, wages[i].YTH); //prints results to screen
    				}
    				else if (option == 3) //when user presses 3, this initiates
    				{
    
                          file_output(wages[i]);
    				}
     			}
    		}
    		option = user_menu();
    	}
    	return (EXIT_SUCCESS); //return success
    }
    
    int user_menu() //function for the user menu
    {
          	int option = 0;
            while (option < 1 || option > 4) //loops through 1 - 4
            {
    		printf("\n\tChoose An Option:\n");
    		printf("\t[1] Read Data From File\n");
    		printf("\t[2] Print Data In Memory On Screen\n");
    		printf("\t[3] Print Data In Memory To File\n");
    		printf("\t[4] Quit Program\n\n");
    		scanf("%d", &option);
    		printf("\n");
            }
    	return option;
    }
    
    int calc_YIT(int emp_income){
      int YIT;
      if (emp_income < 32000){
        YIT = 0.2 * emp_income;
      }
      else{
        YIT = (0.2 * emp_income) + ((0.42) * (emp_income - 32000));
      }
      return YIT;
    }
    
    int calc_YTH(int emp_income, int YIT, int taxcred){
      int YTH;
    
      YTH = (emp_income + taxcred) - YIT;
      return YTH;
    }
    
    void file_output(tax_info tax) //function for outputting file into a .txt file
    {
    
            FILE* fp;
            fp = fopen("out2011.txt", "a");
            fprintf(fp, "%s %s %d %d %d \n", tax.surname, tax.first, tax.emp_income, tax.YIT, tax.YTH);
            fclose(fp);
            return;
    }
    And this is the text file:
    Code:
    Moore Paul 30000 3120
    ODonnell John 12000 3120
    Murphy William 65000 4750
    Bush Sean 45000 5000
    Murphy Denise 15000 3500
    OBrien Mary 100000 5000
    OConnor Julia 16000 3500
    Quinn Judy 25000 5000
    Other have classes, we are class

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
                            //accesses get_offer function to calculate new price
                            wages[tax_no].YIT = calc_YIT( wages[tax_no].emp_income);
                                                tax_no++;
                                      wages[tax_no].YTH = calc_YTH( wages[tax_no].emp_income, wages[tax_no].YIT, wages[tax_no].taxcred);
                                                        tax_no++;
    So why do you increment tax_no twice?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2015
    Posts
    6
    thank you for that WoodSTokk.
    I incremented it twice as I thought it was needed for both calc_YIT and calc_YTH.

  6. #6
    Registered User
    Join Date
    May 2015
    Posts
    6
    I forgot to reply saying that it now functions. It was because of tax_no being incremented twice. So, thank you Salem for pointing that out to me.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    while(!feof(fp)) //scans through the file
    {
        fscanf(fp, "%s%c%s%c%d%c%d%c", &wages[tax_no].surname, &ch, &wages[tax_no].first, &ch,
               &wages[tax_no].emp_income, &ch, &wages[tax_no].taxcred, &ch);
    Read: FAQ > Why it's bad to use feof() to control a loop
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having issues with indexing correctly in a file
    By McFury in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2008, 10:55 AM
  2. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  3. Data Structure exam help
    By ExCoder01 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-23-2003, 11:26 AM
  4. preparation
    By acskpad in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-09-2003, 04:24 AM

Tags for this Thread