why we use cin.get(); ?

0 votes
asked Mar 17 by anonymous
#include <iostream>
using namespace std;

class CounterClass {
private:
static int counter;
int number;

public:
CounterClass() {
number = ++counter;
};

void tell_number() {
cout << "I am object " << number << endl;
};
};

int CounterClass::counter = 0;

int main() {
CounterClass obj1;
CounterClass obj2;
CounterClass obj3;
obj1.tell_number();
obj2.tell_number();
obj3.tell_number();

cin.get();
main();}

1 Answer

0 votes
answered Mar 25 by (380 points)
cin.get() reads a single character from the input stream. In your example however you're hot capturing that character. Perhaps you're trying to force the program to pause on input.

A worse problem is you're calling main recursively from within main. This is a very silly thing to do. You'll run out of stack frames and crash.
commented Apr 2 by (240 points)
how are you defining main?
i would definine main as the place with the most code.
where exactly should he have put cin-get? i think it should be under using namespace std; but i don't know for sure,where it should go and you are an expert on this part of c++.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...