This question already has answers here:
My cpp program is not being asking for input.
(2 answers)
Closed 5 years ago.
#include<iostream>
using namespace std;
int main ()
{
int cin;
cin >> cin;
cout << "cin" << cin;
return 0;
}
This is the program. I thought it should give an error. But it compiled and gave output as cin0. i.e. cin+junkvalue
P.S. I understand that cin >> cin, is a bit shift operation here.
But My Question is why are we able to declare int cin here?
You can declare any variable name at a different scope than some existing variable. cin is not a keyword or a reserved name, just an official name of the input stream. In fact, if you don't use using namespace std; in your code, you could do std::cin >> cin; and read an integer value from the standard input. [Should add that even with using namespace std;, you can still use namespace qualifiers, so std::cin >> cin; would work "with" as well as "without" that statement - I meant rather that it's a bad habit to use using namespace ... in general, I prefer to just type the namespace wherever it is needed]
Obviously, doing these sort of things are generally a bad idea. But it's well defined and "works" as long as you know what you are doing and don't need both at the same time.
Related
This question already has answers here:
Why is the phrase: "undefined behavior means the compiler can do anything it wants" true?
(2 answers)
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 5 months ago.
I try to help people learn to code in C++ and I received an answer for an easy task to write a code that gets three numbers as input and outputs a sum of them. I received a wrong-written code with no return statement which... suprisingly works. It prints a good answer to the console, which (as far as know) shouldn't happen without return. I know it is written wrong and I will reply how it should be done but I want to be precise and include an answer why it worked. Here is the code:
#include <iostream>
using namespace std;
int add ()
{
int a,b,c, result;
cin >> a;
cin >> b;
cin >> c;
result=a+b+c;
}
int main()
{
cout << add();
return 0;
}
I would be grateful for an answer.
In some smart compilers it can run the code. but warning will be still there that function is expected to return integer but it is not returning anything.
but in environment like visual studio it will give the error and program will not be build.
#include <iostream>
using namespace std;
int main() {
int cin;
cin >> cin;
cout << "cin is : " << cin;
}
In this code it gets different output in different complier and can't find proper solution.
There are two things you probably don't understand: scope and initialization.
In the code below the global variable v is hidden by local variable v declared in main. All operations in main are performed on main's v. The same is true for cin. The cin you declared in main is not the same cin declared in std namespace. So, cin >> cin; has a different meaning. You probably expected the same behaviour as std::cin >> cin;.
double v;
int main()
{
int v;
}
c++ allows uninitialized variables. When you write int cin; memory space is allocated for cin, but nothing is written in (the variable is not automatically initialized). Leaving a variable uninitialized may be on purpose or not. Your compiler may be set to warn on uninitialized variables and/or check at run time. If you compile in debug configuration the variables may be automatically set to zero, depending on compiler, but you should not rely on this as your final build will be in release.
The answer to your question "Garbage value, Error, Segmentation fault, Nothing is printed" is garbage value (is this an interview question?):
cin is a local integer variable and
cin >> cin is different from std::cin >> cin and different from cin >>= cin.
Your code invokes undefined behavior. (Look up that term, it's an important concept that makes C++ an unreliable programming language.)
In the line int cin, you define a variable and don't initialize it.
The undefined behavior happens as soon as you read from the uninitialized cin variable.
The next undefined behavior happens depending on the value of cin. The shift-right operator is an arithmetic operation. If its right-hand side is not between 0 and the bit size of the left operand, the behavior is undefined.
you cannot declare keywords name for variable-name. cin and cout are keywords in c++.Please use different name for variable-name which used in your c++ program.
I'm trying to understand why does the following code compile?
#include<iostream>
using namespace std;
int main ()
{
int cin;
cin >> cin;
cout << "cin" << cin;
return 0;
}
How is the compiler able to distinguish in the statement: "cin >> cin"?
If you run the program, you'll notice that it never waits for input.
It doesn't distinguish anything – you're right-shifting the int by its own value.
(And that value is indeterminate, so the program is undefined.)
If you increase the warning level of your compiler, you should see "Warning: statement has no effect" or something to that effect.
To add on to molbdnilo's answer:
When you are using the statement using namespace std;, you are telling the compiler that for all variables in the current scope, loop up the identifier in current scope, if not, look up in the parent scope, until the look up reaches global scope and still unable to find the identifier, then it will try to find it in the namespace you are using.
After you declared int cin, everything that is called cin in the main function will be the local int cin not the std::cin, so all of your codes are just left shifting cin and shifting "cin" by cin amount of bits.
I'm a student and just started learning C++ last week so this question is probably very low level but I can't figure it out.
I've searched around a bit but can't find any results, or maybe I'm looking for the wrong thing.
There are two cin parts. One taking in an int outside the loop, the other taking in a string inside the loop.
I'm getting a compile error saying ""Error no operator matches these commands" even though I just used them 5 lines ago.
Help?
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
// variable declaration
const double payIncrease = 7.6;
string employeeName;
double initialSalary;
double backPay;
double employeeAnnualSalary;
double employeeMonthlySalary;
int numEmployees;
// streams of information
ofstream outStream;
outStream.open("employeeRecords.txt");
// console io's
cout<<"Enter how many employees you have:";
cin>>numEmployees;
for(int i = 0; i <numEmployees;i++)
{
cout<<"What is Employee number: "<<i<<"'s name:";
cin>>employeeName;
cout<<"How much does that employee earn now: ";
cin>>initialSalary;
}
outStream <<"annual salary was: " << numEmployees;
outStream.close();
return 0;
}
Here is a version that actually compiles. You can figure out what you missed on your own ;-)
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter how many employees you have:";
int numEmployees = 0;
cin >> numEmployees;
for(int i = 0; i < numEmployees; ++i)
{
cout << "What is Employee number: " << i << "'s name:";
string employeeName;
cin >> employeeName;
}
}
Total fluke.
i just put
#include<string>
at the top.
I wasn't aware that the console couldn't handle Strings
im getting a compile error saying Error no operator matches these commands even though i just used them 5 lines ago.
If this refers to the snipped you posted, then you're wrong. As all other functions, operators can be overloaded in C++. This means there can be several functions using the same name, provided they take different arguments (or are either const or not member functions).
The variable name numEmployees looks to me as if it would refer to a number, while employeeName likely refers to a string. So this would call two different overloads of operator>>() for inputting these variables.
For reasons I'm omitting here, the operator>>() overload reading into a string is defined in the header <string>, while those for built-in types (int etc.) are in defined in <istream>, which you usually get by including <iostream>.
So, given what little information you gave us, this is a long shot, but I suppose you're missing an #include <string>.
im getting a compile error saying ""Error no operator matches these commands" even though i just used them 5 lines ago.
This sounds like a namespace issue.
Welcome to the wonderful world of programming. ;)
I have one doubt about how to check the data type of input variables in C++.
#include<iostream>
using namespace std;
int main()
{
double a,b;
cout<<"Enter two double values";
cin>>a>>b;
if() //if condition false then
cout<<"data entered is not of double type";
//I'm having trouble for identifying whether data
//is double or not how to check please help me
}
If the input cannot be converted to a double, then the failbit will set for cin. This can be tested by calling cin.fail().
cin>>a>>b;
if(cin.fail())
{
cout<<"data entered is not of double type";
}
Update: As others have pointed out, you can also use !cin instead of cin.fail(). The two are equivalent.
That code is hopelessly wrong.
iostream.h doesn’t exist. Use #include <iostream> instead. The same goes for other standard headers.
You need to import the namespace std in your code (…). This can be done by putting using namespace std; at the beginning of your main function.
main must have return type int, not void.
Concerning your problem, you can check whether reading a value was successful by the following code:
if (!(cin >> a))
cout << "failure." << endl;
…
Also, if my memory serves, the following shortcut should work:
if (! (cin>>a>>B)) { handle error }