what is wrong in this easy if else c++ code? [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 1 year ago.
Improve this question
This question is in hackerrank conditional statements and i wanted to solve it without using array.The code runs but gives wrong output. Can anyone see what is the mistake in this code , Why would it not work?
#include <bits/stdc++.h>
using namespace std;
int n ;
cin >> n ;
int main()
{
int n ;
cin>>n;
if(n=0){
cout<<"zero";
}
else if(n=1){
cout<<"one";
}
else if(n=2){
cout<<"two";
}
else if(n=3){
cout<<"three";
}
else if(n=4){
cout<<"four";
}
else if(n=5){
cout<<"five";
}
else if(n=6){
cout<<"six";
}
else if(n=7){
cout<<"seven";
}
else if(n=8){
cout<<"eight";
}
else if(n=9){
cout<<"nine";
}
else{
cout<<"Greater than 9";
}
return 0;
}

You should use '==' not '='
because "if (n=1)" behaves the same as "if(true)"
== is compare
= is assignement

Related

Codeforces question (unable to detect problem in my code) [closed]

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 2 years ago.
Improve this question
Over the past 2 days i was practising on codeforces . I am new to programming and currently doing implementation problems.
This question is 1337B ( KANA AND DRAGON QUEST)
https://codeforces.com/problemset/problem/1337/B
I cannot figure out my mistake in this code. Please help me out
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
cin>>t;
for (int i=0;i<t;i++)
{
long long int x,n,m;
cin>>x>>n>>m;
for( int j=0;j<n&&x>0&&x>20;j++)
{
x=x/2+10 ;
}
x=x-m*10;
if (x>>0)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
}
return 0;
}
Your code is correct but you made a small error at the end. You do x>>0 instead of x > 0. ">>" is a bitwise shift operator and not a greater than operator. After checking with codeforces test input it does seem to work. Here is the version with no error
if (x>0)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}

C++: invalid use of 'void' [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 6 years ago.
Improve this question
This code should print i = 35 as result but somehow it doesn't even compile. Why ?
#include <iostream>
using namespace std;
void increment(int &p){
p = p +10;
}
int main()
{
int i = 10;
increment(i) += 15;
cout<<"i = " <<i<<endl;
return 0;
}
No it shouldn't! increment has void as return type, that means that an expression call to this function has no value. If you want that call to be able to be used on the left part of an assignment, it must return a left-value.
Basically, when you write a=b a denotes a container but b a value.
You can try:
int &increment(int &p){
p = p +10;
return p; // return the reference passed as argument...
}
int main()
{
int i = 10;
increment(i) += 15;
cout<<"i = " <<i<<endl;
return 0;
}

Postfix notation stack C++ [closed]

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 6 years ago.
Improve this question
I am new to C++ and I want to use a stack to evaluate an expression given as an input (2+3*5+4 for example), containing only numbers, + and *. I wrote this code but it gives me Segmentation fault: 11. Could you please help me solve this or give me a hint about what could be wrong? Thank you! (I noticed there were similar questions on this site, but I couldn't use them to solve my problem)
#include <iostream>
#include <stack>
using namespace std;
bool highPrecedence(char a, char b){
if((a=='+')&&(b=='*'))
return true;
return false;
}
int main()
{
char c = 'a';
double x;
stack<char> stack;
double v[10];
int i=0;
double res;
while(true)
{
c = cin.get();
if(c=='\n'){
while(stack.size()!=0){
if (stack.top()=='*'){
double res = v[i]*v[i-1];
i--;
v[i]=res;
stack.pop();
}
if (stack.top()=='+'){
res = v[i]+v[i-1];
i--;
v[i]=res;
stack.pop();
}
}
break;
}
if ( '0'<=c && c<='9' )
{
cin.putback(c);
cin>>x;
cout<<"Operand "<<x<<endl;
i=i+1;
v[i]=x;
}
else
{
if(c!=' ') cout<< "Operator " <<c<<endl;
if (stack.size()==0)
stack.push(c);
else{
while((!highPrecedence(stack.top(),c)) && (stack.size()!=0)){
if (stack.top()=='*'){
double res = v[i]*v[i-1];
i--;
v[i]=res;
stack.pop();
}
if (stack.top()=='+'){
res = v[i]+v[i-1];
i--;
v[i]=res;
stack.pop();
}
}
stack.push(c);
}
}
}
cout<<v[0]<<endl;
}
Using stack.top() is illegal if the stack is empty.
Change while((!highPrecedence(stack.top(),c)) && (stack.size()!=0)){
to while((!stack.empty()) && (!highPrecedence(stack.top(),c))){
The initiali value of i is not good and you are printing uninitialized variable, which has indeterminate value.
Change int i=0; to int i=-1;

c++ compiling errors void 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 8 years ago.
Improve this question
When i try to compile these errors shows up
line 5: [Error] expected initializer before 'void'
line 27: [Error] expected unqualified-id before '{' token
help me i need to send it to my doctor by 3 hours
#include<iostream>
using namespace std;
void ConvertToCelsuis (int temp)
void ConvertToFahernheit (int temp)
int main()
{
char ch;
int temp;
cout<<"enter a temprature followed by a charcter"<<endl;
cin>>temp>>ch>>endl;
if (ch=='f'|ch=='F')
ConvertToFahernheit (temp);
else if (ch=='c'|ch=='C')
ConvertToCelsuis (temp);
Return 0;
}
void ConvertToFahernheit (int temp)
{
float F;
}
{
F=(temp*1.8)+32;
cout<<F;
cout<<endl;
}
void ConvertToCelsuis (int temp)
{
float C;
{
C=((temp-32)*0.5);
cout<<C;
cout<<endl;
}
}
on these lines:
void ConvertToCelsuis (int temp)
void ConvertToFahernheit (int temp)
you are missing semicolons. After you fix this you will have to delve into other compilation problems, for eg:
cin>>temp>>ch>>endl;
That line is not going to work.
You need a semicolon at the end of your declaration.
void ConvertToCelsuis (int temp);
void ConvertToFahernheit (int temp);

A true if condition is being skipped [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 trying to figure out why this condition is being skipped.. It is a 2d array, it's pretty self explanatory what's being compared. Here's the code first followed by explanation.
if ((reversed[i][j] == true) && (circle[i][j] == 'C'))
{
reversed[i][j] == false;
}
else if (circle[i][j] == 'C')
reversed[i][j] == true;
The problem is that it's skipping the else if EVEN when it's true; I've tested using a cout just before the code where I say:
cout<<circle[i][j];
and it's showing that it is indeed 'C' this is very weird, I've never seen anything like it. I hope there's something simple I'm missing.
circle is defined as follows:
char **circle;
circle = new char *[SIZE];
for (int i = 0; i < SIZE; i++)
circle[i] = new char[SIZE];
Are you sure that you wanted to write
if ((reversed[i][j] == true) && circle[i][j] == 'C'))
{
reversed[i][j] == false;
}
else if (circle[i][j] == 'C')
reversed[i][j] == true;
instead of this?
if ((reversed[i][j] == true) && circle[i][j] == 'C')
{
reversed[i][j] = false; // assignment here
}
else if (circle[i][j] == 'C')
reversed[i][j] = true; // assignment here