Inputting a string and a char

This is a discussion on Inputting a string and a char within the C++ Programming forums, part of the General Programming Boards category; Code: How do I input a string to compare it to a character? Like I want to input '}' and ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Inputting a string and a char

    Code:
    How do I input a string to compare it to a character?
    Like I want to input '}' and input  '*/' and compare them.

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Code:
    char input[SIZE];
    fgets(input, SIZE, filePointer);
    // now input contains a string given by the user

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    You input a string consisting of a single word (no whitespace) like this

    std::string str;

    std::cin >> str;

    If you want a whole line (everything typed before return, including whitespace)

    std::getline(std::cin, str);

    If you want to see if the first character matches 'a' then you can test (str[0] == 'a')

    If you want to see if the string contains 'a' anywhere (mathing "contains" but not "string") then if (str.find('a') == std::string::npos) is true, 'a' was not found (find is equal to the position of the first 'a' found, npos is string's opinion of infinity)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21