Using While Loop with Switch/Case...

Not sure how to set up While Loop???

Page 1 of 1

7 Replies - 14639 Views - Last Post: 21 September 2009 - 01:43 PM Rate Topic: -----

#1 MAV_DevWantB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-September 09

Using While Loop with Switch/Case...

Posted 17 September 2009 - 05:52 PM

:blink:

I am attempting to use a While Loop to read the input (which is an Integer) and store it in the variable "item"... It should loop around until (here is my problem)... not sure how to set up Exit when user has entered all that they want to Enter...

When the user submits to exit'''' the program adds all up and shows OutPut.....





#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);
	//	printf( "\nEnter the EOF (ctrl-z) to end Input.\n\n" );
	printf("======================================\n");

	/* Check for valid selection */

				/* The While Loop is not woking */
	/*NOT sure how ti add in != EOF for user to exit */

	while ((item >= 1) && (item <= 5)) [color=#FF0000]/* while ( (item = getchar()) != EOF) */[/color]	{
		
		/* 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 = (float) prod1 * prodCnt1;
	ttlProd2Cst = (float) prod2 * prodCnt2;
	ttlProd3Cst = (float) prod3 * prodCnt3;
	ttlProd4Cst = (float) prod4 * prodCnt4;
	ttlProd5Cst = (float) 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%17d%21d\n", "1", prodCnt1, ttlProd1Cst );
	printf( "\n%4s%17d%21d\n", "2", prodCnt2, ttlProd2Cst );
	printf( "\n%4s%17d%21d\n", "3", prodCnt3, ttlProd3Cst );
	printf( "\n%4s%17d%21d\n", "4", prodCnt4, ttlProd4Cst );
	printf( "\n%4s%17d%21d\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%12d%25d\n", totlProdCnt, totlGrosCost);
	printf("==========================================\n");
	printf("==========================================\n\n");


	getch();

	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Using While Loop with Switch/Case...

#2 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Using While Loop with Switch/Case...

Posted 17 September 2009 - 05:55 PM

 while ((item >= 1) && (item <= 5)) [color=#FF0000]/* while ( (item = getchar()) != EOF) */[/color]	{


First, your while loop open curly brace is commented out.
Was This Post Helpful? 0
  • +
  • -

#3 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Using While Loop with Switch/Case...

Posted 17 September 2009 - 06:02 PM

Maybe if you read the item inside the while loop. then on the defaul if its the number to exit then let it be and send it back to the while else all the wrong number thing try again and set the item to something valid that wont exit the loop
Was This Post Helpful? 0
  • +
  • -

#4 MAV_DevWantB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-September 09

Re: Using While Loop with Switch/Case...

Posted 20 September 2009 - 07:40 PM

View Postponcho4all, on 17 Sep, 2009 - 05:02 PM, said:

Maybe if you read the item inside the while loop. then on the defaul if its the number to exit then let it be and send it back to the while else all the wrong number thing try again and set the item to something valid that wont exit the loop





Well that seems to be my problem''' but I am looking for some help on how tooo....

I understan how the "getchar() assigns the character from the keyboard... as shown below....

while ( (item = getchar()) != EOF)


My problem is finding a simular "while loop" statement that will have the same outcome... that is it will assign the Integer from the keyBoard to "item" and Loop back until user enters EOF
(ctrl-z)....

AnyHelp out there....
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Using While Loop with Switch/Case...

Posted 20 September 2009 - 07:49 PM

Generally for input routines I find that a do-while works better than a while-loop. The reason being that you generally want to run though the loop at least once before you know if you are ready to exit the loop.

do {
item = getchar();
switch(item) {...}
}while (item!=EOF);


Though you may want to add the EOF to the switch so that an error message is not displayed.
Was This Post Helpful? 0
  • +
  • -

#6 MAV_DevWantB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-September 09

Re: Using While Loop with Switch/Case...

Posted 20 September 2009 - 08:32 PM

View PostNickDMax, on 20 Sep, 2009 - 06:49 PM, said:

Generally for input routines I find that a do-while works better than a while-loop. The reason being that you generally want to run though the loop at least once before you know if you are ready to exit the loop.

do {
item = getchar();
switch(item) {...}
}while (item!=EOF);


Though you may want to add the EOF to the switch so that an error message is not displayed.



*********

Thnks'' Mr. Max

Just wondering''' Can I use the "getchar()" when the user is entering a "Integer" at the keyboard.... that is my problem...
I need a "Integer" evalueated by the the "switch" (item)....

If I can I will give this a shot and get back to ya....

Thnks for your time...
Was This Post Helpful? 0
  • +
  • -

#7 MAV_DevWantB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-September 09

Re: Using While Loop with Switch/Case...

Posted 21 September 2009 - 01:29 PM

Well it would seem my question has been answered by a fellow student... It would seem my problem was that " I did not believe that the (item = getchar()) ) function would be able to work in an Integer inputed at keyboard:...

With just adding Single qoutes around (case '1':) the Integer the getchar () now understood the Integer to be a charater.... I'm sure this is a rough explanation of the situation...

I WOULD WELCOME A MORE PROFESSIONAL EXPLANATION IF ANYONE HAS THE TIME...... THNKS




	while ((item = getchar()) != EOF)
	{
	
		/* determine which product number is inputed */
		/* Check for valid selection */

		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 */





I would appreciat any one thoughts on this subject....

Thnks,,,,
Was This Post Helpful? 0
  • +
  • -

#8 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Using While Loop with Switch/Case...

Posted 21 September 2009 - 01:43 PM

The only explanation is that getchar() receives char's and when you changed 1 for '1' you are changing from integer to char, so basicly this part is no longer working with integers.
Im guessing you also had to change the declaration of int item; to char item.

If not then you are receiving an ASCII for the char '1' and then in the case it compares also cause the ASCII is correct i guess it let you throw. Seems weird tho. To check this you could look for the ASCII code for 1 and print item see if the numbers are alike. This is just in case you didnt changed the int item to char item;
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1