I need some explanation

How can I make a program that can read multiple non-whitespace characters and form a string using the string .push_back() function.

This has always confused me I would appreciate any help.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>

using namespace 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;
}

Last edited on
I'm trying to have it where the user would input the eight characters then the program will output all eight characters
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.
So like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

using namespace std;

int main() {

  string someString;

  const size_t charLength = 8;

  char c1, c2, c3, c4, c5, c6, c7, c8;

  cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6 >> c7 >> c8;

  someString = c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8;



  string EightChars;


  for(size_t i = 0; i < charLength; i++)
    EightChars.push_back(someString[i]);


  std::cout << "The word is: " << EightChars << std::endl;
  return 0;
}
 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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' ;
}
Topic archived. No new replies allowed.