问题:输入0后,显示的交换的两个字符串不对。
#include <iostream>
#include <cstring>
#define MAX 20
using namespace std;
template <class Anytype>
void exchange(Anytype &a , Anytype &b);
template <class T>
void exchange(T a[], T b[]);
template <typename T>
void display(T a, T b);
int main(){
int i = 0;
cin >> i;
if(i != 0){
cout << "Please enter two fractions\n";
double test1 = 0 , test2 = 0;
cin >> test1 >> test2;
exchange(test1,test2);
display(test1,test2);
}else{
cout << "please enter two strings\n";
char a[MAX]{'\0'},b[MAX]{'\0'} ;
cin.getline(a,MAX);
cin.get();
cin.getline(b,MAX);
cin.get();
exchange(a,b);
display(a,b);
}
cin.get();
return 0;
}
template <class T>
void exchange(T &a , T &b){
T temp;
temp = a;
a = b;
b = temp;
}
template <class T>
void exchange(T a[], T b[]){
for(int i = 0; i < strlen(a)|| i < strlen(b); i++){
auto temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
template <typename T>
void display(T a, T b){
cout << "a = " << a <<endl;
cout << "b = " << b <<endl;
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/370912356736185.png "#left")
已经将第二个函数模版用指针的方式进行修改无效。
不知道是哪里错了