В настоящее время я работаю над созданием небольшого текстового процессора, и я столкнулся с проблемой. Я пытаюсь сделать программу так, что, когда вы нажмете enter, программа полностью остановится на вашем входе, а затем пробел (я буду разбирать абзацы позже). Проблема в том, что я не могу заставить программу искать последний символ для пунктуации, а затем вносить ответ на результат. Компилятор дает мне следующую ошибку: ISO C++ запрещает сравнение между указателем и целым числом. код я в настоящее время является:Поиск последнего символа строки для пунктуации
#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 :: string' пуста, лучше сделать с помощью' text.empty() '. Последний элемент 'std :: string' легко доступен с помощью' text.back() '. Обратите внимание, однако, что элементы 'std :: string' имеют тип' char', а не тип 'std :: string', т. Е. Вы не можете сравнивать их со строковыми литералами (и не хотите) , Например, символьные литералы выглядят как ''? ''. –