c++ - 在字符串的最后一个字符中搜索标点符号

标签 c++ if-statement

我目前正在制作一个小型文字处理器,但遇到了一个问题。我正在尝试制作程序,以便当您按 enter 键时,程序会在您的输入上加上一个句号,然后是一个空格(我稍后会整理段落)。问题是我无法让程序搜索标点符号的最后一个字符,然后根据结果进行分支。编译器给我以下错误:ISO C++ forbids comparison between pointer and integer. 我目前的代码是:

#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 ;
}

提前感谢您的帮助!

最佳答案

"!" 是一个字符串,您要与一个字符进行比较:'!'

关于c++ - 在字符串的最后一个字符中搜索标点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18859082/

相关文章:

jquery - 需要修改 jQuery Accordion 菜单

c++ - 什么是复制省略和返回值优化?

c++ - 是否可以将 X 的容器声明为 X 的成员

c++ - SIMD XOR 运算不如 Integer XOR 有效?

r - "binning"行进入范围 (dplyr/R)

c - 是否有任何帮助网站可以反转 C 中的逻辑/条件语句?

c++ - 在 Netbeans 上编译和运行 OpenGL 代码

C++ 在静态方法中使用枚举

多个 If 条件的 C 缩写形式

mysql - count 和 join 必须包含一个表中不存在于另一个表中的记录