Boolean function always returning true [closed] - c++

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 5 years ago.
Improve this question
I am taking a course on Udemy to learn C++, and I am following along with the professor.
This is the exact code that is being used in the class.
You pass in a letter, and it tells you whether or not it is a vowel. However, it is saying every letter is a vowel. For example, when I pass in 'b', it says it is a vowel.
Any clue?
#include <iostream>
#include <cmath>
using namespace std;
bool isVowel(char letter) {
if ((letter == 'a') || (letter == 'e') || (letter = 'i') ||
(letter = 'o') || (letter = 'u'))
return true;
else
return false;
}
int main() {
char let;
cout << "Enter a letter: ";
cin >> let;
if (isVowel(let))
cout << let << " is a vowel." << endl;
else
cout << let << " is a consonant." << endl;
return 0;
}
I get the same problem using both codeblocks and xcode.
Thanks

Here is the problem:
(letter = 'i') || (letter = 'o') || (letter = 'u'))
You should change it with the == in order to make the comparison. = is for assignation.

You are assigning a character to the variable letter, instead of comparing the variable and the character.
A good thing to notice is that the result of an assignation is always true if the assignation was succesful, false if the assignation was not succesful. That means in your code there are at least 3 "true" booleans: (letter = 'i'), (letter = 'o'), (letter = 'u'). These assignations were succesful, so in your if statement, they correspond to the boolean true.

Related

if statement not being implemented in c++ [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 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
It seems, the if statement is not being called in the given program. The output says it's a consonant even if the input is a vowel.
#include <iostream>
using namespace std;
int main() {
char input[1];
cout << "Enter an alphabet:\n";
cin >> input;
if (input == "a" || input == "e" || input == "i" || input == "o" || input == "u") {
cout << "It is a vowel";
}
else
cout << "It is a consonant";
return 0;
}
First of all you don't need to use an array. And on top of that you should use single quotes, so you should have something like this :
int main(){
cout<<"Enter an alphabet:\n";
char input;
cin>> input;
if (input=='a' || input=='e' || input=='i' || input=='o' || input=='u' ){
cout<<"It is a vowel";
}
else
cout<<"It is a consonant";
return 0;
}

I don't exactly understand why my while loop is not catching any of these conditions [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 last year.
Improve this question
I'm trying to output the input string in reverse and when the user inputs "done", "Done", or "d" it will stop. With this, the while loop does not catch any of these conditions to stop the loop.
#include`<iostream>
using namespace std;
int main() {
string userInput;
int i;
char output;
getline(cin, userInput);
while (userInput != "done" || userInput != "Done" || userInput != "d") {
for (i = userInput.size() - 1; i >= 0; --i) {
output = userInput.at(i);
cout << output;
}
cout << endl;
getline(cin, userInput);
}
return 0;
}
replace
while (userInput != "done" || userInput != "Done" || userInput != "d")
with
while ( ! (userInput == "done" || userInput == "Done" || userInput == "d") )

terminate called after throwing an instance of 'std::out_of_range' what(): basic_string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
string word;
int l,eFound,xFound;
l = word.size();
cout <<"Enter a word: ";
cin >> word;
for (l>0 ; word.at(l)!='x' || word.at(l)!='e'; l--)
if (word.at(l) == 'e'){
eFound = true;
}
else if (word.at(l) == 'x'){
xFound = true;
}
if (eFound == true && xFound == true){
cout << "Your word, "<<word<<", contains the character 'e'"<<"\n";
cout << "Your word, "<<word<<", contains the character 'x'";
}
if (eFound == true && xFound != true){
cout << "Your word, "<<word<<", contains the character 'e'";
}
if (xFound == true && eFound != true){
cout << "Your word, "<<word<<", contains the character 'x'";
}
I'm not sure what is going on I'm trying to use a for loop to detect either e or x in a input of some word. I've clicked on other pages with the same error but they have different codes and I don't really understand what is explained. So what is causing this error? I'm 2 weeks into my first programming class, sorry if I'm asking a dumb question.
The issue is that indexing of std::string starts from zero. Not from 1. So, word.at(l) will crash if l = word.size();.
You should change the statement to: l = word.size() - 1;.
Also, Change your loop condition to for (; l >= 0 ; l--)
Suggestion:
Please go for library functions:
Like this:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word;
cout <<"Enter a word: ";
cin >> word;
bool eFound = word.find('e') != string::npos;
bool xFound = word.find('x') != string::npos;
if (eFound) {
cout << "Your word, "<<word<<", contains the character 'e'" << "\n";
}
if (xFound) {
cout << "Your word, "<<word<<", contains the character 'x'" << "\n";
}
return 0;
}

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";
}
}

Delimiter matching in c++ - segmentation fault (core dumped) [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 keep getting the segmentation fault error when running my program. I have no clue what is wrong. I googled the error message, I just don't know what it means. Any help would be great!
#include<iostream>
#include <stack>
using namespace std;
bool delimiterMatching(char *file){
stack<char> x;
int count = 0;
char ch, onTop, check;
while(ch != '/n'){
ch = file[count];
if (ch == '(' || '[' || '{')
x.push(ch);
else if (ch == ')' || ']' || '}') {
onTop == x.top();
x.pop();
if((ch==')' && onTop!='(') || (ch==']' && onTop!='[') || (ch=='}' &&
onTop!= '{'))
return false;
}
count++;
}
if (x.empty())
return true;
else
return false;
}
int main()
{
char test[50];
cout << "enter sentence: ";
cin >> test;
if (delimiterMatching(test))
cout << "success" << endl;
else
cout << "error" << endl;
return 1;
}
A segmentation fault means your program tried to access a memory address that isn't valid. Typically it means you dereferenced a dangling pointer or indexed past the end of an array.
In this case, it looks like the problem is your while(ch != '/n') line. It has two problems:
First, '/n' is not a valid character literal. You probably meant '\n', which represents a newline character.
Second, your string doesn't end with a newline character, because cin >> test reads one line and discards the newline at the end. Your loop will go past the end of the array and into whatever's after it in memory, trying to find a newline character, and eventually it'll reach a location that it can't access, causing a segmentation fault. You should be checking for '\0', which is the null character that actually marks the end of the string.
When I change the ch != '/n' to ch != '\0', the program doesn't crash.
It'd be easier and safer to use a std::string rather than a char[50], by the way.
You cant use such comparison
if (ch == '(' || '[' || '{')
Try
if (ch == '(' || ch== '[' || ch=='{')