switch - first attempt...

This is a discussion on switch - first attempt... within the C Programming forums, part of the General Programming Boards category; This is my first attempt at switch-break.... What I was trying to achieve is to fill in three arrays with ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    37

    Unhappy switch - first attempt...

    This is my first attempt at switch-break.... What I was trying to achieve is to fill in three arrays with data about sold items at 3 stores. First th program is asking the number of the store. Then fills in the corresponding array.... However, the code I wrote doesn't work. The program always asks about the first array/first store, and I have no idea what is going on. Could you take a look at it, please and tell me what I did wrong?
    Attached Files Attached Files

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    int main()
    {
    
    
    
    
    int counter = 0;
    int number = 0;
    int store_1[5] = {0, 0, 0, 0, -1];
    int store_2[5] = {0, 0, 0, 0, -1];
    int store_3[5] = {0, 0, 0, 0, -1];
    
    char arrayfill = 0;
    
    
    
    
    for (counter = 1; counter <=3; counter++)
    {
    	int choice;
    	printf("Enter the number, from 1 to 3:\n");
    	choice = getchar();
    
    	switch(choice)
    	{
    	case '1':
    			arrayfill = '1';
    			printf("For the shop #1, enter number of T-shirts sold:\n");
    			scanf("%d%*c", &store_1[0]);
    			printf("Enter number of shorts sold:\n");
    			scanf("%d%*c", &store_1[1]);
    			printf("Enter number of sports shoes sold:\n");
    			scanf("%d%*c", &store_1[2]);
    			printf("Enter number of sports magazines sold:\n");
    			scanf("%d%*c", &store_1[3]);
    
    
    
    
    break;
    	case '2':
    			arrayfill = '2';
    			printf("\nFor the shop #2, enter number of T-shirts sold:\n");
    			scanf("%d%*c", &store_2[0]);
    			printf("Enter number of shorts sold:\n");
    			scanf("%d%*c", &store_2[1]);
    			printf("Enter number of sports shoes sold:\n");
    			scanf("%d%*c", &store_2[2]);
    			printf("Enter number of sports magazines sold:\n");
    			scanf("%d%*c", &store_2[3]);
    			
    
    break;
    	case '3':
    
    			arrayfill = '3';
    			printf("\nFor the shop #3, enter number of T-shirts sold:\n");
    			scanf("%d%*c", &store_3[0]);
    			printf("Enter number of shorts sold:\n");
    			scanf("%d%*c", &store_3[1]);
    			printf("Enter number of sports shoes sold:\n");
    			scanf("%d%*c", &store_3[2]);
    			printf("Enter number of sports magazines sold:\n");
    			scanf("%d%*c", &store_3[3]); 
    			
    	default:
    		printf("It's not a correct number of your shop.\n");
    	}
    }
    
    
    
    
    
    return 0;
    }             /* end of main  */
    The world is waiting. I must leave you now.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    Thanks, however after getting rid of the "if" part and making all he other adjustments, what I get is the "default" message...

    Maybe I should use something else instead of switch break? Any suggestions? I find it hard to choose the right "tool", whether work with "if", or "while" or "for"....
    Is switch the right choice for this kind of task?

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    int choice should go at the top of the code, sorry about my mistake. Check out the information about switch/case at the FAQ for this site, it'll help you with this project. Find a few good links/sources for switch case structures, sit down and plan out what you want your program to do, and take it step by step with neatly organized code.

    My preference for switch cases is:
    > choice = getchar();
    Pointing your choice/selection integer at an input accepting function. Then doing switch(choice). This is alot easier than too many if statements.

    Here is a small menu example using switch case
    Code:
    #include <stdio.h>
    
    int PrintMenu ( void );
    
    int main ( void )
    {
    	char choice;
    	while ( PrintMenu() == 0 )
    	{
    		choice = getchar();
    		switch ( choice )
    		{
    			case '1': printf("To be, or not to be.\n\n"); break;
    			case '2': printf("Hello, world.\n\n"); break;
    			case 27: printf("Closing.."); exit(0); break;
    			default: break;
    		}
    	}
    	return 0;
    }
    
    int PrintMenu ( void )
    {
    	printf("Choose a message to print:\n\n");
    	printf("1.  To be, or not to be.\n");
    	printf("2.  Hello, world.\n\n");
    	printf("Escape exits.\n\n");
    	return 0;
    }
    If you're getting abnormal program activity afterwords, it is something with your code.
    The world is waiting. I must leave you now.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    OK, thanks. I'll sit down again and read all the stuff. Sometimes you just don't see certain details and you have to re-do everything....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 04:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21