Loop issue? C programming.

March 28, 2010 at 18:50:40
Specs: Windows 7
I am having a small issue with my program, and maybe a new set of eyes will help me out.

Part 1 of my project is to replicate a vending machine menu in which you purchase snacks from. We are practicing using Functions and Pointers - and can only use <stdio.h> as our only library - no if's, and's or but's.

Long story short, and am able to display a proper menu selection, with clean formatting, and can obtain user input from my menu function. However at the end of my program when the user is prompted whether or not they want to select another snack. typing N (for No) will end the program, but typing Y (for yes) is suppose to just loop back to my menu for another selection. When the loop occurs my program immediately displays the menu again, then goes directly to the "do you want to select another item" prompt - without ever letting me choose another item. What am I doing wrong?!?!

Again this is an intro to programming C class - we cannot use library's outside of <stdio.h> and this program is practicing using functions and pointers. My project BUILDS Successfully and will run. I do not get errors. Below is a copy of my code, and output.


-----------------------------------------
CODE:

//Snacks.c: Vending machine software to return product selection, collect money, and return change.

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>


//Function prototypes
//prompts the user for a selection and returns the menu selection
void Quit(char *cptr);

// Displays the list of snack items and prompts for the user’s choice
void Menu(char *selection);

//sets the cost of the purchase based on value in selection
void GetCost(char selection, double *item_cost);


int main ( )
{

double change;
double purchase;
double item_cost;
char selection =' ';
char choice =' ';


do
{

//Displays Menu or snacks and asks for a selection.
Menu(&selection);

printf("\nThe Choice is %c\n", selection);

//GetCost(&selection, &item_cost);

Quit(&choice);//gets menu choice


}while(choice!='n'&& choice!='N');

printf ("\n GODDBYE\n");
}

//prompts the user for a selection
//returns the menu selection
void Quit(char *cptr)
{
//char selection;
printf("\n Would you like another Snack (or N to quit)? ");
scanf(" %c", cptr);
//return selection;
}

// Displays the list of snack items and prompts for the user’s choice
void Menu(char *selection)
{

printf("\n Menu:\n\n");
printf(" P - Protato Chip\n");
printf(" C - Chocolate Bar\n");
printf(" B - Brownie\n");
printf(" N - Nuts\n");
printf(" K - Keebler Crakers\n");
printf(" O - Oreo Cookies\n");
printf(" \nPlease make a selection: ");
scanf("%c", selection);

}

//sets the cost of the purchase based on value in selection
/*void GetCost(char selection, double *item_cost);
{

if(selection == 'p' || choice == 'P')
item_cost=1.25;
else if(selection == 'c' || choice == 'C')
item_cost=1.30;
else if(selection == 'b' || choice == 'B')
item_cost=1.75;
else if(selection == 'n' || choice == 'N')
item_cost=1.40;
else if(selection == 'k' || choice == 'K')
item_cost=1.00;
else if(selection == 'o' || choice == 'O')
item_cost=1.50;
else printf("\nChoice not recognized\n");

}*/

----------------------------------------------

FYI the GetCost function is part 2 of this project, but i have it commented out for the mean time until i get this first issue resolved with in my loop.


--------------------------------------------
OUTPUT:


Menu:

P - Protato Chip
C - Chocolate Bar
B - Brownie
N - Nuts
K - Keebler Crakers
O - Oreo Cookies

Please make a selection: p

The Choice is p

Would you like another Snack (or N to quit)? y

Menu:

P - Protato Chip
C - Chocolate Bar
B - Brownie
N - Nuts
K - Keebler Crakers
O - Oreo Cookies

Please make a selection:
The Choice is


Would you like another Snack (or N to quit)?


Again i have a feeling maybe there is an issue with my do-while loop, but im not sure. You can see where I typed Y the first time it asked if i wanted another item, it looped through the whole prompt and never allowed me to make a second selection, leaving "Please make a selection" and "the Choice is" blank. Any and all help is appreciated. Please let me know if you need any more information! Thanks!


See More: Loop issue? C programming.

Report •


#1
March 28, 2010 at 19:09:22
I found the issue. God i hate programming...

Under the menu function definition:

scanf("%c", selection); <-- Needs a space before the % sign...

scanf(" %c", selection); <-- Corrected.

Stupid.


Report •

#2
March 28, 2010 at 22:51:29
naah, not ("stupid") when dealing with C. you have my sympathies. I would suggest reserving judgement on programming in general if it is only based on C, (at this point, i hate it too!) Most "other" languages are much easier and more forgiving. C is a hard taskmaster...
at least for me, it can be fun, sometimes!

Report •
Related Solutions


Ask Question