Hi, this is my first time posting here. So I'm working on this project for my final that is due in a few days for my C programming class. I have just one thing left. My program is a snack machine. The problem exists when the user puts in how much of something they want. Let's say I have 6 dollars and I want a quanity of 7 of an item (each item if 1 dollar) it says please reenter your choice but goes to the next item. I want it to reask for the same item. Sorry if I am doing something wrong. This is my source code.
Code:/* Term Project I CSC 218, S1 Spring, 2008 Dr. Manki Min Nathan Nash 1750669 */ #include <stdio.h> //header file #include <stdbool.h> //header file #include <stdlib.h> //header file #include <math.h> //header file int money, choice1; // declaration of variables bool a = false; //bool statement char candy[8][11] = {"snickers","mars", "hershey", "chips", "crackers", "cookies", "skittles", "gummybears"}; //snacks int snacks[8]; // defined array for how many wanted bool b = false; //bool statement int main (void) //main program { // opening of snack machine system ( "clear" ); //clears screen printf("\n\t\t\tWelcome to Nate's Tummy Yummies!\n\n"); //Title of snack machine while (a == false) //loop statement { printf("\nPlease enter the amount of money you have in dollars. Ex: 35 dollars = 35. "); //Statement about money scanf("%d", &money); //Enter the ammount of money you have printf("\nYou have entered $%d, is this correct? Type '1' for yes, '2' to return, or '4' to exit. ", money); //Asks user if ammount entered was correct. scanf("%d", &choice1); //Let's user confirm ammount or have a chance to go back if (choice1 == 1) // choices { printf("\nYou have $%d.\n", money); //States how much money the user has left printf("You will get to choose from snickers, mars, hershey, chips, crackers, cookies, skittles, and gummybears.\n\n"); //shows choices printf("Each item costs $1\n"); //display cost of each item a = true; //continues on with program } else if (choice1 == 2) //no choice { a = false; //goes back to enter money } else if (choice1 == 4) //choice to quit { return 0; //terminates program } else //else statement { printf("\nInvalid choice. Please try again.\n"); //invalid choice a = false; //invalid entry returns to choice } } int row; //define row int col; //define col int x; //define x FILE* spOut; //used for opening file spOut = fopen("receipt.dat", "w"); //opens receipt.dat for (row = 0; row < 8; row++) //for statement with how many like of candy { if (money >= 0) //if money is greater than or equal to 0 { printf("\nHow many of the %s would you like? ", candy[row]); //asks for how many snacks scanf("%d", &snacks[row]); //stores users input for snacks if (snacks[row] > money) { printf("\nYou don't have enough money please re-enter the amount you want."); } if (snacks[row] <= money) { fprintf(spOut, "\n%d %s ", snacks[row], candy[row]); //prints output to outside file money = money - snacks[row]; //subtracts money printf("\nYou have $%d remaining. ", money); //money remaining } } } for(row = 0; row < 8; row++) //for statement for how many wanted { printf("\nYou wanted %d", snacks[row]); //tell how many snacks wanted printf(" %s.", candy[row]); //name of candy } printf("\n\nYour receipt is printed on the file receipt.dat\n"); //tells user that file will be made printf("\nThank you for choosing Nate's Tummy Yummies!\n"); //closing statement fprintf(spOut, "\nThank you!"); //prints thanks you to outside file return 0; //End program }



LinkBack URL
About LinkBacks


