What's new

Closed Calculator c++ ( program outputs large numbers ) help !

Status
Not open for further replies.

PHC-KILATIS12

Eternal Poster
Joined
Apr 4, 2017
Posts
705
Reaction
432
Points
314
Age
29
So yun na nga guys gumagawa ako ng program ngayon since na figure out ko na kung pano yung user mismo ang magdedecide kung ilang numbers yung gusto nyang i add/subtract etc..

pero nung ni run ko na yung program sobrang laki nung numbers na ina output nya which is hindi naman dapat kase sa separate program na ginawa ko gumagana naman ng maayos yung program na ginawa ko .


so eto yung program ko na may error ( outputs large numbers )


C++:
#include <iostream>
#include <conio.h>
#include <windows.h>


using namespace std;

void gotoxy(short x,short y);


int addition();
int subtraction();
int    multiplication();
int division();
int    percentage();
int square();

int choice;


int main(){
    
    
    start :
    
    cout<<"\t\t\t\t\t\t  CALCULATOR"<<endl;
    cout<<"\t\t_______________________________________________________________________________"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|               1. ADD                           3. MULTIPLY                  |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|               2. SUB                           4. DIVIDE                    |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|               5. PERCENT                       6. SQUARE                    |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|_____________________________________________________________________________|"<<endl;
    
    gotoxy(37,12);
    cout<<"SELECT ( invalid will exit ):";
    cin>>choice;
    system("cls");
    
    switch(choice)
    {
        case 1 :
            
            system("cls");
            addition();
            getch();
            break;
            
        case 2 :
            
            system("cls");
            subtraction();
            getch();
            break;
            
        case 3 :
            
            system("cls");
            multiplication();
            getch();
            break;
            
        case 4 :
            
            system("cls");
            division();
            getch();
            break;
            
        case 5 :
            
            system("cls");
            percentage();
            getch();
            break;
            
        case 6 :
            
            system("cls");
            square();
            getch();
            break;
            
        default :
            
            return 0;
    }
    
}


int addition()
{
    
    int b=0;
    int a[10];
    long sum;
            
    cout<<"\t\t\t\t\t\t  CALCULATOR"<<endl;
    cout<<"\t\t_______________________________________________________________________________"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t_______________________________________________________________________________"<<endl;
    
    gotoxy(26,5);
    cout << " Enter how many numbers you want to add ( 10 is max ) : " ;
    cin >> b ;
    
    system("cls");
    
    cout<<"\t\t\t\t\t\t  CALCULATOR"<<endl;
    cout<<"\t\t_______________________________________________________________________________"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t_______________________________________________________________________________"<<endl;
    
    
    for(int i=0;i<b;i++)
    {
        gotoxy(32,3+i);
        cout << " Enter number :" ; cin >> a[i];
        gotoxy(32,13);
        sum = a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9];
        cout << " SUM :" << sum ;
}
}


int subtraction()
{
    
    int b=0;
    int a[10];
    int sum;
            
    cout<<"\t\t\t\t\t\t  CALCULATOR"<<endl;
    cout<<"\t\t_______________________________________________________________________________"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t_______________________________________________________________________________"<<endl;
    
    gotoxy(26,5);
    cout << " Enter how many numbers you want to add ( 10 is max ) : " ;
    cin >> b ;
    
    system("cls");
    
    cout<<"\t\t\t\t\t\t  CALCULATOR"<<endl;
    cout<<"\t\t_______________________________________________________________________________"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t|                                                                             |"<<endl;
    cout<<"\t\t_______________________________________________________________________________"<<endl;
    
    
    for(int i=0;i<b;i++)
    {
        
        gotoxy(32,3+i);
        cout << " Enter number :" ; cin >> a[i];
        gotoxy(32,13);
        sum = a[0]-a[1]-a[2]-a[3]-a[4]-a[5]-a[6]-a[7]-a[8]-a[9];
        cout << " difference :" << sum ;
}
}


int    multiplication()
{
    
}


int division()
{
    
}


int    percentage()
{
    
}


int square()
{
    
}


void gotoxy(short x ,short y)
{
    COORD pos={x,y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}


and then eto naman yung separate program na ginawa ko nung una at gumagana naman ng maayos kaya nababaliw na ko kakaisip kung anong problema dun sa unang program na binigay ko kase same lang talaga sila pramis HAHAHAH .

C++:
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

int b=0;
int a[10];
int sum;
void gotoxy(short x,short y);

int main()
{
    
    
    cout << " Enter how many numbers you want to add ( 10 is max ) : " ;
    cin >> b ;
    
    for(int i=0;i<b;i++)
    {
        
        cout << "  Addition " << endl;
        gotoxy(32,3+i);
        cout << " Enter number :" ; cin >> a[i];
        gotoxy(32,14);
        sum = a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9];
        cout << " SUM :" << sum ;


        
    }
    
    
}

void gotoxy(short x ,short y)
{
    COORD pos={x,y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}

so yun lang guys sa mga critical thinker dito pa help naman salamuch <3
 
dalawang for loop nalang gawin mo lods..
unang for loop, yung paglalagay ng value sa array.

tapos sa pangalawang loop.

Python:
sum = a[0]

i = 1

while i  < b:

   sum = sum - a[i]

   i+=1

print sum

pwede mo rin isama sa isang for loop lagyan mo nalang ng condition. para isang loop ka nalang.

Python:
if i == 0:

    sum = a[0]

else:

    sum = sum - a[i]


senxa na, python yung code. pero sana na gets mo.
tsaka wag mo gawin tong "sum = a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9];"

limitado ka jan sa 10..
what if pinili ni user 7.. edi mag aadd sya sa a[7] to a[9] na walang laman.
 
Last edited:
dalawang for loop nalang gawin mo lods..
unang for loop, yung paglalagay ng value sa array.

tapos sa pangalawang loop.

Python:
sum = a[0]

i = 1

while i  < b:

   sum = sum - a[i]

   i+=1

print sum

pwede mo rin isama sa isang for loop lagyan mo nalang ng condition. para isang loop ka nalang.

Python:
if i == 0:

    sum = a[0]

else:

    sum = sum - a[i]


senxa na, python yung code. pero sana na gets mo.
tsaka wag mo gawin tong "sum = a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9];"

limitado ka jan sa 10..
what if pinili ni user 7.. edi mag aadd sya sa a[7] to a[9] na walang laman.
lods tama lang yung program sa pangalawa ehh try mo i run boss para malaman mo yung inis ko HAHAHAHA , kase dun lang talaga ako nagkaproblema sa unang progran na nilagay ko which is sa pangalawa okay naman . but i will try your suggestion boss magnenested for loop nalang ako

btw dun sa sum na sinabi mo tama lang yun , kase naka loop naman pag pinili ng user na 7 hanggang 7 lang talaga yung i eexecute ng program
 
try ko nalang sa c++ kahit di ako marunong.. considered as praktis din to.
mali pag kakaintindi mo..

inefficient kasi ang code na to "sum = a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9];"
what if sasabihin ng teacher gusto ko 100 naman, ayoko ng sampu.

C++:
for(int i=0;i<b;i++)
    {
      
        cout << "  Subtraction" << endl;
        gotoxy(32,3+i);
        cout << " Enter number :" ;
        cin >> a[i];
        gotoxy(32,14);
        if (i == 0){
            sum = a[i];
        }
        else {
            sum = sum - a[i];
        }
        cout << " Diff :" << sum ;


check mo nalang yung syntax ng if else. di ako marunong ng c++
 
Last edited:
try ko nalang sa c++ kahit di ako marunong.. considered as praktis din to.
mali pag kakaintindi mo..

inefficient kasi ang code na to "sum = a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9];"
what if sasabihin ng teacher gusto ko 100 naman, ayoko ng sampu.

C++:
for(int i=0;i<b;i++)
    {
      
        cout << "  Addition " << endl;
        gotoxy(32,3+i);
        cout << " Enter number :" ;
        cin >> a[i];
        gotoxy(32,14);
        if (i == 0){
            sum = a[i];
        }
        else {
            sum = sum - a[i];
        }
        cout << " SUM :" << sum ;

nagraramdom program lang ako boss since wala pa kaming pasok incoming 2nd year palang , kaya di pa gaanong hasa yung utak HAHAHA kaya yan lang muna ginawa ko .

Na solve ko na pala yung problem , kapag naka global declaration yung variables working sya pero pag naka local declaration hindi sya gumagana at yun ang hindi ko maintindihan .

wag nyo muna pansinin yung hindi ko pagiging proficient sa pag gamit ng mga equations 😂

check mo nalang yung syntax ng if else. di ako marunong ng c++
 
ok yan lods. at least nag papraktis ka kahit wala pang pasok. senxa na hindi naman ako programmer, nag-aaral lang ako ng python for bot making.
tuloy mo lang yan. wag susuko
 
Comments ko lang sir:

yung menu mo can be simplified and doesn't have to be repeated several times.
no need to use an array to hold inputted numbers. They can be added straightaway.
yung clear screen system call is unnecessary. You can let the compiler do that for you.

NOTE: walang input validation yung sample code ko below

C++:
#include <iostream>
using namespace std;

const std::string mymenu {
        "\t\t\t\t\t\t  CALCULATOR\n \
    \t\t________________________________________________________________________________\n \
    \t\t|                                                                              |\n \
    \t\t|               1. ADD                           3. MULTIPLY                   |\n \
    \t\t|                                                                              |\n \
    \t\t|               2. SUB                           4. DIVIDE                     |\n \
    \t\t|                                                                              |\n \
    \t\t|               5. PERCENT                       6. SQUARE                     |\n \
    \t\t|                                                                              |\n \
    \t\t|______________________________________________________________________________|\n \
        "
};

void do_addition() {
        int num_inputs{ 0 };
        int current{ 0 };
        int total{ 0 };
        std::cout << "How many numbers you want added together: ";
        std::cin >> num_inputs;
        for (int i=0; i<num_inputs; i++) {
                std::cout << "Enter num: ";
                std::cin >> current;
                total += current;
        }
        std::cout << "The sum is: " <<  total << std::endl;
}

void do_subtraction() {
}

void do_multiplication() {
}

void do_division() {
}

void do_percent() {
}

void do_square() {
}

int get_operation() {
        std::cout << "\033[2J\033[1;1H";
        int choice;
        std::cout << mymenu << std::endl;
        std::cout << "SELECT (Invalid will exit): ";
        std::cin >> choice;
        return choice;
}

void do_operation(int op) {
        switch(op) {
                case 1:
                        do_addition();
                        break;
                case 2:
                        do_subtraction();

                        break;
                case 3:
                        do_multiplication();
                        break;
                case 4:
                        do_division();
                        break;
                case 5:
                        do_percent();
                        break;
                case 6:
                        do_square();
                        break;
                default:
                        std::cout << "Unknown operation" << std::endl;
                        break;
        }
}

int main() {
        int choice = get_operation();
        do_operation(choice);

        return 0;
 
Last edited:
Status
Not open for further replies.

Similar threads

Back
Top