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