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;
}

New Topic/Question
Reply




MultiQuote






|