Remove Vowels from more than first word - c++11

Program compiles, runs, and works.
Big issue:
It only works for the first word of a sentence.
EXAMPLE:
"Welcome to the jungle" results in "wlcm" rather than "wlcm t th jngl".
Small Issue:
There's a "1" appearing between input and output when it runs. How can I get rid of that? I think it's from this, but I'm not positive:
{
withVowel.erase(i, 1);
length = int(withVowel.length());
}
WHOLE CODE:
#include <iostream>
#include <string>
using namespace std;
void removeVowel(string&); // Removes vowels from input string.
string withVowel; // Will be used to read user input.
int main ()
{
const string SENTINEL = "0"; // Sentinel value.
// Request input string unless SENTINEL is entered.
cout << "Enter a word or series of words." << '\n';
cout << "Or, enter " << SENTINEL << " to quit." << '\n' << endl;
cin >> withVowel;
// In case of SENTINEL:
while (withVowel == SENTINEL)
{
cout << '\n' << "***" << endl;
return 0;
}
// Run loop.
removeVowel(withVowel);
// Display the string without vowels.
cout << "The word(s) entered reflecting only consonants: " << withVowel << endl;
return 0;
}
void removeVowel(string& withVowel)
{
int i = 0;
int length = int(withVowel.length());
while (i < length)
{
if (withVowel.at(i) == 'a' ||
withVowel.at(i) == 'A' ||
withVowel.at(i) == 'e' ||
withVowel.at(i) == 'E' ||
withVowel.at(i) == 'i' ||
withVowel.at(i) == 'I' ||
withVowel.at(i) == 'o' ||
withVowel.at(i) == 'O' ||
withVowel.at(i) == 'u' ||
withVowel.at(i) == 'U')
{
withVowel.erase(i, 1);
length = int(withVowel.length());
}
else i++;
}
// Display the string without vowels.
cout << removeVowel << endl;
}

Use getline(cin, withVowel); instead of cin >> withVowel;
Also replace while with if in main().
And don't forget to upvote and accept answers=)

The problem with only getting the first word is being you're using cin >> withVowel;, which will stop reading input as soon as it encounters white space. Try using std::getine(cin, withVowel); instead.
If at all possible, I would avoid manipulating the string in place, and just copy things to the output if they're not vowels.
std::remove_copy_if(withVowel.begin(), withVowel.end(),
[](char c) { return c == 'a' || c == 'A' ||
c == 'e' || c == 'E' ||
c == 'i' ...;});

Related

Memory leak involving an unloaded shared object?

I am trying to understand this programming problem whereby I am supposed to guess the data structures. I am having a slight issue with my program.
PROBLEM: I have no clue as to why my program always gets killed with signal 11 (segmentation fault) but it works and compiles fine.
About the program: n would be number of integer sets ; it takes in an integer p (command) and integer data and pushes/pops into the following data structures. I've use a bool as flag to check the statuses. Am I right to believe the structures would be destroyed after the while loop as it goes out of scope?
int main(){
int n;
while (cin >> n && n != 0){
stack<int> mystack;
queue<int> myqueue;
priority_queue<int> maxq;
bool isstack = true;
bool isqueue = true;
bool ispq = true;
for (int i = 0; i < n; i++){
int p, data;
cin >> p >> data;
if (p == 1){
if (isqueue) myqueue.push(data);
if (isstack) mystack.push(data);
if (ispq) maxq.push(data);
} else if (p == 2){
if ((mystack.empty() || mystack.top() != data) && isstack) isstack = false;
else mystack.pop();
if ((myqueue.empty() || myqueue.front() != data) && isqueue) isqueue = false;
else myqueue.pop();
if ((maxq.empty() || maxq.top() != data) && ispq) ispq = false;
else maxq.pop();
}
}
if (isstack && !(isqueue || ispq)) cout << "stack" << endl;
else if (isqueue && !(isstack || ispq)) cout << "queue" << endl;
else if (isstack && (ispq || isqueue) || (isqueue && ispq)) cout << "not sure" << endl;
else if (ispq && !(isstack || isqueue)) cout << "priority queue" << endl;
else cout << "impossible" << endl;
}
return 0;
}
Yes, the data structures you’ve used are local scope to while, so they get destructed at the end of while loop.
Unless you provide for what input you’re seeing segmentation fault, it’s hard to say. Or run it in debug mode, your program should break at line where seg fault occurs.
I tried to run this code.
This code threw an exception when you pop after 'isstack' was changed to false.
The exception was 'Expression: deque empty before pop'.
After the variable changed false, always condition statements are return false.
So you tried to pop at empty stack.
That is a just logical bug.

Parsing through Vectors

I am new and learning C++ using the Programming Principles ... book by Bjarne Stroustrup. I am working on one problem and can't figure out how to make my code work. I know the issue is with if (words[i]==bad[0, bad.size() - 1]) in particular bad.size() - 1])
I am trying to out put all words in the words vector except display a bleep instead of any words from the words vector that match any of the words in the bad vector. So I need to know if words[i] matches any of the values in the bad vector.
#include "../std_lib_facilities.h"
using namespace std;
int main()
{
vector<string> words; //declare Vector
vector<string> bad = {"idiot", "stupid"};
//Read words into Vector
for(string temp; cin >> temp;)
words.push_back(temp);
cout << "Number of words currently entered "
<< words.size() << '\n';
//sort the words
sort(words);
//read out words
for(int i = 0; i < words.size(); ++i)
if (i==0 || words[i-1]!= words[i])
if (words[i]==bad[0, bad.size() - 1])
cout << "Bleep!\n";
else
cout << words[i] << '\n';
return 0;
}
You need to go through all of the entries in the bad vector for each entry in the words vector. Something like this:
for(const string& word : words)
{
bool foundBadWord = false;
for(const string& badWord : bad)
{
if(0 == word.compare(badWord))
{
foundBadWord = true;
break;
}
}
if(foundBadWord)
{
cout << "Bleep!\n";
}
else
{
cout << word << "\n";
}
}

no conversion from 'const char *' to 'int'

Can someone help me with this issue I'm having? I'm getting the 'no conversion from 'const char *' to 'int' on my first 'if' statement line. Can someone tell me what i'm doing wrong?
char Vtype,
Vehicle;
int HI,
HO,
MI,
MO,
Ctime,
Catime,
Btime,
Basub,
Ttime,
Tasub;
double Ctotal,
Batot,
Bbtot,
Btotal,
Tatot,
Tbtot,
Ttotal;
const double C_Rate = 1.25,
B1_Rate = 2.00,
B2_Rate = 2.50,
T1_Rate = 3.75,
T2_Rate = 4.50;
cout << "TYPE OF VEHICLE: ";
cin >> Vtype;
if (Vtype == "C" || Vtyp == "B" || Vtype == "T")
{
cout << "HOURS IN: ";
cin >> HI;
if (HI < 0 || HI > 23)
{
cout << "Hours cannot be less than 0 or greater than 23!\n";
cout << "Please enter a valid hour.\n";
cin >> HI;
}
Thank you,
T
If Vtype is a char, compare it to characters:
if (Vtype == 'C' || Vtype == 'B' || Vtype == 'T') {
}
The compiler is accusing you of comparing const char * to int because as far as C++ is concerned, a character is an integer.
Or, if you need to compare it to strings, declare it as std::string.

Sentinel not working?

I'm not sure why this isn't working, but this is my error message:
Error] no match for 'operator!=' (operand types are 'std::string {aka std::basic_string}' and 'const int')
EDIT: The above issue has been resolved. But, the current issues are redundant ***, and the lack of vowel removal in a sentence rather than just the first word of a sentence.
#include <iostream>
#include <string>
using namespace std;
void removeVowel(string&); // Removes vowels from input string.
string withVowel; // Will be used to read user input.
int main ()
{
const string SENTINEL = "0"; // Sentinel value.
// Request input string unless SENTINEL is entered.
cout << "Enter a word or series of words. " << '\n';
cout << "Or, enter " << SENTINEL << " to quit. " << endl;
cin >> withVowel;
// In case of SENTINEL:
while (withVowel == SENTINEL)
{
cout << "***" << endl;
}
// Run loop.
removeVowel(withVowel);
// Display the string without vowels.
cout << "The word(s) entered reflecting only consonants: " << withVowel << endl;
return 0;
}
void removeVowel(string& withVowel)
{
int i = 0;
int length = int(withVowel.length());
while (i < length)
{
if (withVowel.at(i) == 'a' ||
withVowel.at(i) == 'A' ||
withVowel.at(i) == 'e' ||
withVowel.at(i) == 'E' ||
withVowel.at(i) == 'i' ||
withVowel.at(i) == 'I' ||
withVowel.at(i) == 'o' ||
withVowel.at(i) == 'O' ||
withVowel.at(i) == 'u' ||
withVowel.at(i) == 'U')
{
withVowel.erase(i, 1);
length = int(withVowel.length());
}
else i++;
}
// Display the string without vowels.
cout << removeVowel << endl;
}
Based on your other question, and the error message, I assume withVowel is a std::string. The error message is pretty much telling you what the problem is: you can't compare a std::string with an int.
Since you only need SENTINEL for printing and comparison, just declare it as a std::string as well:
const std::string SENTINEL = "0";
You can't compare const int with strings.
Use ctrl+z and enter to stop the input.
string word;
cout << "ctrl+z and Enter to exit\n";
while (cin >> word){
cout << word << ' ';
// other processing
}
And for your case:
cout << "Enter a word or series of words.\n";
cout << "Ctrl+z and Enter to exit\n";
while (cin >> withVowel){
removeVowel(withVowel);
cout << "The word(s) entered reflecting only consonants :" << withVowel << endl;
}

Bool must be always returning NULL(if statement ignoring true or false)

I need to know why I am always returning NULL in the following context(for example no matter if I say "Yes", or "No", for any of the questions I am getting null( I assume because the question process repeats when it runs) even though I say no in main. I'm trying to get it to do this: for all invalid answers I return null, if null start over the question process, if answer was valid I return either true or false, if true continue, if false quit program.
bool question1()
{
string answer2;
cout << "Are you 18 or older and have a valid Driver's License? Yes or No: ";
getline( cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
cout << endl;
if( answer2 == "yes")
{
cout << "Alright! " << endl << "You are set for registration. Please fill out the registration form. ";
return true;
}
else if( answer2 == "no")
{
cout << "Do you know someone else who is 18 or older that can register? Yes or No ";
getline( cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
if( answer2 == "yes")
{
cout << "Good, then please continue the process in their place. Please fill out the registration form";
return true;
}
else if( answer2 == "no")
{
cout << "Please come back later when you have the appropriate personel";
return false;
}
else
{
cout << "The answer given was invalid. Please give a valid answer. " << endl << endl ;
return NULL;
}
}
else
{
cout << "The answer given was invalid. Please give a valid answer. " << endl << endl;
return NULL;
}
void registerPerson( array< string, nameSize > namesOfPeople, array< string, idSize > idlen)
{
string pName;
string dLicense;
static int i = 0;
static int b = 0;
static int c = 0;
unsigned int x = 1;
cout << endl << endl << "REGISTRATION FORM:" << endl << endl << "------------------" << endl;
cout << "Please" << endl << "enter the following: \n \n";
cout << "Name: ";
getline( cin, pName );
for ( int j = i; j<=800; ++ j )
{
namesOfPeople[j] = pName;
cout << namesOfPeople[j];
i = i + 1;
break;
}
cout << endl;
while( x = 1)
{
cout << "Driver\'s Licence Number( Must be 9 characters long, no dashesh ): ";
cin >> dLicense;
if ( dLicense.length() < 9 || dLicense.length()> 9 )
{
cout << "The entered number was invalid. Please try again";
}
else
{
for ( int a = i; c<=800; ++ a )
{
idlen[a] = dLicense;
cout << idlen[a];
c = c + 1;
break;
}
}
}
}
}
{
int main()
array< string, nameSize > names = {};
array< string, idSize > ids = {};
carShare mycarShare1;
carShare mycarShare2;
mycarShare2.welcomeMessage();
mycarShare2.question1();
if( mycarShare1.question1() == NULL)
{
mycarShare1.question1();
}
else if( mycarShare1.question1() == true)
{
mycarShare1.registerPerson(names, ids);
}
else
{
return 0;
}
system( "PAUSE" );
return 0;
}
I'd probably change it to handle invalid input within question1() itself. An easy way to start the question over again is letting question1 call itself again on invalid input until it gets a valid one like here (I put comments at all the changed places.):
bool question1()
{
string answer2;
cout << "Are you 18 or older and have a valid Driver's License? Yes or No: ";
getline( cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
cout << endl;
if( answer2 == "yes")
{
cout << "Alright! " << endl << "You are set for registration. Please fill out the registration form. ";
return true;
}
else if( answer2 == "no")
{
cout << "Do you know someone else who is 18 or older that can register? Yes or No ";
getline( cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
if( answer2 == "yes")
{
cout << "Good, then please continue the process in their place. Please fill out the registration form";
return true;
}
else if( answer2 == "no")
{
cout << "Please come back later when you have the appropriate personel";
return false;
}
else
{
cout << "The answer given was invalid. Please give a valid answer. " << endl << endl ;
return question1(); // call itself again on invalid input.
}
}
else
{
cout << "The answer given was invalid. Please give a valid answer. " << endl << endl;
return question1(); // call itself again on invalid input.
}
}
int main()
{
array< string, nameSize > names = {};
array< string, idSize > ids = {};
carShare mycarShare1;
carShare mycarShare2;
mycarShare2.welcomeMessage();
bool answer = mycarShare2.question1();
/* if( answer == NULL) we don't need this anymore if we handle errors inside question1()
{
mycarShare1.question1();
}
*/
if( answer == true) // notice the change to if instead of else if here.
{
mycarShare1.registerPerson(names, ids);
}
else
{
return 0;
}
system( "PAUSE" );
return 0;
}
Another way to do the same without recursion would be a do... while loop, here's a version of only question1() with a loop:
bool question1()
{
string answer2; // has to be declared before the loop.
do
{
cout << "Are you 18 or older and have a valid Driver's License? Yes or No: ";
getline( cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
cout << endl;
if( answer2 == "yes")
{
cout << "Alright! " << endl << "You are set for registration. Please fill out the registration form. ";
return true;
}
else if( answer2 == "no")
{
cout << "Do you know someone else who is 18 or older that can register? Yes or No ";
getline( cin, answer2);
transform(answer2.begin(), answer2.end(), answer2.begin(), ::tolower);
if( answer2 == "yes")
{
cout << "Good, then please continue the process in their place. Please fill out the registration form";
return true;
}
else if( answer2 == "no")
{
cout << "Please come back later when you have the appropriate personel";
return false;
}
}
}while(answer2 != "yes" && answer2 != "no"); // loop as long as the input is invalid.
}
=====================================
Edit:
As clarification to it printing multiple times, here's the culprit in your original code:
mycarShare2.question1();
if( mycarShare1.question1() == NULL)
{
mycarShare1.question1();
}
else if( mycarShare1.question1() == true)
{
mycarShare1.registerPerson(names, ids);
}
else
{
return 0;
}
mycarShare2.question1() is a function call, you were calling your function 3 times in that part. What you'd want was probably saving it in a bool variable and only test in the if/else statements after that, like this:
bool answer = mycarShare2.question1();
if( answer == NULL)
{
mycarShare1.question1();
}
else if( answer == true)
{
mycarShare1.registerPerson(names, ids);
}
else
{
return 0;
}
Note: this is just to show it the way you probably expected it to be executed in your original code and not a solution. As said bool can only be true or false and the check for NULL isn't needed at all. (See my solutions above.)

Resources