Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

do while loops

Status
Not open for further replies.

SloCal

Technical User
May 20, 2005
3
US
I'm doing a class product, and I'm getting stuck in a do while loop. The conditions have to be true in order for the loop to repeat itself right? Since the conditions use OR logic, doesn't that mean that if any of the conditions are false, then it should jump out of the loop? I'm also having problems with getting the right value for funds to come back for the fourth function. Anything would help.

The do while loop problem is in the function called chooseProduct

Thanks in advance.


#include <stdio.h>
#include <assert.h>

#define MAXPRODUCTS 11
#define STRINGLEN 50

/* function prototypes */
void showMenu(char productNames[][STRINGLEN], float prices[], int quantity[],
int numProducts);

float collectFunds(void);

int chooseProduct(int quantity[], float funds, float prices[], int numProducts);

void makeChange(float funds, float price,
int* dollarsBack, int* quartersBack, int* dimesBack,
int* nickelsBack, int* penniesBack);

void thankUser(char productNames[][STRINGLEN], int choice, int dollarsBack,
int quartersBack, int dimesBack, int nickelsBack,
int penniesBack);

int main(void)
{
/* working variables */
int numProducts = MAXPRODUCTS;
int choice;
float funds;
int dollarsBack, quartersBack, dimesBack, nickelsBack, penniesBack;
char shutOff;

/* set up your vending machine products here. */
float prices[MAXPRODUCTS] = {0.00, 9.00, 35.00, 0.50, 13.00, 6.00, 1.25, 11.00,
0.15, 150.00, 8.00};
char productNames[MAXPRODUCTS][STRINGLEN] = {"empty", "Legwarmers",
"Spandex Shorts", "Headband", "MJ's Thriller", "Pump Sneakers", "Rubik's Cube", "Movie: Ghost Busters",
"Neon Green Tie Shirt", "Atari Gamesystem", "Crimping Iron"};
int quantity[MAXPRODUCTS] = {0, 9, 1, 5, 0, 2, 15, 3, 7, 3, 6};

do {
/* step 1: show the menu. */
showMenu(productNames, prices, quantity, numProducts);

/* step 2: collect money. */
funds = collectFunds();

/* step 3: get a product choice. */
choice = chooseProduct(quantity, funds, prices, numProducts);

/* step 4: determine change. */
if (choice > 0) {
makeChange(funds, prices[choice], &dollarsBack, &quartersBack,
&dimesBack, &nickelsBack, &penniesBack);
}
else {
makeChange(funds, 0, &dollarsBack, &quartersBack,
&dimesBack, &nickelsBack, &penniesBack);
}

/* step 5: thank user. */
thankUser(productNames, choice, dollarsBack, quartersBack, dimesBack,
nickelsBack, penniesBack);

/* step 6: figure out whether or not to shut off machine. */
printf("Would you like to shut off the vending machine [Y/N]? ");
scanf(" %c", &shutOff);
shutOff = tolower(shutOff);
printf("\n");
} while(shutOff == 'n');

return 0;
}

/* Displays the menu to the user.
*
* Parameters:
* Name Type
* -----------------------------------------------
* productNames char[][]
* contains the names of the products.
* prices float
* contains the prices of the products.
* quantity int
* contains the quantity of each product.
* numProducts int
* contains the number of products available.
*
* Preconditions:
* 1. productNames must be an array of null-terminated strings.
* 2. size(productNames) == size(prices) == size(quanity) == numProducts.
*/
void showMenu(char productNames[][STRINGLEN], float prices[], int quantity[],
int numProducts)
{
printf("80's Memorabilia Vending Machine\n");
for (numProducts=1; numProducts<MAXPRODUCTS; numProducts++) {
printf("%3d: %-21s%2d%8.2f\n", numProducts, productNames[numProducts],
quantity[numProducts], prices[numProducts]);
}
printf("\n");
return;
}

/* Collects the amount of money the user puts into the machine.
*
* No parameters.
*
* Returns the amount of money input.
*
* Restrictions:
* 1. Entered funds must be >= 0
*/
float collectFunds(void)
{
float funds=0.0;

printf("Please insert money: ");
scanf("%f", &funds);
do {
if (funds > 0)
break;
printf("Insufficient funds.\n");
printf("Please insert money: ");
scanf("%f", &funds);
} while (funds <= 0);
printf("You've entered %3.2f\n", funds);
return;
}

/* Takes the user's product choice.
* Some restrictions:
* 1. Choice > 1 and choice < numProducts.
* 2. The user's choice must take quantity into account; don't let them
* choose a product if there's none available!
* 3. The user's choice must take funds into account; don't let them choose a
* product if they have insufficient funds.
*
* Also, this function must update the quantity of the product chosen.
*
* Parameters:
* Name Type
* -----------------------------------------------------
* quantity int[]
* contains the quantity of each product.
* funds float
* contains the funds that the user has.
* prices float[]
* contains the prices for each product.
* numProducts int
* contains the number of products available (+1 for empty slot).
*
* Postconditions:
* 1. updates the quantity of the chosen product.
*
* Return Value:
* Returns the choice of product, as an index into the array
* of product names. RETURNS -1 if the user wants a refund.
*/
int chooseProduct(int quantity[], float funds, float prices[], int numProducts)
{
int choice;

/* get the user's choice, accounting for a valid selection,
sufficient quantity, and sufficient funds. */
do {
printf("Please enter a product number (-1 for a refund): ");
scanf("%d", &choice);
if (choice < 0)
break;
} while(choice < -1 || choice > numProducts || funds < prices[choice] || quantity[choice] <= 0);
/* CHANGE the above XXX's to validate the selection. */

if (choice > 0)
quantity[choice] -= 1;

return choice;
}

/* Determines the amount of change, given the amount inserted and
* product price.
*
* Parameters:
* Name Type
* -----------------------------------------------------
* funds float
* the amount inserted into the machine.
* price float
* the cost of the selected product.
* dollarsBack int*
* the number of dollars of change.
* quartersBack int*
* the number of quarters of change.
* dimesBack int*
* the number of dimes of change.
* nickelsBack int*
* the number of nickels of change.
* penniesBack int*
* the number of pennies of change.
*
* Postconditions:
* 1) all int* variables will be modified to contain the
* number of coins in change for that denomination.
*/
void makeChange(float funds, float price,
int* dollarsBack, int* quartersBack, int* dimesBack,
int* nickelsBack, int* penniesBack)
{

/*funds = funds - price; */
/*This finds the number of dollars*/
printf("The price of your choice is %3.2f\n", price);
printf("You have entered %3.2f\n", funds);
/*while (funds >= 1) {
funds -= 1;
*dollarsBack += 1;
}
while (funds >= .25) {
funds -= .25;
*quartersBack += 1;
}
while (funds >= .10) {
funds -= .10;
*dimesBack += 1;
}
while (funds >= .05) {
funds -= .05;
*nickelsBack += 1;
}
while (funds >= .01) {
funds -= .05;
*penniesBack += 1;
} */

return;
}
/* Thanks the user for their business, telling them to enjoy
* their product. Display the user's change.
*
* Parameters:
* Name Type
* -----------------------------------------------------
* productNames char[][]
* contains the names of the products.
* choice int
* index of user's selection. (-1 if they want a refund)
* dollarsBack int
* the number of dollars of change.
* quartersBack int
* the number of quarters of change.
* dimesBack int
* the number of dimes of change.
* nickelsBack int
* the number of nickels of change.
* penniesBack int
* the number of pennies of change.
*
* Preconditions:
* 1) productNames must contain null-terminated strings.
* 2) choice must be a valid index into the array.
*/
void thankUser(char productNames[][STRINGLEN], int choice, int dollarsBack,
int quartersBack, int dimesBack, int nickelsBack,
int penniesBack)
{
float change;
if (choice > 0) {
printf("\n");
printf("Thank you! Here is your %s.\n", productNames[choice]);
}
/*printf("You get: \n\t%d dollars\n\t%d quarters\n\t%d dimes\n\t%d nickels\n\t%d pennies\n\t\n",
dollarsBack, quartersBack, dimesBack, nickelsBack, penniesBack);
printf("For a total of $%3.2f\n", funds); */
else {
printf("\n");
printf("Thank you! Here is your refund.\n");
}
printf("You get: \n\t%d dollars\n\t%d quarters\n\t%d dimes\n\t%d nickels\n\t%d pennies\n\t\n",
dollarsBack, quartersBack, dimesBack, nickelsBack, penniesBack);
/*printf("For a total of $%3.2f\n", funds); */

return;
}
 
Nevermind... I found that I forgot to return a value in the second function which screwed everything else behind it up!!
 
Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

> Since the conditions use OR logic, doesn't that mean that if any of the conditions are false,
> then it should jump out of the loop?
No, it means the loops continue while any of the conditions are true.

> I found that I forgot to return a value in the second function
Any decent compiler would have warned you about that.
Code:
gcc -Wall -W -ansi -pedantic  hello.c
hello.c: In function `main':
hello.c:67: warning: implicit declaration of function `tolower'
hello.c: In function `collectFunds':
hello.c:127: [b][COLOR=red]warning: `return' with no value, in function returning non-void[/color][/b]
hello.c: In function `makeChange':
hello.c:205: warning: unused parameter `dollarsBack'
hello.c:205: warning: unused parameter `quartersBack'
hello.c:205: warning: unused parameter `dimesBack'
hello.c:206: warning: unused parameter `nickelsBack'
hello.c:206: warning: unused parameter `penniesBack'
hello.c: In function `thankUser':
hello.c:265: warning: unused variable `change'

--
 
The compiler I used let it go and then when I ran the program, it never let me out of the loop because I initialized the value to zero which passed as zero to the condition which always made it true. Once I got rid of the initial value and passed the calculated one, everything was fine... well at least with the first three functions. I have since re-wrote the others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top