yty2021 2021-11-23 15:38 采纳率: 42.9%
浏览 13
已结题

为什么这个程序显示交换的两个字符串始终有错误?

问题:输入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")

已经将第二个函数模版用指针的方式进行修改无效。
不知道是哪里错了

  • 写回答

1条回答 默认 最新

  • 真相重于对错 2021-11-23 16:48
    关注
    
    直接cin>>a;
    cin>>b;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月2日
  • 已采纳回答 11月24日
  • 创建了问题 11月23日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器