首页 > 解决方案 > 如何使用指针存储值以及如何显示所有存储的值?

问题描述

所以我有这个程序来存储用户将输入的所有数据。我正在使用一个指针,我对如何存储它们以及如何显示所有它们有点困惑。这是我的代码:

#include<iostream>
using namespace std;
int main(){
    int num1, num2, result, range;
    int *ptr1 = &num1, *ptr2 = &num2;
    char operation, answer;
    char *ptrop = &operation;
    while(true){
    cout<<"ENTER TWO NUMBERS: \n";
    cin>>*ptr1>>*ptr2;
    cout<<endl;
    cout<<"CHOOSE OPERATION: ";
    cin>>operation;
    switch(operation){
        case '+':
            result = *ptr1 + *ptr2;
            cout<<"Asnwer is "<<result<<endl;
            break;
        case '-':
                result = *ptr1 - *ptr2;
                cout<<"Asnwer is "<<result<<endl;
            break;
        default:
            cout<<"NONE\n\n";
    }
    cout<<"\nWANT TO TRY AGAIN? ";
    cin>>answer;
    switch(answer){
        case 'Y':
        case 'y':
            ptr1++;
            ptr2++;
            range ++;
            system("cls");
            continue;
        default:
            cout<<"VIEW HISTORY?";
            cin>>answer;
                switch(answer){
                    case 'Y':
                    case 'y':
                        for(int i=0;i<=range;i++){
                            cout<<"ADDRESS OF "<<*ptr1<<" is "<<ptr1<<endl;
                            cout<<"ADDRESS OF "<<*ptr2<<" is "<<ptr2<<endl<<endl;
                        }
                        break;
                    default:
                        return 0;
                }
    }
    }
}

我正在尝试存储所有数据,但是当我查看历史记录时,它显示相同的数字。我很困惑,我不知道如何解决这个问题

标签: c++pointersincrement

解决方案


您应该稍微修改一下逻辑,但想法是:您定义将存储您的值的动态数组。对于每个操作,将值存储在此数组的某个位置,然后递增指针。要查看历史记录,您从第一个位置开始并迭代到最后。

#include<iostream>
using namespace std;
int main(){

    int result;
    int range = 0; 
    int max_range = 10; // maximum number of operations to do, you could ask this to the user...
    int* num1 = new int[max_range]; // arrays instead of simple ints
    int* num2 = new int[max_range];
    int *ptr1 = &num1[0]; // initialize the pointers to the first position
    int *ptr2 = &num2[0];

    char operation, answer;
    char *ptrop = &operation;
    while(true){
        cout<<"ENTER TWO NUMBERS: \n";
        cin>>*ptr1>>*ptr2;
        cout<<endl;
        cout<<"CHOOSE OPERATION: ";
        cin>>operation;
        switch(operation){
        case '+':
            result = *ptr1 + *ptr2;
            cout<<"Asnwer is "<<result<<endl;
            break;
        case '-':
            result = *ptr1 - *ptr2;
            cout<<"Asnwer is "<<result<<endl;
            break;
        default:
            cout<<"NONE\n\n";
        }
        cout<<"\nWANT TO TRY AGAIN? ";
        cin>>answer;
        switch(answer){
        case 'Y':
        case 'y':
            ptr1++; // the pointers are updated to the next position
            ptr2++;
            range ++; // increase the number of operations done.
            system("cls");
            continue;
        default:
            cout<<"VIEW HISTORY?";
            cin>>answer;
            switch(answer){
            case 'Y':
            case 'y':
                ptr1 = &num1[0]; // reset the pointers to the beginning of the arrays
                ptr2 = &num2[0];
                for(int i=0;i<=range;i++){
                    cout<<"ADDRESS OF "<<*ptr1<<" is "<<ptr1<<endl;
                    cout<<"ADDRESS OF "<<*ptr2<<" is "<<ptr2<<endl<<endl;
                    ptr1++; // VERY IMPORTANT!! This moves the pointer to the next value.
                    ptr2++;
                }
                break;
            default:
                return 0;
            }
        }
    }
    delete [] num1; // always delete dynamic arrays.
    delete [] num2;
    return 0;
}

推荐阅读