Thread: Repetitive data (Noob)

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23

    Repetitive data (Noob)

    I have this code:
    Code:
    	for (k; k<4; k++)
    	{
    		printf ("Please enter the category code\n");
    		scanf("%d", &cat[i].id);
    		
    		printf ("Please enter the category description\n");
    		scanf ("s", cat[i].description);
    		
    		printf ("Please enter the category VAT rate\n");
    		scanf ("%f", &cat[i].rate);
    		
    		system ("cls");
    	}
    This data i enter hear is never going to change. It is predefined. Is there anyway i can get this to automatically enter itself without using a file to load it into the array?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes.

    use a initializer, e.g.:

    Code:
    const struct category cat[4] = { { 1, "Text1", 0.11 }, 
                                              { 2, "Text2", 0.22 },          
                                              { 3, "Text3", 0.33 },          
                                              { 4, "Text4", 0.44 } };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    I must implement this next. Why wont my displays work will someone tell me? Everything is compiling correct and all but it wont print the data for me. Please help!!!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    
    
    struct category
    {
    	int id;
    	char description[25];
    	float vatrate;
    	
    };
    
    struct product
    {
    	
    	char  productname[25];
    	float price;
    	int catid;
    	
    };
    
    typedef struct product P;
    typedef struct category C;
    
    void welcome();
    int basket(C array1[], P array2[]);
    void till(int num,P array2[]);
    
    int main()
    {
    	int k = 0;
    	int i = 0;
    	int size;
    
    	const int limit = 10;
    
    	C cat[limit];
    	P prod[limit];
    
    	welcome();
    	
    	for (k; k<4; k++)
    	{
    		printf ("Please enter the category code\n");
    		scanf("&#37;d", &cat[i].id);
    		
    		printf ("Please enter the category description\n");
    		scanf ("%s", cat[i].description);
    		
    		printf ("Please enter the category VAT rate\n");
    		scanf ("%f", &cat[i].vatrate);
    		
    		system ("cls");
    	}
    	printf("Category Code\t\tCategory\t\t\t\tVAT%\n");
    		
    	for(i;i<4;i++)
    	{
    		printf("%d\t\t\t%s\t\t\t\t%f.1f\n\n",cat[k].id,cat[k].description,cat[k].vatrate);
    	}
    	
    	size=basket(cat,prod);
    	till(size,prod);
    }
     
    
    
    int basket(C array1[], P array2[])
    {
    	int k, num=0, code;
    	
    	printf ("How many items are being purchases\n");
    	scanf ("%d", &num);
    	
    	for(k=0;k<num;k++)
    	{
    		printf("Please enter product name\n");
    		scanf ("%s", array2[k].productname);
    		
    		printf ("Please enter a price of the product\n");
    		scanf("%f", &array2[k].price);
    		
    		printf ("Please enter the category code\n");
    		scanf ("%d", &code);
    		
    		array2[k].catid = array1[ code ].vatrate;
    	}		
    	
    	if(code>3||code<0)//if the user types in an invalid category code it prints out this message	
    	{
    		printf("No such code exists");		
    	}
    	
    	return num;
    }
    
    
    void till(int num,P array2[])
    {
    		int q=0;
    	float vat;//the actual vat due is stored here
    	float cost;//this is the total cost
    	
    	
    	printf("Item\t%VAT\tActual Vat\tCost\n");
    	
    	for(q;q<num;q++)//this for loop calculates the vat rate and prints out a table of information
    	{
    		vat=((array2[q].price/100)*array2[q].catid);//this calculates the vat due from each product
    		cost=(vat + array2[q].price);//this calculates the total cost of each product including VAT
    		printf("%s\t%2.0f\t%2.2f\t\t%3.2f\n",array2[q].productname,array2[q].catid,vat,cost);		
    	}
    }
    
    
    void welcome()
    {
    	printf ("\t\t\tWelcome\n");
    }
    Last edited by Salem; 11-30-2007 at 09:29 AM. Reason: Fixed the array indexed by code to de-flummox the code tags

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    for (k; k<4; k++)
    {
    printf ("Please enter the category code\n");
    scanf("&#37;d", &cat[i].id);
    
    printf ("Please enter the category description\n");
    scanf ("%s", cat[i].description);
    
    printf ("Please enter the category VAT rate\n");
    scanf ("%f", &cat[i].vatrate);
    
    system ("cls");
    }
    In the above for-loop, you never initialize k, so it starts at some unknown value. In addition k is your loop variable so why are indexing the array with i instead? To give k some initial condition (like 0), use:
    Code:
    for (k=0; k<4; k++)
    Code:
    for(i;i<4;i++)
    {
    printf("%d\t\t\t%s\t\t\t\t%f.1f\n\n",cat[k].id,cat[k].description,cat[k].vatrate);
    }
    
    size=basket(cat,prod);
    till(size,prod);
    }
    In the above for-loop i is never initialized. And you use k versus i to index the array.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Actually it looks like you did initialize k and i when you declared them, but it's better to initialize them inside the for-loop construct.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    Code:
    void till(int num,P array2[])
    {
    	int q=0;
    	float vat;//the actual vat due is stored here
    	int cost;//this is the total cost
    	
    	
    	printf("Item\t&#37;VAT\tActual Vat\tCost\n");
    	
    	for(q;q<num;q++)//this for loop calculates the vat rate and prints out a table of information
    	{
    		vat=((array2[q].price/100)*array2[q].catid);//this calculates the vat due from each product
    		cost=(vat + array2[q].price);//this calculates the total cost of each product including VAT
    		printf("%s\t%2.0f\t%2.2f\t\t%3.2f\n",array2[q].productname,array2[q].catid,vat,cost);		
    	}
    }
    This function still is returning rubbish to me for the int values? Why is this the case?

    Also how can i call the struct like matsp said into my program to auto load the data?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Also how can i call the struct like matsp said into my program to auto load the data?
    Just substitute the line matsp posted in place of this line at the top of main():
    Code:
    C cat[limit];
    You can verify the values are stored there by printing them as you do now.
    Last edited by swoopy; 11-29-2007 at 06:34 PM.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    i dont understand. How can this hold my constant 4 values? they will always be
    1 hardware 21
    2 electrical 21
    3 appliance 18
    4 food 0
    this is the cat code, the cat and the vate rate. it would just be faster not to have to talk this data in every time.
    Have you any idea why that last function is printing jibberish? Its due up in the morning and to be honest im stumped as to why it wont work.
    thanks for ur help so far.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i dont understand. How can this hold my constant 4 values? they will always be
    Code:
    C cat[limit] = { { 1, "hardware", 21 }, 
                            { 2, "electrical", 21 },          
                            { 3, "appliance", 18 },          
                            { 4, "food", 0 } };
    >Have you any idea why that last function is printing jibberish?
    You'd need to post the latest code. Assuming you fixed the for-loop problems above, you might try making sure all scanf()'s are succeeding (scanf() will return how many items it was able to read, probably a 1 in your case). If it returns something besides 1, then the read was unsuccessful.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    
    
    struct category
    {
    	int id;
    	char description[25];
    	float vatrate;
    	
    };
    
    struct product
    {
    	
    	char  productname[25];
    	float price;
    	int catid;
    	
    };
    
    typedef struct product P;
    typedef struct category C;
    
    void welcome();
    int basket(C array1[], P array2[]);
    void till(int num,P array2[]);
    
    int main()
    {
    
    
    	int k = 0;
    	int i = 0;
    	int size;
    
    	const int limit = 10;
    
    	C cat[limit] = { { 0, "Hardware", 21 }, 
                                       { 1, "Electrical", 21 },          
                                       { 2, "Appliance", 18 },          
                                       { 3, "Food", 0 } };
    
    
    	P prod[limit];
    
    	welcome();
    	
    	printf("Category Code\t\tCategory\t\t\t\tVAT&#37;\n\n");
    		
    	for(i=0;i<4;i++)
    	{
    		printf("%d\t\t\t%s\t\t\t\t%2.1f\n\n",cat[i].id,cat[i].description,cat[i].vatrate);
    	}
    	
    	
    	size=basket(cat,prod);
    	till(size,prod);
    }
     
    
    
    int basket(C array1[], P array2[])
    {
    	int k, num=0, code;
    	
    	printf ("How many items are being purchases\n");
    	scanf ("%d", &num);
    	
    	for(k=0;k<num;k++)
    	{
    		printf("Please enter product name\n");
    		scanf ("%s", array2[k].productname);
    		
    		printf ("Please enter a price of the product\n");
    		scanf("%f", &array2[k].price);
    		
    		printf ("Please enter the category code\n");
    		scanf ("%d", &code);
    		
    		array2[k].catid = array1[ code].vatrate;
    	}		
    	
    	if(code>3||code<0)//if the user types in an invalid category code it prints out this message	
    	{
    		printf("No such code exists");		
    	}
    	
    	return num;
    }
    
    
    void till(int num,P array2[])
    {
    	int q=0;
    	float vat;//the actual vat due is stored here
    	int cost;//this is the total cost
    	
    	
    	printf("Item\t%VAT\tActual Vat\tCost\n");
    	
    	for(q=0;q<num;q++)//this for loop calculates the vat rate and prints out a table of information
    	{
    		vat=((array2[q].price/100)*array2[q].catid);//this calculates the vat due from each product
    		cost=(vat + array2[q].price);//this calculates the total cost of each product including VAT
    		printf("%s\t%2.0f\t%2.2f\t\t%3.2f\n",array2[q].productname,array2[q].catid,vat,cost);		
    	}
    }
    
    
    void welcome()
    {
    	printf ("\t\t\tWelcome\n\n");
    }
    Thats for last piece. worked a dream man. Just the receipt printing now to fix. Why does it keep giving me 0 vales for vat, actual vat and cost?
    Last edited by Signpost; 11-29-2007 at 07:16 PM.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Maybe catid never got stored, as this looks suspicious:
    Code:
    array2[k].catid = array1
    It's impossible to tell the way the code is pasted.

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    i can see some format specifier mistakes, which shouldn't be a problem. But its better to change and see if that works.

    Code:
    printf("&#37;s \t %d \t %2.2f \t\t %d\n",array2[q].productname, array2[q].catid, vat, cost);
    ssharish

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The formatting gets confused by the code containing an index into an array, where the index is called code. Unfortunately, hard to fix, aside from changing the name of the variable code. This obviously have no impact on the actual function of the program.

    Now onto the program itself:
    Code:
    printf("%s\t%2.0f\t%2.2f\t\t%3.2f\n",array2[q].productname,array2[q].catid,vat,cost);
    How about matching the type with the output format?

    Also, this bit:
    array2[k].catid = array1[code].vatrate;
    Assigns a float to an integer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    PS
    Don't use 'code' as an array subscript, it confuses the code tags!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM