Well, there really was no need to create a new post for this... I've taken your code from your other post and modified it a bit. The keyword here is non-whitespaced characters. Characters are not strings. This means you have to initialize eight different char data types holding values of a character, or make a char array. I chose to to the latter.
.push_back() function is just a function that belongs to the string object. Many C++ containers have this function such as a vector container.
#include <iostream>
#include <string>
usingnamespace std;
int main() {
// The length is 8
const size_t charLength = 8;
// A char array that contains the characters making up the word "abstract"
char someString[charLength] = {
'a',
'b',
's',
't',
'r',
'a',
'c',
't'
};
// Declare a string that has no value
string EightChars;
// Loop through the char array and push back each character into the string
for(size_t i = 0; i < charLength; i++)
EightChars.push_back(someString[i]);
// Print the string
std::cout << "The word is: " << EightChars << std::endl;
return 0;
}
Ask yourself "how can I let the user input 8 characters". It's really simple. Remember you are not dealing with strings at first, but characters. Therefore the user should not enter a string.
After the user feeds in eight characters to the program, there will be a function that will concatenate (link together) the characters in such a way to then form a string. This means that you will have to either have an array of char data types or eight unique identifiers that are of char data types.
1. Have the user input each individual character. You can achieve this by having 8 std::cin statements.
2. Concatenate the characters to form a string via the push_back function like so in the above.
#include <iostream>
#include <string>
int main()
{
const std::size_t nchars = 8 ; // number of characters to be input
std::string str ;
std::cout << "enter " << nchars << " non-whitespace characters: " ;
while( str.size() < nchars ) // as long nchars characters haven't been added
{
char c ;
std::cin >> c ; // read in one non-whitespace char
str += c ; // or: str.push_back(c) ; // and append it to the string
}
// print out all nchars characters (all characters that are in the string)
std::cout << str << '\n' ;
}