Is there a member function in the string class to put two strings together at a certain index? For example... if i took these two strings... city = Memphis and state = Tennessee to put them together
like this

Memphis         Tennessee

The city would start at index 0 and state and index 16. For every case?

#include <iostream>
#include <string>
#include <cassert>

int main()
{
  std::string city = "Memphis" ;
  std::string state = "Tennessee" ;
  assert( city.size() < 16U ) ;

  std::string result = city ;
  result.insert( result.end(), 16U-result.size(), ' ' ) ;
  result += state ;
  std::cout << result << '\n' ; 
  
  // or
  result = city + std::string( 16U-city.size(), ' ' ) + state ;
  std::cout << result << '\n' ; 
}

in line ---> assert( city.size() < 16U ) ;
What does the U stand for?

Unsigned.

Different solution using stringstreams:

#include <sstream>
#include <string>
#include <iomanip>
using namespace std;
string foo(string city, string state)
{
  stringstream ss;
  string str;
  ss << left << setw(16) << city << right << state << endl;
  getline(ss, str);
  return str;
}

Yet another way:

string city = "Memphis";
string state = "Tennessee";
string result = city + string(16, ' ').substr(city.size()) + state;

Thanks for all the great ideas, they all help a lot.

Ok, New question... say kinda stuff
This code is wrong...

for (int i = 0; i <10; i++) {
    if (lineStream >> getScores) {
    scores += " " + getScores;
    }
  }

It is suppose to make a string with 10 numbers in it, and if the line doesn't have 10 numbers, fill in the rest with zero's. I think this is close, but it is not right? What am I doing wrong? (i don't want the exact code, just the idea please. Thanks :)

>cores += " " + getScores;
How is getScores declared?

oh, here is the whole function

string process_line(string line) {
  string first;
  string last;
  string scores;
  string getScores;
  string name;
  istringstream lineStream(line);
  lineStream >> last;
  lineStream >> first;
  for (int i = 0; i < 10; i++) {
    if (lineStream >> getScores) {
      scores += " " + getScores;
    }
  }
  name =  last + string(16, ' ').substr(last.size()) + first + scores;
  return (name);
}

That help?

Okay then, the only way to count your numbers is to have some kind of sentinel value at the end of the list. This could be a newline, EOF, or something else. In the case of EOF, you can just read numbers until EOF and maintain a counter up to 10. If the loop breaks and the counter isn't 10, fill in the rest of the string with zeros. Like so:

string s = ""
integer n = 0

while n < 10 and not eof() do
  s = s + get_word() + " "
  n = n + 1
loop

while n < 10 do
  s = s + "0 "
  n = n + 1
loop

..or just take the numbers in a single go in a string variable and do something like: string result = string(10, '0').substr(10 - number.size()) + number;

>..or just take the numbers in a single go in a string variable and do something like:
I'm not sure an absolute zero fill is desired. I could be wrong, but it seems to me that he wants 10 numbers and if there aren't ten numbers, complete the list using zeros. So if you type 1 2 3 4 5, the resulting string would be 1 2 3 4 5 0 0 0 0 0.

yes narue that is correct

string process_line(string line)
{
  string first;
  string last;
  istringstream stm(line);
  stm >> last >> first ;
  string ret_val = last + string(16, ' ').substr(last.size()) + first ;
  for (int i = 0; i<10; ++i )
  {
    string score = "0" ;
    stm >> score ;  // if stm.eof() is true, score remains an unchanged "0" !
    ret_val += " " + score;
  }
  return ret_val ;
}

thanks, that makes a lot of since, and is a lot simpler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.