注册 登录
编程论坛 C++教室

谁能帮我看下这程序出什么问题了,先谢谢了!

ToMoRRoWa 发布于 2010-05-02 16:47, 503 次点击
只有本站会员才能查看附件,请 登录

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <cerrno>

using std::cin;
using std::cout;
using std::endl;
using std::setw;

int _tmain(int argc, _TCHAR* argv[])
{
    const int MAX = 50;
    char Str1[MAX];
    char Str2[MAX];
    char* instead[MAX];
   
    cout << "请输入目标字符串:";
    cin.getline( Str1,MAX,'\n');
    cout << "请输入源字符串:";
    cin.getline( Str2,MAX,'\n');
   
    int count = 0;
    while( Str2 != 0 )
    {
        count ++;
        instead[count] = &Str2[count];
    }
    errno_t error = strcat_s(Str1,MAX,*instead);

    if( error == 0 )
    {
        cout << "恭喜,字符串拼接成功!"
             << endl
                << "拼接结果:" << strcat(Str1,*instead);
    }
    else
        if( error == EINVAL )
        cout << "字符串拼接失败,原因是目标或源为NULL";
    else
        if( error == ERANGE)
        cout << "字符串拼接失败,原因是目标长度太短";
    cout << endl
         << "Press any key . . .";
    _getch();
    return 0;
}
3 回复
#2
2010-05-02 17:16
你的程序没有给全!
#3
ToMoRRoWa2010-05-02 17:30
// 拼接字符串.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <cerrno>
#include <cstring>

using std::cin;
using std::cout;
using std::endl;
using std::setw;

int _tmain(int argc, _TCHAR* argv[])
{
    const int MAX = 50;
    char Str1[MAX];
    char Str2[MAX];
    char* instead[MAX];
    int point = NULL;
   
    cout << "请输入目标字符串:";
    cin.getline( Str1,MAX,'\n');
    cout << "请输入源字符串:";
    cin.getline( Str2,MAX,'\n');
   
    int count = 0;
    while( point != '\0');
    {
        count ++;
        instead[count] = &Str2[count];
        point = Str2[count];
    }
    errno_t error = strcat_s(Str1,MAX,*instead);

    if( error == 0 )
    {
        cout << "恭喜,字符串拼接成功!"
             << endl
                << "拼接结果:" << strcat(Str1,*instead);
    }
    else
        if( error == EINVAL )
        cout << "字符串拼接失败,原因是目标或源为NULL";
    else
        if( error == ERANGE)
        cout << "字符串拼接失败,原因是目标长度太短";
    cout << endl
         << "Press any key . . .";
    _getch();
    return 0;
}
#4
玩出来的代码2010-05-02 23:57
程序代码:
    int count = 0;
    point=Str2[count];         
    while( point != '\0')
    {
        instead[count] = &Str2[count];     //改成这样吧,你那个instead[0]并没有指向,count++放在后面
        count ++;
        point = Str2[count];
    }
1