-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment 1.c
95 lines (67 loc) · 1.86 KB
/
Assignment 1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
/* Name: Jeremiah Rallos
ID: 34454127
Purpose: Modular Change Calculator */
int main()
{
system("color 3");
int num = 0, remainingValue = 0;
char exitresult;
while(exitresult != 'n')
{
num = getInput();
remainingValue = change(50, num);
remainingValue = change(20,remainingValue);
remainingValue = change(10,remainingValue);
remainingValue = change(5,remainingValue);
exitresult = exitStage();
}
return 0;
}
int getInput () //Input Function.
{
int input;
printf("\n[ Please enter a number. ]\n");
printf("[ It must be a multiple of 5. ]\n");
printf("[ It should be a value you wish to change. ]\n");
if (scanf("%d%*c", &input) != 1)
{
while(getchar()!='\n'); // This line prevents the input of alphabetic characters.
printf("\n[ Invalid input. ]\n");
}
else if( (input < 5 || input > 95) || (input % 5 != 0) )
{
printf("\n[ Invalid input, please try again! ]\n");
}
else
{
return input;
}
}
int change(int coinsize, int coinvalue) // Change calculator.
{
int temp = 0;
while(coinvalue >= coinsize)
{
coinvalue = coinvalue - coinsize; // Sending back new value to main.
temp++; //increments the amount of coins
}
printf("[ Number of %i cents are %d ]\n", coinsize, temp);
return coinvalue;
}
int exitStage ()
{
char choice;
printf("[ Would you like to continue? (y/n) ]\n");
scanf("%c%*c", &choice);
if(choice == 'y')
{
return choice;
}
else if(choice == 'n')
{
printf("[ Thanks for sticking around. ]");
return choice;
}
}