string and space

Hi. I have a problem, because in my code if i put space in string, it doesn't work. Please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
string kon;
void ken()
{
    cin >> kon;
    if (kon == "help" )
    {
        cout << "\n";
        cout << "actual nothing\n";
        cout << "more of nothing\n";
        cout << "\n";
        getch();
        ken();
    }
    else if (kon == "nothing is nothing?")
    {
        cout << "\nyes\n"; // just nothing
        getch();
        ken();
    }
}

is should react when i write:
nothing is nothing?
but its only close the window


edit: thanks guys, its work with
 
getline((cin >> ws), kon);

and also i don't write what i did in main int because its only opening ken()

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{

    cout << "write password" << endl;
    cin >> h4sl0;
    if (h4sl0 == "7")
    {
        cout << "\n done \n";
        ken();
    }
    return 0;
}

anyway thanks
Last edited on
std::cin will stop when it encounters a whitespace character.
You need to use std::getline() when your input contains spaces.
http://www.cplusplus.com/reference/string/string/getline/
Hello ASCIIIsVeryHelpful,

That is because you have used cin to get your input. cin will empty it's buffer until it encounters a white space or a newline character (\n).

To get awholw line with spaces you need to use std::getline(std::cin, kon);. This will retrieve whatever you type on the keyboard.

Hope that helps,

Andy
i used it and it doesn't work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string kon;
void ken()
{
        getline (cin,kon);
        if (kon == "help" )
    {
        cout << "\n";
        cout << "actual nothing\n";
        cout << "more of nothing\n";
        cout << "\n";
        getch();
        ken();
    }
    else if (kon == "nothing is nothing?")
    {
        cout << "\nyes\n"; // just nothing
        getch();
        ken();
    }
}

and also its close after i open it
Last edited on
i used it and it doesn't work

You're providing a small snippet of code without context. Please provide a minimal code sample that reproduces the problem you're experiencing to get cogent advice about your code when presenting an issue.

I would guess that you are now mixing formatted and unformatted extraction from the input stream, but it's just an educated guess.

Try: getline((cin >> ws), kon); which will remove any stray newline hanging around in the input stream prior to calling getline.
Last edited on
Hello ASCIIIsVeryHelpful,

The "function ken()" works fine for me. Without knowing what you did in "main" it is hard to know what might be causing the problem.

Something you could try before the getline:
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); and at the top of the file include this:
1
2
#undef max
#include <limits> 

these lines are best put at the end of your includes.

I have not seen cire's suggestion before, but that would work too.

Hope that helps,

Andy
Topic archived. No new replies allowed.