New to coding need help on if statement [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 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'

Related

I am not getting correct even and odd of the given number using function [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 5 days ago.
Improve this question
#include <iostream>
using namespace::std;
int iseven(int a)
{
if (a / 2 == 0) {
return 1;
}
return 0;
}
int main()
{
int num;
cin >> num;
if (iseven(num)) {
cout << "number even";
}
else {
cout << "odd h number";
}
return 0;
}
I am expecting even and odd of the number, but I am getting odd as a only output. help me
You need to compute a modulo b to find the remainder of a / b. Modulo operator is %.
Also, replace return type of the function with bool.
#include <iostream>
using namespace std;
bool iseven(int a)
{
return a % 2 == 0;
}
int main()
{
int num;
cin >> num;
if (iseven(num)) {
cout << "even";
}
else {
cout << "odd";
}
return 0;
}

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

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

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 == '+')

Boolean function always returning true [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 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.