又要开始学习了吗? 2021-03-23 18:16 采纳率: 25%
浏览 62
已采纳

不知道为啥触发断点了

#include <iostream>
#include <cctype>
#include <string>
#include <fstream>

char* functionC(char* destination, const char* source)
{
	int k = 0;
	for (int i = 0; source[i] != '\0'; i++)
	{
		if (!isdigit(source[i])) //isDigit проверяет число ли
		{
			destination[k] = source[i];
			k++;
		}
	}
	destination[k] = '\0';
	return destination;
}

std::string functionString(const std::string& source)
{
	std::string destination = ""; //строка для вывода输出线,输出线
	for (int i = 0; i < source.size(); i++)
	{
		if (!isdigit(source[i]))
		{
			destination += source[i];
		}
	}
	return destination;
}


int main()
{

	std::ifstream infile;
	std::ofstream outfile;

	infile.open("infile.txt");
	outfile.open("outfile1.txt");

	if (infile.peek() == std::ifstream::traits_type::eof()) //проверка на пустой файл
	{
		outfile << "File is empty!\n";
		return 1;
	}

	int length; //длина строки长字符串
	infile >> length;
	while (!infile.eof())
	{
		length++; // учитываем символ \0考虑到符号
		if (infile.fail() || length <= 0)
		{
			outfile << "\n";
			return 1;
		}
		char* arrayC1 = new char[length];
		char* arrayC2 = new char[length];
		arrayC2[0] = '\0';
		infile.ignore(length, '\n');
		infile.get(arrayC1, length);
		for (int i = 0; i < length - 1; i++)
		{
			if (arrayC1[i] == '\0')
			{
				outfile << "Invalid string length\n";
				return 1;
			}
		}
		std::string str = arrayC1;
		outfile << "Source string\t\t\t\t" << str << '\n';
		outfile << "With C-style strings:\t" << functionC(arrayC2, arrayC1) << '\n';
		outfile << "Using the string class:\t" << functionString(str) << '\n';
		delete[] arrayC1;
		delete[] arrayC2;
		infile >> length;
	}
	infile.close();
	outfile.close();
	return 0;
}

if (!isdigit(source[i])) 这里触发了断点不知道为什么

  • 写回答

2条回答 默认 最新

  • 泡视界 2021-03-23 18:43
    关注

    你 arrayC1里面没有 ‘\0’,

    传到functionC里面,循环一直跑,i一直加,arrayC1指向的char数组会越界的吧。

    你在functionC里面加一个答应就知道错在哪一个字符了呀

    std::cout<<i<<": "<<source[i]<<std::endl;

     

    如果分析对了请采纳!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?