if statement not being implemented in c++ [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 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;
}

Related

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") )

Getting same output everytime [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 2 years ago.
Improve this question
This a simple number guessing game. If you guessed the number right, it outputs "You win!!!", but if the number of tries (numberofguesses) is exceeded, it should output "You lose", but it is showing "You win!!!" even though I checked the values of numberofguesses, secretnum and guess after the while loop. Answer in simple words, I'm a beginner.
#include <iostream>
using namespace std;
int main()
{
int secretnum = 7;
int guess = 0;
int numberofguesses = 3;
while (secretnum != guess && numberofguesses != 0) {
cout << "enter your guess: ";
cin >> guess;
--numberofguesses;
}
if (secretnum = guess && numberofguesses != 0) {
cout << "You win!!!";
}
else
{
cout << "You lose";
}
}
You have mistaken the assignment operator = with the comparison operator ==.
In this line here:
if (secretnum = guess && numberofguesses != 0)
cout << "You win!!!";
Change it to:
if (secretnum == guess && numberofguesses != 0) {
cout << "You win!!!";

New to coding need help on if statement [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 3 years ago.
Improve this question
I am trying to code an activity we did in class the first 3 if statements work but the last 2 shows an error statement has no effect [-Wunused-value].
#include <iostream>
#include <cmath>
using namespace std;
char getSign()
{
int grade;
char Sign;
cout << "Enter grade: ";
cin >> grade;
if(grade == 100)
{
Sign = 'A';
}
else if(grade == 95)
{
Sign = 'B';
}
else if(grade == 90)
{
Sign = 'C';
}
else if(grade == 85)
{
Sign == 'D';
}
else
{
Sign == 'F';
}
return Sign;
}
int main()
{
cout << "Your card grade is: 0" << getSign();
return 0;
}
The result is if I do enter the number 85 or any other number it show 0w
You have a typo Sign == 'D', it should be Sign = 'D'. The same for 'F'

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

c++ if statement malfunction [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 4 years ago.
Improve this question
why are my else if loops not working
the code when ran skips over the else statement and executes the if statement even if it is not true
#include<iostream>
using namespace std;
int a;
int b;
char operation;
int sum;
int sub;
int mul;
int divide;
int mod;
int main()
{
cout<< "enter a number \n";
cin >> a>>operation>>b;
if (operation= '')
{
sum=a+b;
cout << a<<operation<<b<<sum;
}
else if (operation= '-')
{
sub=a-b;
cout << a<<operation<<b<<sub;
}
else if (operation= '/')
{ if(b==0){
cout<<"You cannot divide by zero";
}
else{divide=a/b;
cout << a<<operation<<b<<divide;}
}else if (operation= '*')
{
mul=a+b;
cout << a<<operation<<b<<mul;
}
else if (operation= '%')
{
mod=a%b;
cout << a<<operation<<b<<mod;
}
else{
cout<<"Invalid input";
}
return 0;
}
the output is always just adding the numbers no matter what sign the input has it wont go over my else if statements even if the operation isnt + infact even if i use a wrong operation it just adds the two numbers
there are two thing to get right,
the comparison in the if statement should be done with == not with =
= will assign the rightside value to the variable operation also you missed + in the first comparison
if (operation= '') should be if (operation == '+')