Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

C++ Sequence Calculator Tips?

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


4 comments

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).

0

Thanks for the tip, it's just a tiny software in which adds or substracts numbers in a pattern.

0

OOPS: I pasted in the wrong code tomy answer, it's fixed now! My apologies!

0

It's ok, don't worry.

1 answer

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

2 comments

0

Thanks for the tip, i'm just a beginner at C++ looking for tips from the experienced.

0

I noticed the changes, Thanks for your help!

This discussion is preserved as it was. New answers, comments, and votes are closed.