C++ 在字符串的最后一个字符中搜索标点符号
我目前正在制作一个小型文字处理器,我遇到了一个问题。我正在尝试制作程序,这样当你按enter键时,程序会在你的输入上加一个句号,然后加一个空格(我稍后会整理段落)。问题是,我无法让程序搜索最后一个字符的标点符号,然后根据结果进行分支。编译器给了我以下的错误:<强> ISO C++禁止指针和整数的比较。<强>我现在的代码是:C++ 在字符串的最后一个字符中搜索标点符号,c++,if-statement,C++,If Statement,我目前正在制作一个小型文字处理器,我遇到了一个问题。我正在尝试制作程序,这样当你按enter键时,程序会在你的输入上加一个句号,然后加一个空格(我稍后会整理段落)。问题是,我无法让程序搜索最后一个字符的标点符号,然后根据结果进行分支。编译器给了我以下的错误: ISO C++禁止指针和整数的比较。我现在的代码是: #include <fstream> #include <string> #include <iostream> #include <cstr
#include <fstream>
#include <string>
#include <iostream>
#include <cstring>
#include <limits>
using namespace std ;
int main()
{
int i = 0;
string text ;
string text2 ;
string title ;
string usertitle ;
string filelocation = "C:/Users/" ;
string user ;
string punctuation = ". : ! ? "
cout << "Input a title for your file: " ;
getline(cin , title) ;
title.insert(title.length() , ".txt" ) ;
cout << "Your title is: " << title << endl ;
cout << endl << "Input the username associated with your computer: " ;
getline(cin , user) ;
filelocation.append( user ) ;
filelocation.append("/Documents/") ;
filelocation.append(title) ;
cout << "Your chosen file name and location is: " << filelocation << endl ;
for ( i = 1 ; i > 0 ; i++ )
{
if (text == "")
{
cout << "There are a few instructions that you need to follow in order to use this system effectively: " << endl ;
cout << "The first being that if you want to use it, you actually have to use a directory that exists. " << endl ;
cout << "The second being that when you want to exit the program you press enter with nothing typed" << endl ;
cout << "The third being NOT TO USE FULL STOPS, THE PROGRAM WILL PUT THEM IN FOR YOU" << endl ;
cout << "Please begin writing: " << endl ;
getline(cin,text) ;
}
if (text!="")
{
text2.append(text) ; //<===HERE IS WHERE I AM HAVING TROUBLE
if ((text.at(text.size() -1 ) != "!" ) && (text.at(text.size() -1 ) != "?") && (text.at(text.size() -1 ) != ":" ))
{
text2.append(". ") ;
getline(cin, text) ;
}
else
{
getline(cin, text) ;
}
if (text == "")
{
cout << "End of session" << endl ; break ;
}
}
}
ofstream writer( filelocation.c_str() ) ;
if(! writer)
{
cout << "Error opening file for output: " << strerror(errno) << endl ;
return -1 ;
}
else
{
writer << text2 << endl ;
writer.close() ;
}
return 0 ;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
int i=0;
字符串文本;
字符串text2;
字符串标题;
字符串用户名;
字符串filelocation=“C:/Users/”;
字符串用户;
字符串标点=“.:!?”
cout“!”
是一个字符串,您要与字符进行比较:“!”
“!”
是一个字符串,您要与字符进行比较:“!”
您不能比较“!”
和“!”
我建议使用rbegin()
来处理最后一个字符:
text2.append(text.begin(), text.end());
switch(*text.rbegin())
{
case '!':
case '?':
case ':': text2.append(". "); break;
}
getline(cin, text);
if(text.empty())
{
cout << "End of session" << endl;
break;
}
text2.append(text.begin(),text.end());
开关(*text.rbegin())
{
案例“!”:
案例“?”:
大小写“:”:text2.append(“.”);break;
}
getline(cin,text);
if(text.empty())
{
不能比较“!”
和“!”
我建议使用rbegin()
来处理最后一个字符:
text2.append(text.begin(), text.end());
switch(*text.rbegin())
{
case '!':
case '?':
case ':': text2.append(". "); break;
}
getline(cin, text);
if(text.empty())
{
cout << "End of session" << endl;
break;
}
text2.append(text.begin(),text.end());
开关(*text.rbegin())
{
案例“!”:
案例“?”:
大小写“:”:text2.append(“.”);break;
}
getline(cin,text);
if(text.empty())
{
库特
这既低效又不符合您的想法。text.at(text.size()-1)
返回最后一个字符。要获取所需内容:
char lastChar = text[text.size() - 1]; // or char lastChar = *text.rbegin();
if (!(lastChar == '.' || lastChar == '?' || lastChar == '!')) // note the single quotes
{
text2.append(". ");
}
getline(cin, text);
这既低效又不符合您的想法。text.at(text.size()-1)
返回最后一个字符。要获取所需内容:
char lastChar = text[text.size() - 1]; // or char lastChar = *text.rbegin();
if (!(lastChar == '.' || lastChar == '?' || lastChar == '!')) // note the single quotes
{
text2.append(". ");
}
getline(cin, text);
您总是需要在读取后检查读取操作是否成功。此外,最好使用text.empty()
来测试std::string
是否为空。std::string
的最后一个元素可以通过text.back()随时使用
。但是,请注意,std::string
的元素属于char
类型,而不是std::string
类型,也就是说,您无法将它们与字符串文本进行比较(您也不想这样做)。例如,字符文字看起来像?”
。您总是需要在读取后检查读取操作是否成功。此外,测试std::string
是否为空最好使用text.empty()
。std::string
的最后一个元素可以使用text.back()随时获得
。但是,请注意,std::string
的元素属于char
类型,而不是std::string
类型,也就是说,您无法将它们与字符串文本进行比较(您也不想这样做).例如,字符文字看起来像?”
。我确实尝试过这个,但在我输入的第一行之后,它一直出错,并且它说程序停止工作。就像变魔术一样,现在没事了!谢谢你的“帮助”:PI确实尝试过这个,但在我输入的第一行之后,它一直出错,它说程序停止工作。就像变魔术一样,现在一切都好了!谢谢你的“帮助”:PYeah我想比较最后一个字符,这样我就可以知道它是否是标点符号,在按回车键后不需要程序输入句号。非常感谢你的回答,但事实证明我一直都是正确的,我的计算机根本没有它!如果你将字符
与字符*
(正如您在发布的代码中所示),那么你就不知道了……嗯,我不知道char*
,但是我已经测试了面前的代码,效果很好。是的,我想比较最后一个字符,这样我就可以知道它是否是标点符号,不需要程序在按回车键后输入句号。非常感谢你的回答,但结果是我如果你把char
与char*
进行比较(正如你在你发布的代码中所做的那样),那么你就没有做对……我不知道char*
,但是我已经在我面前测试了代码,它工作得很好。