isalpha function inside boolean function help

Hi,

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
}
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/cctype/isalpha/

You also have some serious type problems.

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.
Last edited on
Testing for true or false like this

 
    if (isalpha(punctuationlist[i]) == false)

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).

Consider this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
	cout << isalpha('A') << '\n';
	cout << ispunct(',') << '\n';
	cout << isdigit('1') << '\n';
	cout << isspace(' ') << '\n';

	if (isalpha('A') == true) cout << "alpha\n";
	if (ispunct(',') == true) cout << "punct\n";
	if (isdigit('1') == true) cout << "digit\n";
	if (isspace(' ') == true) cout << "space\n";
}

g++ output:
1
16
1
8
alpha
digit

C++Builder XE output:
256
16
4
8

visual studio c++ output:
1
16
4
8
alpha


The solution?
An appropriate syntax would be
1
2
    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).
Last edited on
Topic archived. No new replies allowed.