Programs for COP2213 Lecture 9/8 Lecturer: Arup Guha Example 1 --------- { Name: Arup Guha Date: 9/9/99 Program: This program lets the user get change. The user is presented with a menu that gives them choices to get pennies, nickels, dimes, quarters, or to quit. After each choice, the user is asked how many of each coin they want. This process continues until the user quits at which point, the total amount of change in cents is printed to the screen. } program Change_Store; var num_coins : integer; {used to read in number of particular coin user wants.} total : integer; {Keeps track of current total spent.} ans: integer; {Used to read in menu choice.} begin {Print out menu and read answer choice.} writeln('We provide change. Here are your options:'); writeln('1. Pennies'); writeln('2. Nickels'); writeln('3. Dimes'); writeln('4. Quarters'); writeln('5. Done getting change.'); readln(ans); {Main loop. This runs till the user chooses to quit.} while ans <> 5 do begin {Case statement where menu choice is actually executed.} case ans of {User is asked for number of pennies they want. Then total is adjusted accordingly. Rest of the cases are similar.} 1 : begin writeln('Enter number of pennies you want.'); readln(num_coins); total := total + num_coins end; 2 : begin writeln('Enter number of nickels you want.'); readln(num_coins); total := total + 5*num_coins end; 3 : begin writeln('Enter number of dimes you want.'); readln(num_coins); total := total + 10*num_coins end; 4 : begin writeln('Enter number of quarters you want.'); readln(num_coins); total := total + 25*num_coins end; else writeln('Sorry, that was not a valid choice, try again.'); end; {end case} {Print menu and read in menu selection.} writeln('Would you like more change? Here are the choices again.'); writeln('1. Pennies'); writeln('2. Nickels'); writeln('3. Dimes'); writeln('4. Quarters'); writeln('5. Done getting change.'); readln(ans); end; {end while} {Output total cents purchased.} writeln('You have purchased a total of ',total,' cents in change.'); end. {end program} Example 2 --------- { Name: Arup Guha Date: 9/9/99 Program: This program lets the user get change. The user is presented with a menu that gives them choices to get pennies, nickels, dimes, quarters, or to quit. After each choice, the user is asked how many of each coin they want. This process continues until the user quits at which point, the total amount of change in cents is printed to the screen. } program Change_Store; var num_coins : integer; {used to read in number of particular coin user wants.} total : integer; {Keeps track of current total spent.} ans: integer; {Used to read in menu choice.} begin {Main loop. This runs till the user chooses to quit.} repeat {Print out menu and read in menu selection.} writeln('We provide change. Here are your options:'); writeln('1. Pennies'); writeln('2. Nickels'); writeln('3. Dimes'); writeln('4. Quarters'); writeln('5. Done getting change.'); readln(ans); {Case statement where menu choice is actually executed.} case ans of {User is asked for number of pennies they want. Then total is adjusted accordingly. Rest of the cases are similar.} 1 : begin writeln('Enter number of pennies you want.'); readln(num_coins); total := total + num_coins end; 2 : begin writeln('Enter number of nickels you want.'); readln(num_coins); total := total + 5*num_coins end; 3 : begin writeln('Enter number of dimes you want.'); readln(num_coins); total := total + 10*num_coins end; 4 : begin writeln('Enter number of quarters you want.'); readln(num_coins); total := total + 25*num_coins end; 5 : writeln('Thank you for choosing the Change Store.'); else writeln('Sorry, that was not a valid choice, try again.'); end; {end case} until ans = 5; {end repeat-until} {Output total cents purchased.} writeln('You have purchased a total of ',total,' cents in change.'); end. {end program}