import<string>
import<iostream>
bool isNumber(char ch) {
//this function will determine if a character is a number.
//this allows the function to use an ASCLL value
if (int i = ch >= '0' && ch <= '9') {
//If character found is anything from 0-9 operation will return the condition true.
return true;
}//ends the statement
else {
//else return the condition false
return false;
}//ends the statement
}//ends the function
bool isUpperCase(char ch) {
//this function will determine if a character is upper case.
//this allows the function to use an ASCLL value
if (int i = ch >= 'A' && ch <= 'Z') {
//If character found is anything from A-Z operation will return the condition true.
return true;
}//ends the statement
else {
return false;
//else return the condition false
}//ends the statement
}//ends the function
bool isLowerCase(char ch) {
//this function will determine if a character is lower case.
//this allows the function to use an ASCLL value
if (int i = ch >= 'a' && ch <= 'z') {
//If character found is anything from a-z operation will return the condition true.
return true;
}//ends the statement
else {
return false;
//else return the condition false
}//ends the statement
}//ends the function
void processData(char text[], int size) {
int upperCase = 0;
int lowerCase = 0;
int numbers = 0;
int i;
text[size];
for (i = 0; i <= text[size]; i ) {
if (isNumber(text[i]) == true) {
numbers ;
}
else if (isUpperCase(text[i]) == true) {
upperCase ;
}
else if (isUpperCase(text[i]) == true) {
lowerCase ;
}
}
std::cout << "There are " << upperCase << " uppercase letters in this string." << std::endl;
std::cout << "There are " << lowerCase << " lowercase letters in this string." <<std::endl;
std::cout << "There are " << numbers << " numbers in this string." << std::endl;
}
int main()
{
std::cout << "Enter some text: ";
std::string s;
std::getline(std::cin, s);
int size = sizeof(s);
char text[sizeof(s)];
processData(text, size);
return 0;
}
I am trying to create a program that will count the number of numbers, lowercase letters and uppercase letters. But my code keeps returning all the values at 0. I know my boolean functions are correct because they worked when I counted the numbers in lowercase letters and uppercase letters in a file. Can some please give some insight on what I am doing wrong?
CodePudding user response:
I saw two bugs right away, but I don't know if there are more because I have not tried to run your code.
- Since
s
is astring
instead of an array, it's invalid to usesizeof
on it. (Try printing outsizeof(s)
if you don't believe me.) Uses.length()
to get the length of the string instead. - You never actually populated your
text
array with any data, so it is uninitialized when you pass it toprocessData
. Instead of making thetext
array, try this:
But to avoid warnings, you probably need to changeprocessData(s.c_ptr(), s.length());
processData
so its first argument is const, like this:const char * text
.
CodePudding user response:
#include <iostream>
#include <string>
using namespace std;
//Not to type std:: over and over again.
bool isNumber(char ch) {
//this function will determine if a character is a number.
if (int i = ch >= '0' && ch <= '9') {
//If character found is anything from 0-9 operation will return the condition true.
return true;
}//ends the statement
else {
//else return the condition false
return false;
}//ends the statement
}//ends the function
bool isUpperCase(char ch) {
//this function will determine if a character is upper case.
if (int i = ch >= 'A' && ch <= 'Z') {
//If character found is anything from A-Z operation will return the condition true.
return true;
}//ends the statement
else {
return false;
//else return the condition false
}//ends the statement
}//ends the function
bool isLowerCase(char ch) {
//this function will determine if a character is lower case.
if (int i = ch >= 'a' && ch <= 'z') {
//If character found is anything from a-z operation will return the condition true.
return true;
}//ends the statement
else {
return false;
//else return the condition false
}//ends the statement
}//ends the function
void processData(char text[], int size) {
int specialCharacter = 0;
int upperCase = 0;
int lowerCase = 0;
int numbers = 0;
int i;
for (i = 0; i < size; i ) {
if (isNumber(text[i]) == true) {
numbers ;
}
else if (isUpperCase(text[i]) == true) {
upperCase ;
}
else if (isLowerCase(text[i]) == true) {
lowerCase ;
}
}
cout << "There are " << upperCase << " uppercase letters in this string." << endl;
cout << "There are " << lowerCase << " lowercase letters in this string." <<endl;
cout << "There are " << numbers << " numbers in this string." << endl;
}//ends the prosses Data function
int main()
{
cout << "Enter some text: ";
//Outputs enter some text onto the console.
string str;
//initiates the string variable name to str
getline(cin, str);
//gets the whole line of input and stores it in the string variable
int size = sizeof(str);
//sets the integer size to be the size of the string
char text[sizeof(str)];
// sets the character text array to set the number of characters equal to the size of the string
str.copy(text, size);
//converts the string into a character array
processData(text, size);
//calls the processData function
return 0;
//returns the main function at 0.
}//ends the main function
I found the problem I forgot to convert the string input to an array of characters. str.copy(text, size);
was all that was needed to fix the code.