C++ 字符串重新排列C++;
我拉一个文本文件-它将由用户输入 目前它只是从C:\Dump\test.txt中提取 我想把文件里的字重新排列一下 这就是我目前所拥有的C++ 字符串重新排列C++;,c++,string,function,stringstream,C++,String,Function,Stringstream,我拉一个文本文件-它将由用户输入 目前它只是从C:\Dump\test.txt中提取 我想把文件里的字重新排列一下 这就是我目前所拥有的 static string revFunc(string a) { ifstream inp; inp.open(a, std::ios::in); string lines; while(getline(inp, lines)){ istringstream iss(lines); strin
static string revFunc(string a)
{
ifstream inp;
inp.open(a, std::ios::in);
string lines;
while(getline(inp, lines)){
istringstream iss(lines);
string outstr;
string word;
iss >> outstr;
if (inp >> word){
outstr = word + ' ' + outstr + ' ';
cout << outstr;
}
}return 0;
}
静态字符串revFunc(字符串a)
{
ifp;
输入打开(a,std::ios::in);
弦线;
while(getline(inp,line)){
istringstream iss(线);
弦外之音;
字符串字;
国际空间站>>突出;
如果(inp>>word){
outsr=单词+outsr+“”;
cout您应该删除返回0
并用有效的字符串替换它
我认为您应该在revFunc
函数的末尾使用return outtr;
,而不是return 0;
另外,在(…)
的同时,将字符串移出:
静态字符串revFunc(字符串a)
{
ifp;
输入打开(a,std::ios::in);
弦线;
弦外之音;
while(getline(inp,line)){
istringstream iss(线);
字符串字;
国际空间站>>突出;
如果(inp>>word){
outsr=单词+outsr+“”;
您可以说您的refFunc
返回一个字符串
,但返回值是0
(一个int
,而不是字符串
)
当程序尝试使用字符串对象并查找“垃圾”(在预期字符串时已分配,但尚未填充正确的字符串位)时,这将导致出现问题
要修复此问题,您需要将返回类型更改为int
,或将返回值更改为有效字符串(例如“”
)
或者,如果您不使用返回值(如示例中的情况),则可以将返回类型更改为void
。在这种情况下,只使用return;
(注意:返回后没有值),或者可以省略整个return语句
注意:为防止将来出现此类错误,请将编译器配置为使用“最大警告”设置进行编译,并可能将警告转换为错误。这将防止您在编译程序时,编译器至少不会告诉您(很可能)有错误的操作
注意:即使在暂停应用程序时也不要养成使用system()
的习惯。它依赖于平台,可能会带来安全风险(尽管我认为您暂时不会担心这一点)您可以发布完整的代码吗?尽量将其最小化-在revFunc()末尾返回0
是什么
?您没有返回有效的字符串。我在if循环中有return-outtr;,但它只会返回一行反向字符串并退出-因此我将return 0放在那里,我想除了返回一些东西之外,没有其他原因了。呃-我讨厌犯这样的愚蠢错误-我改为return“”;不再显示错误,但仍然只显示2行反转可能只有两行中至少有2个单词?不-它们都有2个以上-大多数都超过5个左右我将if循环更改为while循环,并打印出所有重新排列的单词(大约100倍ha)我改为return outsr;=未澄清的标识符移动到while循环中,只给出了两个单词reversedOK,这就是你的问题所在:如果你在while中移动了return,那么你在第一次迭代后就从while中退出了。(请参阅Masoud关于如何修复你是否想使用他们的建议的编辑)这和return一样有效“”-我拉线的方式有点问题,我相信我得到的重新排列的单词在整个文档中都有-哦,好吧,谢谢,伙计们!我会从那里找到答案的,哈哈
int main(){
string file;
int words = 0;
int lines = 0;
int choice;
system("cls");
cout << "Enter Filename: " << endl;
file = "C:\\Dump\\test.txt";
cout << file;
//cin >> file;
cout <<"MENU"<<endl;
cout <<"----------------------------------" << endl;
cout << "1. Count Words" << endl;
cout << "2. Count Lines" << endl;
cout << "3. Index Words" << endl;
cout << "4. Average Length of Words" << endl;
cout << "5. Print Text File to Screen" << endl;
cout <<"----------------------------------" << endl;
cout << "??: ";
cin >> choice;
switch(choice){
case 1: cout << "Words: " << wordFunc(file) << endl;
system("PAUSE");
break;
case 2: cout << "Lines: " << lineFunc(file, lines) << endl;
system("PAUSE");
break;
case 3: indexFunc(file);
system("PAUSE");
break;
case 4: cout << "Average Length: " << avgWordFunc(file) << endl;
system("PAUSE");
break;
case 5: printStrFunc(file);
cout <<endl;
system("PAUSE");
break;
case 6: revFunc(file);
cout << endl;
system("PAUSE");
break;
}
return 0;
}
static string revFunc(string a)
{
ifstream inp;
inp.open(a, std::ios::in);
string lines;
string outstr;
while(getline(inp, lines)){
istringstream iss(lines);
string word;
iss >> outstr;
if (inp >> word){
outstr = word + ' ' + outstr + ' ';
cout << outstr;
}
}return outstr;
}