I am new in programming I am studying about for loop I tried it in various ways and output every time was different.
Code when I wrote only "a" to initialize for loop:
input was "Enter binary number:11011"
Output was "Decimal Number=27"
#include <iostream>
using namespace std;
int main(){
int a,b,c=1,d=0;
cout<<"Enter binary number:";
cin>>a;
for (a;a!=0;a=a/10) {
b=a%10;
d=d+b*c;
c=c*2;
}
cout<<"Decimal Number="<<d<<endl;
}
Code when I wrote "int a" to initialize for loop:
input was "Enter binary number:11011"
Output was "Decimal Number=0"
#include <iostream>
using namespace std;
int main(){
int a,b,c=1,d=0;
cout<<"Enter binary number:";
cin>>a;
for (int a;a!=0;a=a/10) {
b=a%10;
d=d+b*c;
c=c*2;
}
cout<<"Decimal Number="<<d<<endl;
}
Code when I wrote nothing to initialize for loop:
input was "Enter binary number:11011"
Output was "Decimal Number=27"
#include <iostream>
using namespace std;
int main(){
int a,b,c=1,d=0;
cout<<"Enter binary number:";
cin>>a;
for (;a!=0;a=a/10) {
b=a%10;
d=d+b*c;
c=c*2;
}
cout<<"Decimal Number="<<d<<endl;
}
Your first and last code snippets do not differ from each other, apart from a being evaluated and its value discarded in the initialization section of the first for loop
Your second loop re-declares a without initializing it. Variable a inside the for loop is not the same as the variable a outside the loop, so reading its uninitialized value is undefined behavior.
Since you do not really need a separate loop variable, using a while loop is a more common choice:
while (a) {
b = a%10;
d += b*c;
c *= 2;
a /= 10;
}
Note the use of compound assignment expressions +=, *=, and /=. A compound assignment of the form x += y is equivalent to x = x + y
Related
I'm starting out to learn C++ and I'm a bit clueless as to what might be happening here. when I run the program and enter 2 equal inputs the int variable-result, automatically assigns itself the value, when it should not because none of the if statements are fulfilled.
#include <iostream>
#include <cmath>
using namespace std;
// this is the function to find the highest value among 2.
int MaxNum(int Num1, int Num2) {
int result;
string asdf;
if (Num1 > Num2) {
result = Num1;
} else if (Num1 == Num2) {
asdf = "equal inputs";
} else if (Num2 > Num1) {
result = Num2;
}
// here if the 2 given values are equal the variable result, should not be
// assigned any value but when it is tested, it automatically assigns
//itself the input value.
cout << result;
cout << asdf;
}
int main() {
int Uno;
int Dos;
cout << "enter 2 nos and ill tell the highest.";
cin >> Uno;
cin >> Dos;
MaxNum(Uno, Dos);
return 0;
}
When you define a variable, it will always have some value. This value, if you do not assign one, can be anything - just whatever value happens to be in the memory where it is defined. So you might see the number you inputted here, but that can be just a coincidence, or caused by some more complicated reasons on how programs use and recycle memory (because of how the stack is built up, but that is out of scope here). Either way, you cannot count on this.
nb- it is good practice to always initialize your variables (for example int result = 0). If you don't do that, bugs can get hard to reproduce when you start building more complicated programs.
I'm new to programming so please write your answer as basic as possible. I made a simple calulator in C++. It's supposed to add 2 numbers but for some reason the output is always 16, no matter the numbers. Can someone explain this to me? This is the code:
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int sum = a + b;
cout << "Enter a number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
cout << sum;
return 0;
}
But, when i do this (creating the int sum first and then assigning it later), it works:
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int sum;
cout << "Enter a number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
sum = a + b;
cout << sum;
return 0;
}
int sum = a + b;
Is not an algebraic rule, it is a statement evaluated at that point in the sequence of statements.
Just do it after your inputs.
In the first example, you're initializing the sum variable before the initialization of a and b so before initialization, the a and b will contain some garbage value and that's why you're getting output 16 the garbage value can be anything. Just initialize your sum variable after a and b variables have some user inputted values.
And if you're doing some addition then it's a good practice to initialize your result variable(sum) with zero sum=0 so it also doesn't contain any garbage values
When you use
int sum = a + b;
sum initilized to whatever a + b evaluates to. The value of sum does not change when you set the values of a and b after that statement. In your case, neither a nor b has been initialized before that statement. Hence, it causes undefined behavior.
The second version of your program works correctly since you are assigning a + b to sum after a and b have been assigned values from user input.
This program does the addition of two integers, the program works perfectly (the exercise asks me to use constructor and past integers on it ).
But on the constructor, if I initialize num1 = nbre1 instead of nbre1 = num1the program doesn't work.
Any explanation about the order ?
#include <iostream>
int main(){
class op{
public :
int nbre1, nbre2;
op(int num1, int num2){
nbre1=num1;
nbre2=num2;
std::cout<<"numbers initialized";
}
int add(){return nbre1+nbre2 ;}
};
int n1;
int n2;
std::cout<<"Enter the first integer >> ";
std::cin>>n1;
std::cout<<"\n";
std::cout<<"Enter the second integer >> ";
std::cin>>n2;
op addition(n1,n2);
std::cout<<"The sum of the two numbers is >> " << addition.add();
return 0;
}
The C++ assignment operator = sets the left hand value equal to the right hand value. The statement num1 = nbre1 sets num1 to be the uninitialized value in nbre1. If nbre1 was previously initialized, the statement sets num1 to that value but since num1 isn't being used anywhere else in that function, it essentially does nothing.
I'm trying to understand something. I'm still a beginner to c++ and I just created this little program where you input a value and it tells you whether it's even or odd. To do this, I made an integer called "result" which takes value, and then does % 2 operation.
However, my first mistake was that I put int result above "cin >> value" so for some reason that messed up the program and the number would always be even no matter what. Then when I put int result below "cin >> value" the program worked like it should. Why is it doing this?
Any help would be appreciated thank you. I apologize if this is a duplicate but I don't know what to search for.
#include <iostream>
#include <string>
#include "Human.h"
#include <ctime>
using namespace std;
int main() {
int value = 0; // where I input
cin >> value;
// if you put int result above cin program changes.
int result = value % 2;
if (result == 0) {
cout << "Even number." << endl;
}
else {
cout << "Odd number." << endl;
}
return 0;
}
Any code whichever programming language you use runs from top to bottom.
You need to first declare the variable, give it a value and then check for being even or odd.
When you used cin after setting the value of result = value%2; the compiler used the originally initialized value for value which is 0 to compute the value of result which will be 0%2.
That's why you need to use cin>>value; before setting result = value%2;.
C++ read the code top to bottom , line by line. So you will have to int your variable first.I made a much more simpler version of the program if you want to read it:
#include <iostream>
using namespace std;
int main() {
int a;
cout << "a=";
cin >> a ;
if(a%2==0)
{cout<<"a is even";}
else
{cout<<"a is uneven";}
}
When you put int result = value % 2; before cin >> value;, your program will calculate the result before you put a value inside int value via your input.
So your program does calculate int result = 0 % 2;
I'm trying to write a very simple program in C++ that finds the modulus of two numbers as follows:
#include <iostream>
using namespace std;
int n;
int d;
int modulus;
int main()
{
cout<<"***Welcome to the MODULUS calculator***";
cout<<"Enter the numerator, then press ENTER: ";
cin>>n;
cout<<"Enter the denominator, then press ENTER: ";
cin>>d;
modulus=n%d;
cout<<"The modulus is ---> "<<modulus;
return 0;
}
But, when I try to compile it, I get the following:
How can this be solved?
Thanks.
You get the error because the name of your global variable modulus clashes with std::modulus. To fix this, you can:
Make modulus a local variable
Rename the modulus variable
Remove using namespace std and either import the names you need from std individually or qualify them with std::
Because you have using namespace std; it clashes with std::modulus
Corrected version:
#include <iostream>
using std::cout;
using std::cin;
int main()
{
cout<<"***Welcome to the MODULUS calculator***";
cout<<"Enter the numerator, then press ENTER: ";
int n;
cin>>n;
cout<<"Enter the denominator, then press ENTER: ";
int d;
cin>>d;
int modulus=n%d;
cout<<"The modulus is ---> "<<modulus;
return 0;
}