I am writing to display the number of a's in a user input. I am getting an out of range at memory location. I am not sure what I am doing wrong with my code, any suggestion would be appreciated.
#include <iostream>
#include <string>
usingnamespace std;
//Creating constants
int countA(string, char);
string userInput;
constchar character = 'a';
int main()
{
//Explain program to user
cout << "This program will take your input, and calculate the number of a's entered" << endl;
//Prompt user to enter string
cout << "Please enter a short sentence: ";
cin >> userInput;
//Call the recursive part
cout << "The count of a's in the sentence is: " << countA(userInput, character) << endl;
system("PAUSE");
return 0;
}
int countA(string str, char character)
{
//Initialize the count
int count = 0;
//Read the input into an array
if (userInput [0] == character)
{
//Returning count
return count;
}
else
{
//Returning count
count++;
return count + countA(str.substr(1), character);
}
}
Okay I appreciate your input, I have made some changes to the global variables. But I am not sure how to get it to check for the end of the string. Here are my changes thus far.
#include <iostream>
#include <string>
usingnamespace std;
//Creating constants
int countA(string, char);
int main()
{
string userInput;
constchar character = 'a';
//Explain program to user
cout << "This program will take your input, and calculate the number of a's entered" << endl;
//Prompt user to enter string
cout << "Please enter a short sentence: ";
cin >> userInput;
//Call the recursive part
cout << "The count of a's in the sentence is: " << countA(userInput, character) << endl;
system("PAUSE");
return 0;
}
int countA(string str, char character)
{
string userInput;
//Initialize the count
int count = 0;
//Read the input into an array
if (userInput [0] == character)
{
//Returning count
return count;
}
else
{
//Returning count
count++;
return count + countA(str.substr(1), character);
}
}
That is a good point, thanks for your input. I have removed the information that you have indicated. I am trying to wrap my head around what data needs to be in my if statement there.
#include <iostream>
#include <string>
usingnamespace std;
//Creating constants
int countA(string, char);
int main()
{
string userInput;
constchar character = 'a';
//Explain program to user
cout << "This program will take your input, and calculate the number of a's entered" << endl;
//Prompt user to enter string
cout << "Please enter a short sentence: ";
cin >> userInput;
//Call the recursive part
cout << "The count of a's in the sentence is: " << countA(userInput, character) << endl;
system("PAUSE");
return 0;
}
int countA(string str, char character)
{
//Initialize the count
int count = 0;
//Read the input into an array
if (str == "")
{
//Returning count
return count;
}
else
{
//Returning count
count++;
return count + countA(str.substr(1), character);
}
}
I don't understand what you are saying, I am new to c++ so all of that doesn't make sense to me without an explanation. Whether or not this is "extremely inefficient" it's how I have been asked to write it. I need to write a program that returns the number of "a's" in a phrase entered by a user. I appreciate the help keskiverto, but is there anyone else that is willing to help me out with this one?
I have changed this thing around a little and am getting correct results but only for the first word, it will not do it for the entire string. Whats a good way to do that?
#include <iostream>
#include <string>
usingnamespace std;
string:: size_type vowelsNumbersof(const string &string, char character)
{
constchar vowels[] = "aeiou";
string::size_type count = 0;
if (std::strchr(vowels, character) != 0)
{
for (char character2 : string)
{
if (character2 == character) count++;
}
}
return count;
}
int main()
{
string str;
//Explain program to user
cout << "This program will take your input, and calculate the number of a's entered" << endl;
//Prompt user to enter string
cout << "Please enter a short sentence: ";
cin >> str;
//Call the recursive part
cout << "The number of a's:" << vowelsNumbersof(str, 'a') << endl; //Function to count a's
cout << "The number of e's:" << vowelsNumbersof(str, 'e') << endl; //Function to count e's
cout << "The number of i's:" << vowelsNumbersof(str, 'i') << endl; //Function to count i's
cout << "The number of o's:" << vowelsNumbersof(str, 'o') << endl; //Function to count o's
cout << "The number of u's:" << vowelsNumbersof(str, 'u') << endl; //Function to count u's
system("PAUSE");
return 0;
}