Infinite Output

i have declared the variable str and defined it as follows..
1
2
3
char str;
std::cout<<"\n Enter name: ";
    std::cin>>str;

whenever i enter something like ish kri, i.e., any name with a space in it, it gives me an infinite loop. i tried declaring the variable as a string too, but that didn't help either. PLEASE HELP :)
Last edited on
char only takes in a single character. This may be what you are looking for:

http://www.cplusplus.com/reference/string/

If you still have a problem, then it is likely something else in your code.
use getline instead of cin:

1
2
3
4
5
6
7
8
9
10
11
#include <string>

int main ()
{
    std::string str;
    /* bla bla */
    std::getline ( cin, str );
    /* bla bla */

    return 0;
}
thank you :) :D
Topic archived. No new replies allowed.