+ 2
Why 8?
I am working on a VERY basic calculator, and I have run into a weird bug/glitch. I have to inputs that are entered by the user. However, I found that if you enter only one number and then enter it in, it still uses a second number. That number is 8. Always, for no reason that I know. Why does it do that? Is there a way to fix it? Here is my code: https://code.sololearn.com/cGLc2557MYvd/#cpp Also, my favorite number is 8. What a coincidence.
6 Answers
+ 1
when declaring int give it default value 0
+ 4
It is a good coding practice if you initialize your integer variables with zero or any other number you required. So that they won't use their own garbage value if they are not assigned any value within the program
it's a me-a, meme addict int num1 = 0, num2 = 0;
+ 2
It's a question I asked just yesterday, I also found it buggy at first.
See the answers I got
https://www.sololearn.com/discuss/1456938/?ref=app
+ 1
let's change it little bit,
initialize two variables as giving it a value 0 and put an if like below:
int int1,int2;
cin >> int1 >> int2;
if(int1!=0&&int2!=0)
{
statements;
}
or making step by step input like-:
cout << "enter first:";
cin >> int1;
cout << "enter second:"
cin >> int2;
0
it's a me-a, meme addict If you didn't type any input in the code Playground, the fail bit of cin is set to 1. Then you can use cin.fail() to check the bit's state and perform additional steps.
Eg :
cin>>num1;
if(cin.fail()) {cout<<"Wrong/No Input for num1!"; return 0;}
cin>>num2;
if(cin.fail()) {cout<<"Wrong/No Input for num2!"; return 0;}
0
RIJFAS I dont see how that would help? initialising the variables with 0, sure, but an if statement? why?