Classes/csci218/Labs/lab6/GarrettHaldruplab6.c

228 lines
4.4 KiB
C

/* Name: Garrett Haldrup
lab6.c
Problem: Runs a loop letting a user decide which of the 4 programs to run until they are done.
Certification of Authenticity:
I certify that this assignment is entirely my own work.
*/
#include <stdio.h>
#include <ctype.h>
void scoreToLetter();
void ageInfo();
void digit2word();
void simpleCalculator();
int main() {
char select, rerun;
while (1) {
// Display Selection Info
printf("Select one program:\n");
printf("- A: scoreToLetter\n");
printf("- B: ageInfo\n");
printf("- C: digit2word\n");
printf("- D: simpleCalculator\n");
printf("Enter selction: ");
scanf("%c", &select);
printf("\n");
// Standardize char selection
select = toupper(select);
// Based on selection run a program
switch (select) {
case 'A':
scoreToLetter();
break;
case 'B':
ageInfo();
break;
case 'C':
digit2word();
break;
case 'D':
simpleCalculator();
break;
}
// Clear input buffer
while (getchar() != '\n');
// See if the user wants to continue the loop
printf("\nRun another? (y/n): ");
scanf("%c", &rerun);
// Standardize char for rerun ask
rerun = tolower(rerun);
if (rerun == 'n') {
break;
}
// Clear input buffer
while (getchar() != '\n');
printf("\n");
}
return 0;
}
void scoreToLetter() {
// Purpose
printf("This program will take a numerical score and convert it to a letter grade.\n");
// Dclare int
int score;
// Take input
printf("Enter your score(0-100): ");
scanf("%d", &score);
// Switch Case and print grade
switch (score) {
case 90 ... 100:
printf("A\n");
break;
case 80 ... 89:
printf("B\n");
break;
case 70 ... 79:
printf("C\n");
break;
case 60 ... 69:
printf("D\n");
break;
case 0 ... 59:
printf("F\n");
break;
default:
printf("Must be really bad or really good :)\n");
}
}
void ageInfo() {
// Purpose
printf("This program will take your age and say what you cant and can do\n\n");
// Declare age
int age;
// Take Input
printf("Enter your age: ");
scanf("%d", &age);
// Compare age to see range
if (age >= 18) {
printf("Can vote\n");
if (age >= 21) {
printf("Can buy alcohol\n");
} else {
printf("Not old enoguth to buy alcohol\n");
}
} else {
printf("Not old enough to vote\n");
}
}
void digit2word() {
// Purpose
printf("This program will take a digit (0-9) and convert it to a word.\n\n");
// Declare int variable
int num;
// Ask for input number
printf("Enter a number(0-9): ");
scanf("%d", &num);
// Swtich case the numbers
switch(num) {
case 0:
printf("Zero");
break;
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
case 4:
printf("Four");
break;
case 5:
printf("Five");
break;
case 6:
printf("Six");
break;
case 7:
printf("Seven");
break;
case 8:
printf("Eight");
break;
case 9:
printf("Nine");
break;
}
}
void simpleCalculator() {
// Purpose
printf("This program will take float inputs and an operand and display the calculation!\n\n");
// Declare floats and char
float num1, num2, answer;
char operand;
// Ask 2 floats and operand
printf("Enter float number 1: ");
scanf("%f", &num1);
while (1) {
printf("Enter your operand('-', '+', '*', '/'): ");
// Clear Buffer
while (getchar() != '\n');
scanf("%c", &operand);
if (!(operand == '-' || operand == '+' || operand == '*' || operand == '/')) {
printf("Not a valid opperand! Please try again!\n");
continue;
}
break;
}
// Check if dividing by 0
while (1) {
printf("Enter float number 2: ");
scanf("%f", &num2);
if (operand == '/' && !num2) {
printf("Cannot divide by 0! Please try again!\n");
continue;
}
break;
}
// Compute
switch (operand) {
case '-':
answer = num1 - num2;
break;
case '+':
answer = num1 + num2;
break;
case '*':
answer = num1 * num2;
break;
case '/':
answer = num1 / num2;
break;
}
// Display output
printf("%.1f %c %.1f = %.2f", num1, operand, num2, answer);
}