The isalpha function is not working at all I keep getting the message: "there is no matching function to call to 'isalpha'(const string&)"
What I want it to do is to recognize the sentence in the data file which one is a capital letter and which one is lower case skipping the spaces so that I can later change them to lower case letters.
the data file reads this:
hello world ALL is great. HELLO WORLD ALL IS GREAT. hellO worlD alL iS great.
#include <iostream>
#include <string>
#include <fstream>
//you must include this library if you wish to do file i/o
#include <vector>
//you must include this library if you wish to use vectors
#include <cctype>
//used for lower, upper and alpha characters
using namespace std;
int SIZE = 26;
//the capacity of the array which is the total amount
//of memory allocated to the array.
class order_record
{
public:
string letters;
};
//Prototypes for your functions
void input(order_record INV[ ], int & count);
void alphatolower(const order_record INV[], int count);
void output(const order_record INV[], int count);
//Function Implementations will go here
void input(order_record INV[ ], int & count)
{
//example using the call by reference mechanism in C++ --
//call record is passed by reference --note & operator
//declare an ifstream
ifstream in;
//ifstream is to read from files
//"in" means input
in.open("mytext.dat");
//this names the file that we want to read from
if (in.fail())
{
cout << "INPUT file did not open correctly" << endl;
cout << "Program cannot execute." << endl;
//pops up if file was not open and
//there for the rest of program cannot execute
}
else
{
while(!in.eof() && count < SIZE)
{
in >> INV[count].letters;
count++;
}
}
in.close();
}
void alphatolower(const order_record INV[], int count)
{
//turning caps into lower case letters
for(long long unsigned int x =0; x < sizeof((INV[count].letters)) ; x++)
{
if(isalpha(INV[count].letters)){
cout << INV[count].letters << endl;
//cout << "testing letters" << endl;
}
}
The error message is correct, there is no function for isalpha(const string&).
isalpha is in <cctype> and can only work on single characters, not strings.
You could write your own wrapper function that takes in a string, but at some point you have to iterate over every char.
1 2 3 4 5 6 7 8 9 10
// not tested
bool isalpha_string(const string& str)
{
for (size_t i = 0; i < str.length(); i++)
{
if (!isalpha(str[i]))
returnfalse;
}
returntrue;
}
I see. Thank you for your response. Would there be another way to code having the string count all the capitals and lowercase letters? I'm a bit lost on what to do. I want the program to read the string including the spaces and turn them all to lowercase letters.
Also...
We haven't covered bool yet. I'm not understanding why you changed void to bool.
I'm not understanding why you changed void to bool.
I didn't change anything. isalpha was not void to begin with. As FurryGuy explained, the C library's isalpha returns a non-zero int to mean "true", and 0 to mean "false". You don't have to use my function if you don't want to, the point is that you need to iterate through each character and check if each character is alpha, is capital, is lowercase, etc.
Would there be another way to code having the string count all the capitals and lowercase letters?
Loop through the string's characters, and see which make islower(character) return non-zero value (i.e. "true"). Use isupper to check if uppercase.
Keep a count of what you want to keep track of, e.g.
class order_record
{
public:
string letters;
};
void alphatolower( const order_record INV[], int count )
{
// turning caps into lower case letters
for ( longlongunsignedint x =0; x < sizeof( (INV[count].letters) ) ; x++ ) {
if ( isalpha( INV[count].letters ) ) {
cout << INV[count].letters << endl;
}
}
}
Issues:
* A class that has only one public member serves no obvious purpose
* Array INV has count element. It does not have element INV[count]. out_of_range error.
* sizeof(std::string) is meaningless here. std::string has size(). INV has count elements
* Nothing in the function converts to lowercase, despite function's name and comment say so
* Redundant parentheses
* Wrong argument type for isalpha()
Did I miss some?
Overall, I'd:
FOR EACH c in input
IF c is text
IF c is upper
c = tolower(c)
++uppercount
ELSE
++lowercount
print c
Thank you for all of your responses. I'm going to take a second look and really try to work on this program properly. I really appreciate all the constructive criticism. It helps a lot. And thank you for the websites as well. I don't get a lot of examples from my class so it is really nice to have so many examples that show how to code properly.
No need to know whether each letter is upper or lower case, just loop through the string(s) and use <cctype>'s tolower() function on each string element. Assigning the result back into the element. http://www.cplusplus.com/reference/cctype/tolower/
If the letter is already lower case no conversion occurs.
I recommend using a temporary string constructed from the original, changing the case is one way.
#include <iostream>
#include <string>
#include <cctype>
struct count
{
unsigned upper { }; // default zero at construction
unsigned lower { };
};
void GetCount(const std::string&, count&);
std::string ToUpper(const std::string&);
std::string ToLower(const std::string&);
int main()
{
std::string str { "hello world ALL is great. HELLO WORLD ALL IS GREAT. hellO worlD alL iS great." };
count cnt;
GetCount(str, cnt);
std::cout << "There are " << cnt.upper << " upper case letters and " << cnt.lower << " lower case letters.\n\n";
std::string strupper { ToUpper(str) };
std::string strlower { ToLower(str) };
std::cout << str << "\n\n" << strupper << "\n\n" << strlower << '\n';
}
void GetCount(const std::string& str, count& cnt)
{
for (size_t itr { }; itr < str.size(); itr++)
{
if (isalpha(str.at(itr)))
{
isupper(str.at(itr)) ? cnt.upper++ : cnt.lower++;
}
}
}
std::string ToUpper(const std::string& str)
{
std::string temp { str };
for (std::string::iterator itr = temp.begin(); itr != temp.end(); itr++)
{
*itr = toupper(*itr);
}
return temp;
}
std::string ToLower(const std::string& str)
{
std::string temp { str };
for (auto& itr : temp)
{
itr = tolower(itr);
}
return temp;
}
There are 27 upper case letters and 33 lower case letters.
hello world ALL is great. HELLO WORLD ALL IS GREAT. hellO worlD alL iS great.
HELLO WORLD ALL IS GREAT. HELLO WORLD ALL IS GREAT. HELLO WORLD ALL IS GREAT.
hello world all is great. hello world all is great. hello world all is great.