Read characters into array charArray until the character 'p' is encountered, up to a LIMIT of 10 characters. - C++ File Stream

C++ examples for File Stream:cin

Description

Read characters into array charArray until the character 'p' is encountered, up to a LIMIT of 10 characters.

Demo Code

#include <iomanip>
#include <iostream>
#include <limits>

int main(int argc, const char *argv[]) {
    const int LIMIT = 11;

    char charArray[LIMIT];
    int i = 0;/*from w w  w. j a v a 2s . c om*/

    std::cout << "Enter up to 10 chars 'p' to end input:\n";

    while (i < LIMIT) {
        if (std::cin.peek() == 'p') {
            i++;
            break;
        }
        charArray[i++] = std::cin.get();
    }

    charArray[i] = '\0';

    std::cout << charArray;

    return 0;
}

Result


Related Tutorials