Working with Nested "Switch Multiple-Selection"

This is a discussion on Working with Nested "Switch Multiple-Selection" within the C Programming forums, part of the General Programming Boards category; Good day All,,, I am working on a Switch Multiple-Selection program that is nested in a "While Loop".... Which seems ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Location
    Aurora, CO
    Posts
    9

    Angry Working with Nested "Switch Multiple-Selection"

    Good day All,,,

    I am working on a Switch Multiple-Selection program that is nested in a "While Loop".... Which seems to be my problem.. Problem being, I think, is that I need to use an "Integer" & NOT a Char in my opening "While" statemnt...

    Meaning my request is for the User to input a "Product Number" which is in fact a "Integer"......

    I no that this is inCorrect: "while ( (item = getchar()) != EOF)"

    I am not sure what to use in case of an "Integer" is requested....

    Any Help would be appreciated.... Spent several hours on this already
    and cannot seem to find any good examples on the WWW... :{



    Here is my Code thus far...



    Code:
    #include "stdafx.h"
    
    #include <stdio.h>
    
    #include <conio.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    	/* loop until user types end-of-file (ctrl-z) */
    
    	/* user inputs Item# // program gets running Retail cost */
    
    	
    	int item = 0;		/* one item */	
    	
    	int prodCnt1 = 0;		/* number of Prod1 purchase */
    	float prod1 = 2.98;		/* Price Prod1 */
    	float ttlProd1Cst = 0;	/* Total Cost of Prod1 */
    
    	int prodCnt2 = 0;		/* number of Prod2 purchase */
    	float prod2 = 2.98;		/* Price Prod2 */
    	float ttlProd2Cst = 0;	/* Total Cost of Prod2 */
    
    	int prodCnt3 = 0;		/* number of Prod3 purchase */
    	float prod3 = 2.98;		/* Price Prod3 */
    	float ttlProd3Cst = 0;	/* Total Cost of Prod3 */
    
    	int prodCnt4 = 0;		/* number of Prod4 purchase */
    	float prod4 = 2.98;		/* Price Prod4 */
    	float ttlProd4Cst = 0;	/* Total Cost of Prod4 */
    
    	int prodCnt5 = 0;		/* number of Prod5 purchase */
    	float prod5 = 2.98;		/* Price Prod5 */
    	float ttlProd5Cst = 0;	/* Total Cost of Prod5 */
    
    
    	int totlProdCnt = 0;	                /* Total Num of Prod Sold */
    	float totlGrosCost = 0;	/* Total Cost of all Prod Sole */
    
    
    	/* output table for Customers to view Items */
    
    	printf( "\n%4s%21s\n", "Product#", "Retail Price");
    	printf("======================================");
    	printf( "\n%4s%21s\n", "1", "$2.98");
    	printf( "\n%4s%21s\n", "2", "$4.50");
    	printf( "\n%4s%21s\n", "3", "$9.98");
    	printf( "\n%4s%21s\n", "4", "$4.49");
    	printf( "\n%4s%21s\n", "5", "$6.87");
    	printf("======================================\n");
    	printf("======================================\n");
    
    	/* Uner Input for Items Purchased */
    
    	printf( "\nEnter product number to purchase Item.\n" );
    	//	scanf("%d", item);  /* NOT SURE IF THIS IS NEEDED */
    
    	printf( "\nEnter the EOF (ctrl-z) to end Input.\n\n" );
    	printf("======================================\n");
    
    
                              /* HERE' I BELIEVE IS PROBLEM */
                              /* NEED TO RETRIEVE INTEGER FROM USER */
    
    	while ( (item = getchar()) != EOF) 
    	{
    		/* determine which product number is inputed */
    
    	         switch ( item )	           /* switch nested in while loop */
    		{
    		case 1:		/* user inputed prod1 */
    		++prodCnt1;	/* increment prod1 */
    			break;	/* necessary to exit switch */
    
    		case 2:		/* user inputed prod1 */
    		++prodCnt2;	/* increment prod1 */
    			break;	/* necessary to exit switch */
    
    		case 3:		/* user inputed prod1 */
    		++prodCnt3;	/* increment prod1 */
    			break;	/* necessary to exit switch */
    
    		case 4:		/* user inputed prod1 */
    		++prodCnt4;	/* increment prod1 */
    			break;	/* necessary to exit switch */
    
    		case 5:		/* user inputed prod1 */
    		++prodCnt5;	/* increment prod1 */
    			break;	/* necessary to exit switch */
    
    
    		/* Eleminate User from inputing */
    
    		case '\n':		/* ignore newLines */
    		case '\t':		/* ignore tabs */
    		case ' ':		/* ignore spaces in input */
    	                      break;	/* exit switch */
    
    
    		/* catch all other inCorrect Numbers inputed */
    
    		default:	/* catch all other numbers */				
    		printf( "Incorrect product number entered.\n");
    		printf( "Enter a new product number.\n" );
    		       break; /* optional - will exit switch anyway */
    
    		} /* END SWITCH */
    
    
    	} /* END WHILE LOOP */
    
    
    	ttlProd1Cst = prod1 * prodCnt1;
    	ttlProd2Cst = prod2 * prodCnt2;
    	ttlProd3Cst = prod3 * prodCnt3;
    	ttlProd4Cst = prod4 * prodCnt4;
    	ttlProd5Cst = prod5 * prodCnt5;
    
                   totlProdCnt = prodCnt1 + prodCnt2 + prodCnt3 + 
                                         prodCnt4 + prodCnt5;
    
                   totlGrosCost = ttlProd1Cst + ttlProd2Cst + ttlProd3Cst + 
                                           ttlProd4Cst + ttlProd5Cst;	
    
    
    	/* display resutlts in tabular format */
    
    printf( "\n%4s%17s%21s\n", "Product#", "Qauntity Sold", "Total Item Price");
    
    	printf("======================================\n");
    	printf( "\n%4s%17s%21s\n", "1", prodCnt1, ttlProd1Cst );
    	printf( "\n%4s%17s%21s\n", "2", prodCnt2, ttlProd2Cst );
    	printf( "\n%4s%17s%21s\n", "3", prodCnt3, ttlProd3Cst );
    	printf( "\n%4s%17s%21s\n", "4", prodCnt4, ttlProd4Cst );
    	printf( "\n%4s%17s%21s\n", "5", prodCnt5, ttlProd5Cst );
    	printf("======================================\n");
    	printf("======================================\n\n");
    
    	printf("\n=====================================\n");
    	printf("======================================\n");
    	printf( "\n%4s%21s\n", "Total Products Sold", "Total Price");
    	printf("======================================n");
    	printf( "\n%12s%25s\n", totlProdCnt, totlGrosCost);
    	printf("======================================\n");
    	printf("======================================\n\n");
    
    
    
    	getch();
    
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It is unlikely that you can type the character 1 from the keyboard. You can type '1' from the keyboard, so you should consider using that as your case.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Location
    Aurora, CO
    Posts
    9
    Thanks, tabstop....

    I am actualy tring to use "Integers" for my "Case's (1, 2, 3, 4, 5)".... My example above has the
    "( (item = getchar()) != EOF)" as an Example Only... I am tring to find simular code that can be used for an "Integer".....

    So I agree with you'''' I must admit I probably did not explain it well enough....

    Can anyOne give me a suggestion to implement a simular code that will take an "Integer" and "Assign" it to "item" and also have the "EOF" in affect....

    Hope this is more clear.... thnks

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Have a look at this. Play around with the code, and once you understand how it all works, you can wrap it up in a function, and re-use it any time you need integer input from the user.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MAV_DevWantB View Post
    Thanks, tabstop....

    I am actualy tring to use "Integers" for my "Case's (1, 2, 3, 4, 5)".... My example above has the
    "( (item = getchar()) != EOF)" as an Example Only... I am tring to find simular code that can be used for an "Integer".....

    So I agree with you'''' I must admit I probably did not explain it well enough....

    Can anyOne give me a suggestion to implement a simular code that will take an "Integer" and "Assign" it to "item" and also have the "EOF" in affect....

    Hope this is more clear.... thnks
    '1' is an integer (it is not the integer 1, but it is an integer).

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    '1' in ASCII is equivalent to integer 49.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with multiple source files
    By abh!shek in forum C Programming
    Replies: 17
    Last Post: 06-03-2008, 11:23 AM
  2. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  3. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  4. Replies: 1
    Last Post: 05-01-2003, 02:52 PM
  5. working out the multiple of a number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-22-2001, 11:23 AM

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