Program doesn't continue loop [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
When my program gets to the while(!looking) loop for the first time, it performs the task, but afterwards it doesn't continue on with taking the words and translating them. Need some help on figuring out why it doesn't run through.
while (cin.good()){
getline(cin, lines);
while (!looking) {
spot = lines.find(" ");
if (spot == -1){
looking = true;
spot = lines.length( );
}
line = lines.substr(0, spot);
TP1stLetter(line);
if (!looking)
lines = lines.substr(spot + 1, lines.length( ) - spot + 1);
}
cout << endl;
//while( cin.good() ) {
//getline (cin, line);
//for(x = 0; x < line.size(); x++) {
//char letter = line[x];
//if (letter == 'a' || letter == 'e' || letter == 'i'
// || letter == 'o' || letter == 'u'){
//cout << letter;
//}
//}
}
}

Just add one line of code after the cout statement as follow:
if (mode == TOPIG) {
cout << "TOPIG MODE" << endl;
while (cin.good()){
getline(cin, lines);
while (!looking) {
spot = lines.find(" ");
if (spot == -1){
looking = true;
spot = lines.length( );
}
line = lines.substr(0, spot);
TP1stLetter(line);
if (!looking)
lines = lines.substr(spot + 1, lines.length( ) - spot + 1);
}
cout << endl;
looking = false;
//while( cin.good() ) {
//getline (cin, line);
//for(x = 0; x < line.size(); x++) {
//char letter = line[x];
//if (letter == 'a' || letter == 'e' || letter == 'i'
// || letter == 'o' || letter == 'u'){
//cout << letter;
//}
//}
}
}

2 things I can think of, hope it helps
- Should you be setting looking = false at any point in your while(!looking) loop?
- Are you adding quote around your argument string in your command line
like this
YourExe.Exe these words are five arguments
But instead you need quotes to make a string
YourExe.Exe "this is one argument"

Related

Counting syllables in a word C++

I have created a program to count the syllables in a word inputted by the user. The user is to enter any amount of words followed by the enter key until the (#) key is entered, then the program is to display the words in a table followed by the syllable count for each word.
I am having issues with the "silent e" portion of my program.
if (word_length - 1 == 'e')
{
vowels = vowels - 1;
It seems as though it is not able to pick up the last letter in the string of words. I have tried to move some of the if statements around to see if that helps and to better identify where the problem lies, and from what I have noticed, as stated earlier i believe it to be with the silent e portion of the code.
It is difficult to find even the smallest errors in my code, so I am asking for another set of eyes to gaze over my code.
Any help will be greatly appreciated.
Also, I have yet to complete the formatting of my results table, so please over look that.
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
string word_1 = "create list";
int vowels = 0;
int word_length; // better to set .length() into variable to eliminate warnings
int words_to_print = 0; // this will count how many words to print to use in for loop later
/*
vector <variable type> name_of_vector[size of vector];
creating vectors
leaving them empty for now
*/
vector <string> words_saved;
vector <int> number_of_syllables_saved;
cout << "Enter 4 words from the English dictionary, to determine the amount of syllables each word has." << endl;
cout << "Please enter [#] when finished, to create a list." << endl;
cin >> word_1;
while (word_1 != "#") // as long as user doesnt enter # you can enter a word and
{ // have it run thru the syllable logic
word_length = word_1.length();
words_to_print++;
words_saved.push_back(word_1);
// ^ this saves the word into the next availabe index of vector for strings.
for (int i = 0; i < word_length ; i++) // length is a variable now instead of function syntax this
{ // eliminates the <: signed/usnsigned mismatch warning below
if ((word_1[i] == 'a') || (word_1[i] == 'e') || (word_1[i] == 'i') || (word_1[i] == 'o') || (word_1[i] == 'u') || (word_1[i] == 'y'))
{
vowels = vowels + 1;
if ((word_1[i + 1] == 'a') || (word_1[i + 1] == 'e') || (word_1[i + 1] == 'i') || (word_1[i + 1] == 'o') || (word_1[i + 1] == 'u') || (word_1[i + 1] == 'y'))
{
vowels = vowels - 1;
if (word_length - 1 == 'e')
{
vowels = vowels - 1;
if (vowels == 0)
{
vowels = vowels + 1;
}
}
}
}
}
number_of_syllables_saved.push_back(vowels);
//^ this puts number of syllables into vector of ints
vowels = 0; // this resets the amounts so it can count vowels of next word and not stack from previous word
cin >> word_1; // this will reset the word and controls loop to print out chart if # is entered
}
// use a for loop to print out all the words
cout << endl << endl << endl;
cout << "Word: " << setw(30) << "Syllables: " << endl;
for (int x = 0; x < words_to_print; x++)
{
cout << words_saved[x] << setw(20) << number_of_syllables_saved[x] << endl;
}
//system("pause");
return 0;
}
You are comparing the character 'e' to the integer word_length - 1 instead of comparing it to the last character of your string as you intended.
You should replace if (word_length - 1 == 'e') with if (word_1[word_length - 1] == 'e').
Your nested if-statements don't get evaluated at the anticipated index.
Say the input is "state".
At i = 0, word_1[i] = 's', doesn't pass the first if statement.
At i = 1, word_1[i] = 't', doesn't pass the first if statement.
At i = 2, word_1[i] = 'a', vowels becomes 1. The program does not proceed further down the nested if statements since word_1[i+1] is 't'.
At i = 3, word_1[i] = 't', doesn't pass the first if statement.
At i = 4, word_1[i] = 'e', vowels becomes 2. The program does not proceed further down the nested if statements since word_1[i+1] is garbage value.
As you can see, it never reaches the nested if-statements as you intended

Testing word is in language L = {a^n-1b^n} using stack

I am writing a C++ function that implements a stack to test if a word is in language L = {a^n-1b^n}. Words that exist include b, abb, aabbb.
I have seen many postings with regards to testing a word being in a language where L = {a^nb^n} and I am just wondering how to go about this when the n's are not equal.
The code that I have written works but I feel that I am taking the easy way out with regards to making sure the word is not in a format like aba bababa abababa etc. I have implemented this test outside of the stack but feel there should be a way to do it in the stack.
The reason I say this is because I later have to take the function bool isInLanguageL(string w) and use the following header instead bool isInLanguageL(queueType &w). I have been having trouble trying to convert the code I previously had with string w to queue w.
//function prototype
bool isInLanguageL(string w);
//function to see if the word exists in the language
bool isInLanguageL(string w) {
//creating counters for the a's and b's
int countPush = 0;
int countPop = 0;
reverse(w.begin(), w.end()); //putting the b's infront
//creating the charachter stack
stack<char> charStack;
//checking the word for an illegal format such as aba or bab
for (int i = 0; i < w.length(); i++) {
if ((w[i] == 'a' && w[i + 1] == 'b' && w[i+2] == 'a') || (w[i] == 'b' && w[i + 1] == 'a' && w[i + 2] == 'b')) {
cout << "This is an illegal format of a word in language L and therefore: ";
return false;
}
else { //if the word is a legal format then continue onto stack creation
//adding the b's to the stack and counting them simultaneously
if (w[i] == 'b') {
charStack.push(w[i]);
countPush++;
}
//popping off the stack everytime there is an a and counting simultaneously
else if (w[i] == 'a') {
charStack.pop();
countPop++;
}
}
}
//if the stack is not empty and a = b-1 then the word exists
if (!charStack.empty() && countPop == (countPush - 1))
return true;
else
return false;
}
int main() {
//introduction
cout << "The language rule is L = {a^n-1b^n}" << endl;
//creating an empty string for the word and condition variable for exit
string word = " ";
int exit = 0;
while (exit == 0) {
//getting word from the user
cout << "\nWord: ";
cin >> word;
//calling function to check if word exists in L
if (isInLanguageL(word)) {
cout << "The word exists in language L" << endl << endl;
}
else
cout << "The word doesn't exist in language L" << endl << endl;;
cout << "Would you like to exit (1-yes/0-no): ";
cin >> exit;
}
system("pause");
return 0;
}
If anyone can just lead me in the right direction with regards to testing for whether the word is of a legal format (not aba abababa bababa), it would be greatly appreciated.

What is wrong with this Pig Latin program?

What it's supposed to do
My piglatin program is supposed to take a phrase from user input and output it into pig latin. Basically it would turn a word such as "hello" into "ellohay".
My problem
When I input hello my man the output is ellohay y man an may and when I just input hello my the output is ellohay y may. As you can see, after it translates the first word successfully, it struggles on the second word. It places a space after the y and mayI cannot figure out for the life of me why this keeps happening. The output is even stranger when I input more than two words, as shown above. What I want to happen is for it to output ellohay ymay anmay when I input hello my man. Code is below. Thanks!
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void phrase_Parser(string); // Goes through phrase and looks for ' ' to then pass to pigLatin_Translator()
void pigLatin_Translator(string);
int main()
{
string phrase; //used for user word or phrase to be translated to piglatin
cout << "Enter any word: ";
getline(cin, phrase);
phrase_Parser(phrase);
return 0;
}
void phrase_Parser(string phrase) {
int startCount = 0;
for (int i = 0; i < phrase.length(); i++) {
if (phrase[i] == ' ') {
string word = phrase.substr(startCount, i);
startCount = (i + 1); // decides where to start the word next time it is ran through
pigLatin_Translator(word); // runs word through translator
}
}
}
void pigLatin_Translator(string word) {
string partOne;
string partTwo;
for (int x = 0; x < word.length(); x++) {
if (word[0] == 'q' && word[1] == 'u') {
cout << word.substr(2, word.length()) << word.substr(0, 2) << "ay ";
break;
}
else if ((word[x] == 'a') || (word[x] == 'e') || (word[x] == 'i') || (word[x] == 'o') || (word[x] == 'u') || (word[x] == 'y')) {
partOne = word.substr(x, word.length()); //from first vowel to end of word
partTwo = word.substr(0, x); // from first letter to first vowel, not including the vowel
cout << partOne << partTwo << "ay "; // adding "ay" to the end of the word
break;
}
}
}
Your problem is in the line string word = phrase.substr(startCount, i);
You are using substr incorrectly. The second argument to substr is the length of the substring you wish to extract. Replace i with i-startCount and you should be good to go.
Alternatively, search for a nicer way to split strings. There are a number of options that are much easier than doing it manually.

C++ cin.ignore To ignore spaces, numbers, and characters to test a palindrome

I need to process a user input to see if it is a palindrome. Our professor said to use cin.ignore() to ignore spaces, numbers, and other characters so we will just compare the letter inputs.
So far I have just found code that ignores just one of these at a time and the code is more advanced than my learning so I do not know how to modify or apply it to my code.
I have the code to check the palindrome, I just do not know how to make it ignore the unwanted inputs.
Sorry this sort of question has been asked many times over but I cannot seem to figure it out.
Thanks in advance.
do
{
checkInput = false;
cout << "Enter the Palindrome: ";
getline(cin, input);
len = input.length();
if (len == 0)
{
cout << "\nNo data was entered, please enter a palindrome.\n";
checkInput = false;
}
} while (checkInput);
for (int i = 0, j = input.size() - 1; i < input.size(); i++, j--)
{
if (input[i] != input[j] && input[i] + 32 != input[j] && input[i] - 32 != input[j])
{
isPalindrome = false;
break;
}
}
if (isPalindrome)
{
cout << "This is a Palindrome!!" << endl;
}
else
{
cout << "This is not a Palindrome." << endl;
}
im assuming that input is a string. if so then what we want to do before your for loop is have another for loop to run through the string and remove anything unwanted from the string. I am going to assume that all you want to deal with are the upper and lowercase letters, as we are focusing on the ascii values between 65-90 and 97-122. source http://www.ascii-code.com/
to so do we can simply check each index in the string to see if it falls between these two ranges, and if it doesn't, then delete it.
for(unsigned int i = 0; i<input.size();i++)
{
if(input[i]< 65 || (90 <input[i] && input[i] < 97) || input[i] > 122)
{
input.erase(i,1);
i--
}
}
that should work.

Issues with if string validation loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm working on a validation if loop which checks for pipes at the beginning and end and makes sure there are 32 valid characters (valid chars are : and |)
I'm wondering why my program is not reading the if statement correctly for a 32 character input. Here is what I have so far.
void checkitout(string validate)
{
string check;
check = validate;
if ((check.length() == 31) &&
(check.substr(0,1) == "|") &&
(check.substr(31,1) == "|"))
{
cout << "is this running?";
for (int i = 0; i < 31; i++)
{
cout << "for loop running";
if (!(check.substr(i, 1) == ":") || !(check.substr(i, 1) == "|"))
{
cout << "Please enter acceptable barcode.";
return;
}
}
}
else
{
cout << "else Please enter acceptable barcode";
}
}
I'm new to this but I think I'm on the right track. The couts are to test to see if the loop is working. It goes right to the else state. Here is a sample input
||:|:::|:|:||::::::||:|::|:::|||
As always, any thoughts on how to do this better are greatly appreciated.
Your string has a lenght of 32, thus the if-condition is false because of check.length() == 31.
Also the if-condition in your loop needs an "&&" instead of an "||", since you want it to be neither "|" nor ":" to be an unacceptable barcode.
Changes are marked in bold.
void checkitout(string validate)
{
string check;
check = validate;
string one = check.substr(4,1);
cout << (check.substr(4,1) == one) << endl;
if ((check.length() == **32**) &&
(check.substr(0,1) == "|") &&
(check.substr(31,1) == "|"))
{
cout << "is this running?";
for (int i = 0; i < 31; i++)
{
cout << "for loop running";
if (!(check.substr(i, 1) == ":") **&&** !(check.substr(i, 1) == "|"))
{
cout << "Please enter acceptable barcode.";
return;
}
}
}
else
{
cout << "else Please enter acceptable barcode";
}
}