Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

C++ Sequence Calculator Tips? [closed]

Asked by
Xianon 105
9 years ago

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();
}


0
Could you briefly explain what this is supposed to be doing? Also note that your code will look MUCH better if you put the opening curly bracket on the line with whatever it belongs to (called cuddling). BlueTaslem 18071 — 9y
0
Thanks for the tip, it's just a tiny software in which adds or substracts numbers in a pattern. Xianon 105 — 9y
0
OOPS: I pasted in the wrong code tomy answer, it's fixed now! My apologies! BlueTaslem 18071 — 9y
0
It's ok, don't worry. Xianon 105 — 9y

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?

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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

0
Thanks for the tip, i'm just a beginner at C++ looking for tips from the experienced. Xianon 105 — 9y
0
I noticed the changes, Thanks for your help! Xianon 105 — 9y
Ad