-4

For the following code:

#include<iostream>
using namespace std;
int main ()
{
       int cin;
       cin >> cin;
       cout << "cin : " << cin;
       return 0;
}

I expected the output to be:

cin : <input>

But the output is:

cin : <junk value>
Shivam Arora
  • 476
  • 5
  • 18

2 Answers2

7

OK let's dissect your code (using namespace std assumed):

#include <iostream>
using namespace std;
int main ()
{
       int cin; // Shadows the declaration of cin coming from the iostream header
       cin >> cin; // A bit shift operation on variable cin without capturing the result
       cout << "cin" << cin; // Output of the non initialized variable cin
       return 0;
}

Kinda proof

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • can you please explain a bit more where I can land into trouble using **using namespace std;** – Shivam Arora Jun 27 '17 at 20:21
  • @ShivamArora Sure: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – πάντα ῥεῖ Jun 27 '17 at 20:22
  • @ShivamArora People are guessing that you think `cin` in `cin >>` refers to `std::cin`. – juanchopanza Jun 27 '17 at 20:22
  • 1
    How are you answering anything when there is no question? And why are you providing code that does not compile? cout is no qualified with std:: and you did no include On any C++11 capable compiler, the output Is 'cout' : undeclared identifier. – Christopher Pisz Jun 27 '17 at 20:22
  • @ShivamArora does this really not convince you? This is the farthest into land of trouble I ever saw someone getting with using namespace std – 463035818_is_not_an_ai Jun 27 '17 at 20:22
  • @tobi303 What if OP's real code doesn't have `using namespace std;`? – juanchopanza Jun 27 '17 at 20:23
  • @juanchopanza its the only way i can imagine to get the result OP is reporting.... wait no...you are completely right. I am 99% certain that the original code has a using namespace, but yes, unless OP fixes his post there is no point in making assumptions. – 463035818_is_not_an_ai Jun 27 '17 at 20:25
  • @tobi303 How about `using std::cin;`? – juanchopanza Jun 27 '17 at 20:26
  • @juanchopanza then it would not compile, unless it also had a `using std::cout`, but really you dont have to convince me. The OP misses a mcve and cannot be answered properly as is. And actually even if it had a `using namespace std;` the bad thing about it is a bad choice of variable names and the using issue is just secondary imho – 463035818_is_not_an_ai Jun 27 '17 at 20:32
  • @tobi303 Local scope 1st, as always. – πάντα ῥεῖ Jun 27 '17 at 20:33
  • @πάνταῥεῖ you should probably mention that the local `cin` is shadowing the global `cin` – Mgetz Jun 27 '17 at 20:39
  • 1
    @ChristopherPisz I dont want to join a pointless fight, but I saw the original answer before any edits and it had a note saying that it assumes that there is a `using namespace std;`. I agree that the question should first be improved before it can be answered properly, but I think you are overreacting a bit – 463035818_is_not_an_ai Jun 27 '17 at 20:48
6

Ok, that is your opportunity to join the white side of the force and stop using using namespace std;

The following example works fine for int inputs:

#include <iostream>

int main ()
{
       int cin;
       std::cin >> cin;
       std::cout << "cin: " << cin;
       return 0;
}

Why? In your example your local name int cin will take preference over cin from std and cause your program have UB using an int variable without initializing.

And a good advise, but offtopic could be to test the result of std::cin::operator >> with the failbit like this link

Rama
  • 3,222
  • 2
  • 11
  • 26
  • Thank you Rama. I don't know why people are down voting anyways thanks for your valuable time – Shivam Arora Jun 27 '17 at 20:20
  • its a fix, but doesnt really explain why OPs code produces the output it does – 463035818_is_not_an_ai Jun 27 '17 at 20:20
  • @ShivamArora Your question is being downvoted because its not a very good question. Yes, it is a question, but its such a basic question that you could pick up a basic C++ book, read the first chapter, and figure out what your problem is. You showed that you did not do any research nor understand what you wrote. – Javia1492 Jun 27 '17 at 20:25
  • @tobi303 Thanks!, added explanation – Rama Jun 27 '17 at 20:40
  • 1
    @ChristopherPisz Thanks, Added an advise regarding to failbit check – Rama Jun 27 '17 at 20:41