1

I'm trying to read input from an array into a string, but it's not working so well. Here's my code.

std::string str1; //for(int i = 0; grid[0][i].letter !=0; i++) char ch1 = grid[0][0].letter; char ch2 = grid[0][1].letter; char ch3 = grid[0][2].letter; char ch4 = grid[0][3].letter; char ch5 = grid[0][4].letter; char ch6 = grid[0][5].letter; cout << ch1 << ch2 << ch3 << ch4 << ch5 << ch6; str1 += ch1 + ch2 + ch3 + ch4 + ch5 + ch6; cout << str1; 

Earlier, I had set the grid[0][x] to be equal to the chars 'e' 'g' 'g' 'c' 'a' 't', respectively. When I print all the ch chars next to each other, it displays "eggcat", as it should. However, for some reason, when I try to print out str1, which should also display "eggcat", it instead only gives me the letter 'k'. I've been trying for a while to fix this, but I haven't been able to at all. Does anyone know what the problem might be? I'm only #including iostream, iterator, and algorithm, so if I'm just forgetting to #include something, please let me know!

1
  • 2
    If you are interested in the math look at this paste bin pastebin.com/h8cPQs5G Commented Apr 22, 2014 at 18:31

4 Answers 4

3

You are adding a lot of characters before applying str1 += result_of_addition.

Make it:

str1 = str1 + ch1 + ch2 + ch3 + ch4 + ch5 + ch6 

An operation char + char results in char - the operation std::string + char results in a std::string. The precedence of the operator += is less than the precedence of the operator +. Hence all chars (in your example) are added before that result is added to your string.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly, thank you. Everything I found that related to adding chars to a string told me to use +=, which I'm not familiar with. Thank you!
2

Use a stringstream:

#include <sstream> stringstream ss; ss << ch1 << ch2 << ch3 << ch4 << ch5 << ch6; str1 = ss.str(); 

Comments

0

You can just keep appending chars to a std::string.

#include <string> #include <iostream> int main() { std::string str; str += 'a'; str += 'b'; str += 'c'; str += 'd'; std::cout << str << std::endl; }; 

Output

 abcd 

Comments

0

Try the following

std::string str1; str1.reserve( sizeof( grid[0] ) / sizeof( *grid[0] ) ); for ( const auto &cell : grid[0] ) str1.push_back( cell.letter ); cout << str1; 

The above code also can be written as

std::string str1; str1.reserve( sizeof( grid[0] ) / sizeof( *grid[0] ) ); for ( const auto &cell : grid[0] ) str1 += cell.letter; cout << str1; 

Or even the following way

std::string str1; str1.reserve( sizeof( grid[0] ) / sizeof( *grid[0] ) ); for ( char c : { grid[0][0].letter, grid[0][1].letter, grid[0][2].letter, grid[0][3].letter, grid[0][4].letter, grid[0][5].letter } ) { str1 += c; } cout << str1; 

Or you could use standard algorithm std::transform for example

#include <algorithm> //... std::string str1; str1.reserve( sizeof( grid[0] ) / sizeof( *grid[0] ) ); std::transform( std::begin( grid[0] ), std::end( grid[0] ), std::back_inserter( str1 ), []( decltype( *grid[0] ) &cell ) { return cell.letter; } ); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.