isspace issue

Need is space to detect space from single input characters and print out the responses below.
seems (space) and (enter) are not working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <ctype.h> 
#include <stdio.h> 
#include <cmath> 
using namespace std;

int main() 
{
    // taking input 
    char letter;
    cin >> letter;
    
    // checking is it space
    if (isspace(letter)) 
        printf("\nThe character you entered is a whitespace character"); 
    else
        printf("\nThe character you entered is not a whitespace character"); 
    return 0;
}
(Lastchance's post is simpler than mine)

std::cin >> operator ignores whitespace.

You can use getline instead.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <cctype> 
#include <cstdio> 
#include <cmath> 
using namespace std;

int main() 
{
    // taking input 
    std::string input;
    getline(cin, input);
    
    if (input == " ")
        printf("\nThe character you entered is a whitespace character"); 
    else
        printf("\nThe character you entered is not a whitespace character"); 
    return 0;
}
Last edited on
Or
cin.get( letter );

I'm afraid that your [enter] is probably needed to signify the end of the input.
Last edited on
Thank you both, was so simple i cant belive i overlooked it. :)
Topic archived. No new replies allowed.