My program won't seem to run with the isalpha function inside the boolean function, wondering if anybody could help.
//code
#include<iostream>
#include<sstream>//so that my stringstream variable works
#include<vector>
using namespace std;
bool punctuationcheck(vector <string> punctuationlist, string oneword)
{
for(int i = 0; i < punctuationlist.size();i++)
{
if(isalpha(punctuationlist[i]) == false && punctuationlist[i] != ' ')
return false;
}
return true;
}
int main()
{
//input Prototype for sentence and finding word in sentence
string inputsentence;//declares a variable to store the sentence inputed by the user
string findword;//declares a variable to store the word the user wants to find in the sentence
cout << "Please enter a sentence without punctuation " << endl;//asks the user to input a sentence
getline(cin, inputsentence);//stores the input in the variable inputsentence and takes the whole line
cout << "Please enter a word from your previous sentence to find " << endl;//asks the user the input a word to find in the sentence previously inputted
cin >> findword;//stores the input in the variable findword
//tolower Prototype to remove case sensitivity
cout << "Your sentence is ";//output the sentence they inputted
for(int i = 0; i < inputsentence.size();i++)//a for loop that goes through each character in the sentence
{
inputsentence[i] = tolower(inputsentence[i]);//turns each individual character in sentence to lower case to make it eaiser
cout << inputsentence[i];//outputs the result
}
cout << " ";//space so that doesn't join with next output
cout << "Your word to find is ";//output the word to find they inputted
for(int i = 0; i < findword.size();i++)//a for loop that goes through each character in the word to find in the sentence
{
findword[i] = tolower(findword[i]);//turns each individual character in word to find to lower case to make it eaiser
cout << findword[i];//outputs the result
}
//Stringstream Prototype to count words
stringstream feedoneword;//declares a variable stringstream to got throught the sentence and find position in sentence
vector <string> punctuationlist;
feedoneword << inputsentence;//puts original sentence into stringstream
string oneword;//declares a temporary variable that holds each word in the sentence to be checked
bool wordfound = false;//declares a boolean and sets a value of false to it
int wordposition = 0;//declares an integer variable to be used to find the position of the word in the sentence
while(feedoneword >> oneword)//while loop, which is only carried out when words are still being put into the stringstream
{
wordposition++;//increases the value of wordposition by one each time the while loop is carried out
if(oneword == findword)//if statement, is carried out when word to find is the same as the word in the sentence
{
cout << endl << "Word found in position " << wordposition << endl;//outputs to user that the word has been found and gives position where it was found in the sentence aswell
wordfound = true;//sets the value of wordfound to true so that next if statement is not carried out
}
}
if(wordfound == false)//if statement, carried out when wordfound is still equal to false
{
cout << endl << "Word entered to find has no position in inputed sentence" << endl;//outputs to user that the word to find was not found in any position in the sentence
}
if(punctuationcheck(punctuationlist,oneword) == false)
{
punctuationlist.push_back(oneword);
cout << "The punctuation found was: "
}
for(int i = 0; i < punctuationlist.size(); i++)
{
cout << punctuationlist[i] << " ";
}
return 0;//returns all values to zero
}
punctuationlist is a vector of strings.
punctuationlist[i] is a string
isalpha function operates on char's and a value different from zero (i.e., true) if indeed c is an alphabetic letter. Zero (i.e., false) otherwise.
is dangerous. Not just because of the type mismatch, but because the function isalpha returns an int, not a bool (because of its origins in the C library).
if (isalpha('A')) // test for true
if (!isalpha('A')) // test for false
(technically testing for false might be safe, but it is a dangerous approach since it leads to the incorrect assumption that the code to check for true would use the same syntax).