How do a compare a char to a number? 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 3 months ago.
Improve this question
in my current program I have the user input a number that gets stored in a char list. After that I go through the list and try to assign that number to an int variable. That is where I run into an error. This is an excerpt of my code.
if (list1[j]=='1');
z=1;
if (list1[j]=='2');
z=2;
if (list1[j]=='3');
z=3;
if (list1[j]=='4');
z=4;
The issue is that z always becomes 4 even if list[j]=3. I know I am making a mistake my comparisons but I've been unable to locate it. I would appreciate your help.
This is how I define the list:
char list1[32];
And this is how I fill it up:
for(int i=0;i<(2*c);i+=2)
{
cin>>list1[i]>>list1[i+1];
}

You can replace your if statements with a single math line:
z = list1[j] - '0';
This works for most encodings.
Note: the above statement only works with single digit characters.
Edit 1: switch vs. if
If you insist on comparing, I believe a switch would be more readable:
switch (list[j])
{
case '1': z = 1; break;
case '2': z = 2; break;
case '3': z = 3; break;
// ...
}
The single statement above is still less code, less chances for defects.

All you need to do is drop the semicolons from your if statements. If you include the semicolon after each if statement the code-block in the if statement won't execute. It should look like this:
if (list1[j]=='1')
z=1;
if (list1[j]=='2')
z=2;
if (list1[j]=='3')
z=3;
if (list1[j]=='4')
z=4;

Related

Why 2 is not showing prime? [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 months ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int i = 2;
while(i<n){
if(n%i==0){
cout<<"not Prime";
break;
}else{
cout<<"Prime";
break;
}
i++;
}
return 0;
}
This code is written for showing prime or composite/notPrime numbers but 2 is prime & why it is not showing in output?
I write this code for getting for identifying that given number is prime or not. It can work on any number/digit but it can't show about "2" . So why is it?
Because 2 is special number it's even prime and you have to add special case for 2.
for more info: https://mathworld.wolfram.com/EvenPrime.html#:~:text=The%20unique%20even%20prime%20number,%22oddest%22%20prime%20of%20all.
Your program will not work for 2 because the condition goes false and it will never enter to the loop!
Update
Look here for primality test
https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test

Why does the compiler execute the while loop again even when the condition is not getting satisfied? [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
while(ans == 'Y' || 'y')
{
cout<<"--MENU--"<<endl;
cout<<"1.Create a file"<<endl;
cout<<"2.Display that file"<<endl;
cout<<"3.Exit"<<endl;
cout<<"Enter your choice-";
cin>>ch;
switch(ch)
{
case 1:
create(); //create and display functions are written above this, which are not required
break;
case 2:
display();
break;
case 3:
exit(0);
}
cout<<"Do you want the menu again?(Y or y for Yes and anything else for a No)";
cin>>ans;
}
My expectation is:-
When the input is other than Y or y in ans in the last line, the control of the program should exit the loop and execute the next line...
However it executes while loop again. Why?
Suppose if the input is n in the last line, then the compiler should check whether ans contains the character Y or y and should exit the loop.
The condition inside the while ans=='Y'||'y' always evaluates to true.
It should actuay be: ans == 'Y' || ans == 'y'
The way it was written, it only evaluates the equality comparison for ans=='Y' and then it does a logical or with 'y' which is interpreted as true (any non 0 value in C is considered true) so the whole condition is always evaluated true, regardless of the first part. I hope this makes sense.

save variable from 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 7 years ago.
Improve this question
Couldn't find an answer on google because I didn't know how to phrase is.
I have a regular function as below and would like to update the variable number in the first if statement. I've tried all sorts of combos but nothing works.
int main()
{
int apple, number;
cout << "Enter you number"<< endl;
cin >> apple;
if (apple == 1){
number = 2;
}
else {
number = 3;
cout << number << endl;
}
How would I change the above so I get 2 to output to the screen?
Thanks in advance!
You need to use
if (apple == 1)
instead of
if (apple = 1)
== is used for comparison. Also to note that your code will always assign the value 2 to the variable apple as in your condition you are not comparing rather you are assigning. So in your case the output will always be 2.

Binary Conversion 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 8 years ago.
Improve this question
In order to convert a given number to binary I wrote this code
//Binary conversion
int num,count=0;
int bi[15];
cout<<"Enter number";
cin>>num;
while(num>=1){
bi[count]=num%2;
num=num/2;
count++;
}
for(int i=0;i<=count;i++){
cout<<bi[count-i];
}
But the answer is wrong.It gives a -85993460 at the front.
If I want to convert 10 the result would be -859934601010.
Can someone please point out what's wrong with this code
When i is zero, the expression count-i is one position after the last entry of the array; this is undefined behavior, so an arbitrary number, such as -85993460, can be printed, or the program could crash.
To print your array backwards, use bi[count-1-i] instead, and end the loop upon reaching count:
for(int i=0 ; i != count ; i++) {
cout<<bi[count-1-i];
}
Your loop limits are off-by-one - the loop should be
for(int i=1;i<=count;i++){
cout<<bi[count-i];
}

"while" loop not ending when it should [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
Here's the very basic code. The loop will start, even if the condition is false, and it won't end.
int oldpick = 6;
int pick = rand() % 5;
while (pick = oldpick){
pick = rand() % 5;
}
pick = oldpick
This assignes the value of oldpick to pick, and then enters the loop. I think you wanted ==
while (pick == oldpick){
Also, this line:
pick = rand() % 5;
will not give a number higher than 4, so the condition could never be realised, even with ==
All of this could have been seen if warnings had been activated when compiling.
You have a simple typographical error in your while loop condition...
while (pick = oldpick) should be while (pick == oldpick) for equals or while (pick != oldpick) for not equals.
Plus, pick will never ever equal 6 as 6 % 5 == 5