Skip to content

Instantly share code, notes, and snippets.

@FrankyBoy
Created April 17, 2017 07:18
Show Gist options
  • Save FrankyBoy/fec5c064ad7a0411d108ffdb347d9c19 to your computer and use it in GitHub Desktop.
Save FrankyBoy/fec5c064ad7a0411d108ffdb347d9c19 to your computer and use it in GitHub Desktop.
// before
void foo() {
// some code
bar(); // not yet defined
}
void bar() {
// some code
foo();
}
// after
// Do foo things
void foo();
// do bar things
void bar();
void foo() {
// some code
bar(); // signature already above, everything fine
}
void bar() {
// some code
foo();
}
// before
void choosePayroll() {
printf("-----------------------------------------------\n");
printf("Press '1' to access the Manager's payroll\n");
printf("Press '2' to access the Hourly worker's payroll\n");
printf("Press '3' to access Commission worker's payroll\n");
printf("Press '4' to access Pieceworkers's payroll\n");
printf("Press 'Q' to access Manager's payroll\n");
printf("-----------------------------------------------\n");
}
// after
const char* payrollOutput =
"-----------------------------------------------\n"
"Press '1' to access the Manager's payroll\n"
"Press '2' to access the Hourly worker's payroll\n"
"Press '3' to access Commission worker's payroll\n"
"Press '4' to access Pieceworkers's payroll\n"
"Press 'Q' to access Manager's payroll\n"
"-----------------------------------------------\n";
// somewhere later in the code
printf(payrollOutput);
printf("Press 'R' to return back to the main menu\n\n");
printf("Press 'E' to exit to the total\n");
scanf_s(" %c", &charReturn); // space before %c to tell scanf to skip the leading whitespace
switch (charReturn)
{
// and so on
}
// sanitize the input:
switch(toLower(charReturn))
{
// and so on
}
// before
printf("Please enter the quantity of Item 1 produced this week: ");
scanf_s("%f", &quantityItem1);
printf("-----------------------------------------------\n");
printf("Please enter the quantity of Item 2 produced this week: ");
scanf_s("%f", &quantityItem2);
// after
const char* separator = "-----------------------------------------------\n";
// something along these lines, a little rough
float readFloat(char* message){
float target;
do{
printf(message);
}while(!scanf_s("%f", &target))
return target;
}
quantityItem1 = readFloat("Please enter the quantity of Item 1 produced this week: ");
printf(separator);
quantityItem2 = readFloat("Please enter the quantity of Item 2 produced this week: ");
// very much pseudo code, just to demonstrate the gist
// now
mainMenu()
case 1: showA()
case 2: showB()
case q: quit;
float showA()
someOutput()
var target = readSomeInputs()
case r: mainMenu() // recursive
return target;
float showB() // see showA
So what happens to your stack:
User enters: stack = mainMenu
input 1: stack = mainMenu, showA
input r: stack = mainMenu, showA, mainMenu
input 2: stack = mainMenu, showA, mainManu, showB
you get the picture.
The code should rather work like so:
mainMenu()
case 1: showA(&target)
case 2: showB()
case q: quit;
void showA(float* target)
someOutput()
target = readSomeInputs()
case r: return
case e: totalResult()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment