I know this is a Lua site, but after 2 Hours of building such a fine structure i will need some tips to make a better layout.
#include <iostream> #include <cstdlib> #include <cstdio> using namespace std; void extract(void) { int accu = 0; for(;;) { cout << "Enter Number:" << endl; int su = 0; cin >> su; if(su < 0) { cout << "Terminated" << endl; break; } else { int total = su - accu; cout << su << " - " << accu << endl; cout << "Total: " << total << endl; accu = total; } } } void sum(void) { int accu = 0; for(;;) { cout << "Enter Number:" << endl; int su = 0; cin >> su; if(su < 1) { cout << "Terminated" << endl; break; } else { int total = su + accu; cout << su << " + " << accu << endl; cout << "Total: " << total << endl; accu = total; } } } void Expl(void) { cout << "Welcome to Byteware TM Sequence Calculator \n" << "End each sequence with a number below zero (0) or Negative (-) \n" << "Please, Choose an Option:\n" << "1 for + \n" << "2 for -" << endl; } void opt(void) { int op; cin >> op; if(op == 1) { sum(); } if(op == 2) { extract(); } } int main() { Expl(); opt(); }
Looking at least at sum
, there isn't much you can do to simplify the code.
You can eliminate the declaration of total
though:
void sum(void) { int total = 0; for(;;) { cout << "Enter Number:" << endl; int term = 0; cin >> term; if (term >= 1) { cout << term << " + " << accu << endl; total += term; cout << "Total: " << total << endl; } else { cout << "Terminated" << endl; break; } } }
Note how much cleaner this looks with the braces cuddled (on the same line as the appropriate terms)
extract
is identical to sum
except that the final result is negative, so there isn't really a point in implementing it at all.
EDIT: I copied in the wrong code! Sorry! It's fixed now
Closed as off-topic by User#24403
This question has been closed by our community as being off-topic from ROBLOX Lua Scripting.
Why was this question closed?