Thread: Char array display error...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    38

    Char array display error...

    I have a problem displaying a string which is a part of a 2-dimensional array.
    Here's my code...
    - As long as I don't enter 8 for "Other" everything is displayed ok, but as soon as i choose 8 when running the program, the first item of the array, i.e. "Bread" is always replaced with some gibberish. The rest of the array is displayed fine... What am i doing wrong?
    I thought maybe it has something to do with the minus in the Sub-total, which follows the "other" option... Any help is much appreciated.

    Code:
    #include <stdio.h>
    
    int main ( ) {
    
    char item[11][20] = {" Bread", " Butter", " Confectionary",
    				" Fruit", " Meat", " Milk", " Vegetables",
    				" Other", " Sub-total", "Total", "Quit"};
    int i;
    int choice;
    float bread, butter, conf, fruit, meat, milk, veg, other;
    float sum, tendered, change;
    float subtotals[7];
    
    bread = 0;
    butter = 0;
    conf = 0;
    fruit = 0;
    meat = 0;
    milk = 0;
    veg = 0;
    other = 0;
    
    choice = 0;
    tendered = 0.0;
    
    while (choice != 11) {
    	printf("\n Enter your choice:\n");
    	for (i = 0; i < 11; i++)
    	{
    		printf("\t%d. %s\n", (i + 1), item[i]);
    	}
    	printf("\t\t--> ");
    	scanf("%d", &choice);
    
    	switch (choice){
    		case 1:
    			printf(" Price:\t\t");
    			scanf("%f", &subtotals[0]);
    			bread += subtotals[0];
    			break;
    		case 2:
    			printf(" Price:\t\t");
    			scanf("%f", &subtotals[1]);
    			butter += subtotals[1];
    			break;
    		case 3:
    			printf(" Price:\t\t");
    			scanf("%f", &subtotals[2]);
    			conf += conf + subtotals[2];
    			break;
    		case 4:
    			printf(" Price:\t\t");
    			scanf("%f", &subtotals[3]);
    			fruit += subtotals[3];
    			break;
    		case 5:
    			printf(" Price:\t\t");
    			scanf("%f", &subtotals[4]);
    			meat += subtotals[4];
    			break;
    		case 6:
    			printf(" Price:\t\t");
    			scanf("%f", &subtotals[5]);
    			milk += subtotals[5];
    			break;
    		case 7:
    			printf(" Price:\t\t");
    			scanf("%f", &subtotals[6]);
    			veg += subtotals[6];
    			break;
    		case 8:
    			printf(" Price:\t\t");
    			scanf("%f", &subtotals[7]);
    			other += subtotals[7];
    			break;
    		case 9:
    			if (bread > 0){
    				printf("\n%s:\t\t\t%9.2f", item[0], bread);
    			}
    			if (butter > 0){
    				printf("\n%s:\t\t%9.2f", item[1], butter);
    			}
    			if (conf > 0){
    				printf("\n%s:\t\t%9.2f", item[2], conf);
    			}
    			if (fruit > 0){
    				printf("\n%s:\t\t\t%9.2f", item[3], fruit);
    			}
    			if (meat > 0){
    				printf("\n%s:\t\t\t%9.2f", item[4], meat);
    			}
    			if (milk > 0){
    				printf("\n%s:\t\t\t%9.2f", item[5], milk);
    			}
    			if (veg > 0){
    				printf("\n%s:\t\t%9.2f", item[6], veg);
    			}
    			if (other > 0){
    				printf("\n%s:\t\t\t%9.2f", item[7], other);
    			}
    			sum = bread + butter + conf + fruit + meat + milk + veg + other;
    			printf("\n----------------------------------\n");
    			printf(" Total:\t\t\t%9.2f\n", sum);
    			printf("----------------------------------\n");
    			break;
    		case 10:
    			if (bread > 0){
    				printf("\n%s:\t\t\t%9.2f", item[0], bread);
    			}
    			if (butter > 0){
    				printf("\n%s:\t\t%9.2f", item[1], butter);
    			}
    			if (conf > 0){
    				printf("\n%s:\t\t%9.2f", item[2], conf);
    			}
    			if (fruit > 0){
    				printf("\n%s:\t\t\t%9.2f", item[3], fruit);
    			}
    			if (meat > 0){
    				printf("\n%s:\t\t\t%9.2f", item[4], meat);
    			}
    			if (milk > 0){
    				printf("\n%s:\t\t\t%9.2f", item[5], milk);
    			}
    			if (veg > 0){
    				printf("\n%s:\t\t%9.2f", item[6], veg);
    			}
    			if (other > 0){
    				printf("\n%s:\t\t\t%9.2f", item[7], other);
    			}
    			sum = bread + butter + conf + fruit + meat + milk + veg + other;
    			printf("\n-----------------------------------\n");
    			printf(" Total:\t\t\t%9.2f\n", sum);
    			printf("-----------------------------------\n");
    			printf(" Tendered:\t\t");
    			scanf("%f\n", &tendered);
    			change = tendered - sum;
    			printf(" Change:\t\t%9.2f\n", change);
    			break;
    	}
    }
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How many subtotals do you have, and what is the biggest index you use?

    --
    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
    Oct 2007
    Posts
    38
    I got only 1 sub-total, but it loops while user enters anything but 11 (which exits the program). Biggest index? If i understand you correctly, its 11 to quit.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, your array subtotals, which is the highest index you use, and how large is the array itself?

    --
    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.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    conf += conf + subtotals[2];
    Is that right?

    Code:
    scanf("%f\n", &tendered);
    Don't think you mean that to be there.
    "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

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    38
    I know whats the problem now... I have 8 items in the list, but only 7 subtotals i.e. subtotal[7]...

    btw, this is my first ever assignment. I've been asked to use arrays, loops and if/switch statements...
    From a pro point of view, does this code look OK? Are there any simpler ways of approaching certain parts of the code? Any advice is much appreciated

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    A minor suggestion:

    The whole switch statement can be replace with:

    Code:
         printf(" Price:\t\t");
         scanf("%f", &subtotals[choice]);
         bread += subtotals[choice];
         break;
    Cause that's all it's doing. Using choice as an index.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    38
    Oops, i replied seconds too late... Thank you guys...
    hk_mp5kpdw, i was doing it the simpler way and then started editing the code to "optimize" it a bit, if you like... Must have missed that
    IceDane: Great, will work on it now

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are completely unnecessarily using subtotals as an array - you are only ever using one item at any given time, so you could just use a simple variable.

    I would use a do - while() loop rather than a while() loop, because you are always wanting to do at least ONE choice.

    I would probably also use an array instead of simple variables for the sum of each product category, e.g. instead of "bread", use prodCost[choice-1]. That way, you could have all the first 7 choices as one case. Then you could use a loop for cases 9 and 10, instead of a lot of repeated code that is almost the same but not quite.

    --
    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.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Karpaty View Post
    I know whats the problem now... I have 8 items in the list, but only 7 subtotals i.e. subtotal[7]...

    btw, this is my first ever assignment. I've been asked to use arrays, loops and if/switch statements...
    From a pro point of view, does this code look OK? Are there any simpler ways of approaching certain parts of the code? Any advice is much appreciated
    There is a lot of duplicated code there, IceDane's suggestion points to a better way although it won't work exactly as he's put it. How I'd do it (untested):
    Code:
    char item[11][20] = {" Bread", " Butter", " Confectionary",
    				" Fruit", " Meat", " Milk", " Vegetables",
    				" Other", " Sub-total", "Total", "Quit"};
    int i;
    int choice = 0;
    float cost;
    float sum, tendered, change;
    float subtotals[8] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
    
    while (choice != 11) {
        printf("\n Enter your choice:\n");
        for (i = 0; i < 11; i++)
        {
            printf("\t&#37;d. %s\n", (i + 1), item[i]);
        }
        printf("\t\t--> ");
        scanf("%d", &choice);
    
        if( choice > 0 && choice < 9 )
        {
            printf(" Price:\t\t");
            scanf("%f", &cost);
            subtotals[choice-1] += cost;
        }
        else if( choice == 9 )
        {
            for( i = 0; i < 8; ++i )
            {
                sum = 0;
                if( subtotals[i] > 0 )
                    printf("\n%s:\t\t\t%9.2f",item[i],subtotals[i]);
                sum += subtotals[i];
            }
            printf("\n----------------------------------\n");
            printf(" Total:\t\t\t%9.2f\n", sum);
            printf("----------------------------------\n");
        }
        else if( choice == 10 )
        {
            for( i = 0; i < 8; ++i )
            {
                sum = 0;
                if( subtotals[i] > 0 )
                    printf("\n%s:\t\t\t%9.2f",item[i],subtotals[i]);
                sum += subtotals[i];
            }
            printf("\n----------------------------------\n");
            printf(" Total:\t\t\t%9.2f\n", sum);
            printf("----------------------------------\n");
    	printf(" Tendered:\t\t");
    	scanf("%f", &tendered);
    	change = tendered - sum;
    	printf(" Change:\t\t%9.2f\n", change);
        }
    }
    Last edited by hk_mp5kpdw; 10-30-2007 at 08:09 AM.
    "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

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or even:
    Code:
        else if( choice == 9  || choice == 10 )
        {
            for( i = 0; i < 8; ++i )
            {
                sum = 0;
                if( subtotals[i] > 0 )
                    printf("\n%s:\t\t\t%9.2f",item[i],subtotals[i]);
                sum += subtotals[i];
            }
            printf("\n----------------------------------\n");
            printf(" Total:\t\t\t%9.2f\n", sum);
            printf("----------------------------------\n");
            if (choice == 10) {
       	   printf(" Tendered:\t\t");
    	   scanf("%f", &tendered);
    	   change = tendered - sum;
    	   printf(" Change:\t\t%9.2f\n", change);
            }
        }
    That reduces the duplication a bit further, since both 9 and 10 do almost identical things.

    Red shows typo fixed.

    --
    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.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by matsp View Post
    That reduces the duplication a bit further, since both 9 and 10 do almost identical things.

    Red shows typo fixed.

    --
    Mats
    Oops, I'll fix that. Like I said, untested...
    "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

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    38
    matsp & hk_mp5kpdw: thats exactly what I'm doing right now... After i read IceDane's post and took a hard look at my code it just hit me: jeez, there's so much repetition in there... So, now Im trying to further optimize the code... I haven't studied or tested your code yet, since i wanna see how I get on doing it myself... seems quite simple in theory, but then again i thought the same about this whole project and been burnt quite a few times on the way...
    Anyway, will get back to you when I'm done...

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Karpaty View Post
    matsp & hk_mp5kpdw: thats exactly what I'm doing right now... After i read IceDane's post and took a hard look at my code it just hit me: jeez, there's so much repetition in there... So, now Im trying to further optimize the code... I haven't studied or tested your code yet, since i wanna see how I get on doing it myself... seems quite simple in theory, but then again i thought the same about this whole project and been burnt quite a few times on the way...
    Anyway, will get back to you when I'm done...
    Doing it yourself is the best way to KNOW that you [at least think] that you understand what's going on. Use the examples here only as a "last resort".

    --
    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.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    38
    Great! I managed to get it working myself... Hooray!...
    I even found a little misplaced assignment in the above code (yes, i realize you did not test the code prior posting it). I just thought i point it out for those who might find this code useful in the future. You assigned "0" to "sum" inside the FOR loop, so every time the loops repeats itself it overwrites the value of "sum" to "0".
    In my "version" I moved the assignment statement just above the FOR loop line... Seems to produce the desired result

    Anyway, thank you all who replied to this thread. It's been a wonderful learning experience.

    P.S. last question, in my code i used i++ assignment in the FOR loop, you used ++i... What is the difference? Seems to be doing the same thing, doesn't it?
    Last edited by Karpaty; 10-30-2007 at 09:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM