根据每个字符比较同一数组中的两个字符串值.此结果仅显示Last OUT值的原因

compare two string value in same array based on each char . this result only showing Last OUT value why

本文关键字:结果 字符串 显示 OUT Last 两个 比较 字符 数组      更新时间:2023-10-16
#include <iostream>
#include <string>
using namespace std;

void Calc( const string, const string , string&);
void Dbf(const string,const string, string&);

int main ()
{
    string OUT;
    string A[]={"10","12"};
    string j = A[0];
    string k = A[1];
     Calc(j,k,OUT);
        cout<< "ODD  :"<<j<<endl;
        cout<< "EVEN :"<<k<<endl;
        cout<< "OUT  :"<<OUT<<endl;
        cout<< "OUT  :"<<OUT<<endl;
// this result only showing Last OUT value why pls help
  return 0;
}
void Calc( const string J ,const string K, string& Out){
        int OD_L = J.length();
        int EV_L = K.length();
        int D = (OD_L - 1);
        int E = (EV_L - 1);
            for(int io = D ; io >= 0; io-- ){
            string OD(1, J[io]);
            string EV(1, K[io]);
            Dbf( OD , EV, Out);
            }
}
void Dbf(const string S_odd, const string S_even, string& p_Out){
    if(S_odd == S_even){
        p_Out = " yes ";
    }else{
        p_Out = " no " ;
    }
}

您似乎正在尝试比较两个字符串,以产生"yes"answers"no"的长结果。在Dbf中,您应该将字符串附加到p_Out,而不是覆盖它。尝试:

void Dbf(const string S_odd, const string S_even, string& p_Out){
    if(S_odd == S_even){
        p_Out += " yes ";
    }
    else{
        p_Out += " no " ;
    }
}