Problem with cin.get()

Hello, before all I want to note that I am not a native english speaker, sorry for any mistake.

This is my first time in the forum, I have great experience with plain vanilla C (or that is what I like to think xD), and I am transitioning to C++.

I decided to make a simple program to test all the functions of istream, but I have a problem at the middle:

1
2
3
4
5
6
7
8
9
10
11
  #include <iostream>
      int main(){
          char prueba[255];
          int limite;
          std::cout<<"Introduce limite de caracteres"<<std::endl;
          std::cin>>limite;
          std::cout<<"Introduce la cadena de caracteres:"<<std::endl;
          std::cin.get(prueba,limite);
          std::cout<<"Tu cadena introducida es:"<<prueba<<std::endl;
          return 0;
      }


The problem is that when I type the input for limite, it goes all the way until the end of the problem, jumping the std::cin.get(prueba,limite) line.

I have narrowed to the std::cin>>limite line, and I think that it is because the buffer still contains data (I had the same problem with some programs in C using scanf, but I ressolved it using a function), but I don't know how to clear it, or if it is because another thing completely different.

If it helps, I am running this with Linux crunchbang, at the terminal using g++
So, can anyone help me please?
Last edited on
Thanks, it worked.
Topic archived. No new replies allowed.