How would I add a character to a string?

I have a while loop that continually asks for an input (char). I want the input to be stored in a string after each iteration. For example, if I have an input of "x, c, z", I want my string to store "xcz."

See: std::string::push_back() and related functions.
You can just append it to the end, like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <iostream>

int main() {
    std::string str;

    // some condition, just 10 characters here for demonstrative purposes
    for (int i = 0; i < 10; ++i) {
        char c;
        std::cin >> c;
        str += c;
    }

    std::cout << str << std::endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.