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 == '+')
Related
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;
}
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 1 year ago.
Improve this question
Why is this program returning random letters after output?
#include <iostream>
using namespace std;
int guessIt(int num) {
if(num == 27) {
cout << "You Won";
}
else {
cout << "You Lost";
}
}
int main() {
cout << guessIt(27);
}
There are some problems in your code:
#include <iostream>
using namespace std;
int guessIt(int num) { //when you make the function guessIt(int num) you say it returns an int
if(num == 27) {
cout << "You Won";
}
else {
cout << "You Lost";
}
} //code finished without returning an int
int main() {
cout << guessIt(27); //What happens here? guessIt didn't return anything, so cout prints something it got that was in the memory where guessIt was supposed to put it's return value
}
What you have to do is either add returns, like return 0; if you win and return 1; if you lost instead of cout<<"You won"; or cout<<"You Lost"; Then in main() add
if (guessIt(27)==0) {
//TODO: Win message here
}
else {
//TODO: Lose message here
}
You can also change guessIt's return value to void, and remove the cout<<guessIt(27) and just have guessIt(27). (void means it will return nothing.)
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!!!";
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'
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 6 years ago.
Improve this question
#include <iostream>
#include <string>
using namespace std;
int main ()
{
do
{
string name, answer;
cout << "Welcome to the prime number checker! Please enter your name: ";
getline (cin, name);
int a;
cout << "\nHello " << name;
cout << "\nPlease enter an integer: ";
cin >> a;
cin.sync();
if (a == 2)
{
cout << "\nThis is a prime number" << endl;
}
else
{
for (int b = 2; b < a; b++)
{
if (a % b == 0)
{
cout << "This number is not prime number" << endl;
break;
}
else
{
cout << "This number is a prime number." << endl;
break;
}
}
}
cout << "Do you want to do this again (Yes or No)?";
getline (cin, answer);
}
while (answer == "yes" || answer == "YES" || answer == "Yes"); //Not declared in this scope
return 0;
}
You declared answer within the do block. But then try to reference answer outside of that scope block.
Declare answer at the top of main instead of in the do block.
You need to move the declaration of answer outside the loop:
string answer;
do {
string name;
...
} while (answer == "yes" || answer == "YES" || answer == "Yes");
If you declare it inside the loop, it no longer exists by the time the while clause is evaluated.
As other people said, the "answer" variable only exists inside the loop - it isn't accessible from outside it.
One other recommendation: rather than checking every possible permutation of capitalization just cast the whole string to lowercase. (You actually missed several - there are 6 total because each position could have one of 2 possible values. Presumably something like "YeS", for example, should still be accepted as "yes").