Last active
February 19, 2017 03:00
-
-
Save cwells/9bf9a5143163566a4141 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_WARNINGS // turn off SAFE COMMANDS | |
// ############################ | |
// Written By: Cristian Colocho | |
// Date Written: March, 14, 2015 | |
// Purpose: Loop Menu and Switch | |
// ############################ | |
#include <stdio.h> | |
#include <stdlib.h> | |
int enter(int n) { | |
int number; | |
printf("Enter a number: "); | |
scanf("%i", &number); | |
return number; | |
} | |
int square(int n) { | |
int square = n*n; | |
printf("%i squared is %i\n", n, square); | |
return n; | |
} | |
int cube(int n) { | |
int cubed = n*n*n; | |
printf("%i cubed is %i\n", n, cubed); | |
return n; | |
} | |
int parity(int n) { | |
char *p = n % 2 ? "odd" : "even"; | |
printf("%i is %s\n", n, p); | |
return n; | |
} | |
int quit(int n) { | |
printf("Exiting\n"); | |
return n; | |
} | |
struct MenuFunction { | |
char *text; | |
int (*f)(int n); | |
}; | |
int main() { | |
int choice, number = 0; | |
struct MenuFunction menuitems[] = { | |
{"Enter a number", enter}, | |
{"Square the number", square}, | |
{"Cube the number", cube}, | |
{"Display parity", parity}, | |
{"Quit", quit} | |
}; | |
int menucount = sizeof(menuitems) / sizeof(struct MenuFunction); | |
do { | |
printf("\nSelect an option: \n"); | |
for (int i = 0; i < menucount; i++) { | |
printf("%d) %s\n", i+1, menuitems[i].text); | |
} | |
scanf("%d", &choice); | |
if (choice < 1 || choice > menucount) { | |
printf("Invalid option\n"); | |
continue; | |
}; | |
number = (menuitems[choice-1].f)(number); | |
} while (choice < menucount); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment