c++ - 如何使用指针存储值以及如何显示所有存储的值?
问题描述
所以我有这个程序来存储用户将输入的所有数据。我正在使用一个指针,我对如何存储它们以及如何显示所有它们有点困惑。这是我的代码:
#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;
}
}
}
}
我正在尝试存储所有数据,但是当我查看历史记录时,它显示相同的数字。我很困惑,我不知道如何解决这个问题
解决方案
您应该稍微修改一下逻辑,但想法是:您定义将存储您的值的动态数组。对于每个操作,将值存储在此数组的某个位置,然后递增指针。要查看历史记录,您从第一个位置开始并迭代到最后。
#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;
}
推荐阅读
- node.js - node.js:错误:错误:0B080074:x509 证书例程:X509_check_private_key:键值不匹配
- python - 根据变量对字符串进行子集化
- javascript - 非字母数字字符串的 Number.parseInt 给出 typeof 'number'
- excel - 如何在当前文件路径中打开文件夹中包含的文件
- spring-boot - Spring Boot 文件转换微服务作为 Cloud Foundry 用户提供的服务
- python - 过滤多对多关系结果(flask sqlalchemy)
- c# - 如何并行调用数千次异步方法
- google-custom-search - 将 Google 自定义搜索布局从移动设备更改为桌面设备?
- advanced-custom-fields - wp_mail 在 acf/pre_save_post 或 acf/save_post 中不起作用
- mysql - 选择多个相似的行