A question about applying pointers and it makes a loop infinite

61 views Asked by At

I'm new to programming and doing a calculator, that an use the program to compute another set of numbers and after doing loops for input controls, I decided to have a memory management because the previous input just keeps stack with the new input number.

I did try to apply some pointer and memory management but it just makes the first for loop infinitely loop. But if I remove the pointers it makes the program ok but the previously calculated will be added to the new calculated value.

#include <iostream>
#include <cstdlib>
using namespace std;

int main(){

    //variables
    float a[1000],c;
    char d,e;
    int f = 1,g;

    //pointers
    float *b;

    do{
        system("CLS");

        cout << endl;
        cout << "  Welcome!!!" << endl;
        cout << endl;
        cout << "  This is a calculator (obviously)" << endl;
        cout << endl;
        cout << "  Please input how many entities to be calculated: ";
        cin >> g;
        cout << endl;
        cout << endl;

        for(int h = 0;h < g; h++){
            cout << "  Input numbers: ";
            cin >> a[1000];
        }

        cout << endl;
        cout << "  Choose the LETTER corresponding to the operation below" << endl << endl;
        cout << "   A - Addition" << endl;
        cout << "   S - subtraction" << endl;
        cout << "   M - Multiplication" << endl;
        cout << "   D - Divison" << endl << endl;

        cout << "  Choose operation to be used: ";
        cin >> d;
        d = toupper(d);

        if((d != 'A') && (d != 'S') && (d != 'M') && (d != 'D')){
            do {
            cout << "  Choose operation from [A - S - M - D] respectively: ";
            cin >> d;
            d = toupper(d);
            }while((d != 'A') && (d != 'S') && (d != 'M') && (d != 'D'));
        }

        switch (d){
            case 'A':
                for(int h = 0;h < g; h++){
                    c +=a[1000];
                }
                cout << "  sum is " << c << endl;
                break;
            case 'S':
                for(int h = 0;h < g; h++){
                    c -=a[1000];
                }
                cout << "  difference is " << c << endl;
                break;
            case 'M':
                for(int h = 0;h < g; h++){
                    c *=a[1000];
                }
                cout << "  product is " << c << endl;
                break;
            case 'D':
                for(int h = 0;h < g; h++){
                    c /=a[1000];
                }
                cout << "  quotient is " << c << endl;
                break;
        }

        do{
            cout << endl;
            cout << "  Would you like to calculate again? [Y/N] ";
            cin >> e;
            e = toupper(e);
        }while ((e !='N') && (e != 'Y'));

        if (e == 'Y'){
            // Announce pointer and deletion of values ; also to clear memory on new start.
            //
        }
        else{
            f = 0;
        }
    }while (f == 1);

return 0;
}
1

There are 1 answers

0
463035818_is_not_an_ai On

I dont really understand what pointers or memory management you are refering to. However, this

cin >> a[1000];

is wrong. An array with 1000 elements has the last valid index as 999. You are accesing the array out of bounds which invokes undefined behaviour. Anything could happen (well not really but its best to think of it that way).

The correct loop should be ("correct" as in "minimum changes to avoid the ub"):

for(int h = 0;h < g; h++){
    cout << "  Input number: ";
    cin >> a[h];
}

However, this will also cause trouble when the user enters a number bigger than 1000 for g. What you really should use is a std::vector that lets you push elements as many as you like without having to specifiy its size at compile time.

PS: If your code is fine without pointers or manual memory managment, then there is no reason to add it. Pointers and (manual) memory managment are complicated, learning to deal with it is mainly about learning how to avoid it.