Hi, I am new to C++ and I joined this forum with hopes of learning as much as i possibly can about the language. This seems like a nice place with a mature community. I bought a C++ book and I'm just now learning the basics such as goto, for, while, and do-while statements, but I'm already encountering problems


When I compile my code and I want to use "

cin >>

anything " I cannot use it because if i try, the

cin.get()

that I put at the end of the code doesn't execute, and I'm stuck with having to use

system( "pause")

.

If you need more clarification just say so, thanks a lot.

please post complete example -- what you posted doesn't make much sence.

For example, if i compile

int main ()
{
    int a;
    
    cout << "Hello World";
  
  cin.get();
 return 0;
}

Everything is fine, the Hello World message executes, and i get a blinking underscore waiting for me to press any key so that the window would close.

But, if I put in a "cin >>" anywhere in the code, the window no longer gives me a blinking underscore and closes the window in a split second after i type in whatever I want for "a" and press enter.

int main ()
{
    int a;
    
    cout << "Hello World, how are you?";
    cin >> a;
  cin.get();
 return 0;
}

So I'm forced to use system( "pause" ) instead of cin.get() because that is the only way my window will stay open after all of the operations have been performed, and I have no idea how to fix it.

I hope this clarifies it a little bit more, thanks.

At the beginning of your program add

#include <iostream>
using namespace std;

That should fix your problem.

The reason you have that problem is because you hit the Enter key after entering the integers, so the '\n' (Enter key) is still in the keyboard. The next line says to get anything stil in the keyboard. In your first example there was nothing in the keyboard buffer so the program waited for you to type something. But in the second example the keyboard buffer still had '\n', and cin.get() used that.

To fix that problem you need to flush the input keyboard buffer of all remaining keys. There is no really good reliable way to do it, but using getline() will work ok for most purposes, like this:

int main ()
{
    int a;
    
    cout << "Hello World, how are you?";
    cin >> a;
  cin.get();
 return 0;
} int main ()
{
    int a;
    std::string line;    
    cout << "Hello World, how are you?";
    cin >> a;
    getline(cin,line);
  cin.get();
 return 0;
}

Thanks a lot, may I ask what was the std:string line; and the getline(cin,line) for?

Thanks a lot, may I ask what was the std:string line; ...

Define the variable line as a string variable

... and the getline(cin,line) for?

get a line ( getline() ) entered from the keyboard up to the \n and put it in the variable line.

Thanks a lot, may I ask what was the std:string line; and the getline(cin,line) for?

I guess I wasted board banwidth making my previous post :) Did you read the writing or just skip over it and looked at the code ?

BTH you will have to include <string> in order to compile that code.

by the way why dont you use getch() at the end of the main function. It will wait for the user to press enter before the program closes.

you have to include header file conio.h for that

int main ()
{
    int a;
    
    cout << "Hello World";
  
    cin>>a;
    getch();

}

Sorry, I read over it but it didn't register. Thanks.

by the way why dont you use getch() at the end of the main function. It will wait for the user to press enter before the program closes.

you have to include header file conio.h for that

int main ()
{
    int a;
    
    cout << "Hello World";
  
    cin>>a;
    getch();

}

Because it's not portable. It's not part of standard C/C++. Why not just use a C++ function, like cin >> dummy; or a call to getline()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.