what I want to do is replace the spaces inputted to '~10' text.
I input a string a1.
I change the string to a char array names SA so I can go through each char.
if the char is a space, I want to append ~10 to the sa string.
if the char is anything else, I would like to append the character.

The program works fine until I hit spaces.
if I input something like: hello world
the output will be hello+Garbage.
This is the best I came up with.

Thanks for taking your time to help an uber n00b :)

sa & sa1 are strings
SA is a char[50]

cin>>sa1;
strcpy(SA, sa1.c_str());
int i=0;
for(i;i<49;i++){
	if (SA[i]==' '){
	sa+="~10";
	}
	else
	sa+=SA[i];
}
sa[49]='\0';
sa+=SA[49];
cout<<sa;

> cin>>sa1;
This will stop at the first space anyway. So use getline() to read a whole line.

> for(i;i<49;i++)
Use the length of the string, rather than the length of the array.

For what it's worth, you don't need to copy your std::string to a char array just to be able to use array indexing on it.

hmm.
well this is what I have now. I've surfed around quite a bit, notably going to cplusplus.com to see how to implement getline.
It seems like cin.getline(SA,50); will prompt the user an input.
In my case it doesn't (and I really am confused)

this is what I have:

cin.getline(SA,50);
//strcpy(SA, sa1.c_str());
int i=0;
for(i;i<49;i++){
	if (SA[i]==' ')
	sa+="~10";
	else if (SA[i]=='\0')
		return;
	else
	sa+=SA[i];
}
sa[49]='\0';
sa+=SA[49];
cout<<sa;

What happens is that the getline gets the information from the previous cin.
so say I cin hello world hello world,
my cout will give me world~10hello~10world+garbage.
any reasons why?

What happens is that the getline gets the information from the previous cin.
so say I cin hello world hello world,
my cout will give me world~10hello~10world+garbage.
any reasons why?

There is no previous cin so we can't tell. We also don't know whether SA is a c-string, C++string, integer, or something else.

Without details, help can be pretty unuseful.

What you need to do is find a useful reference. I like cppreference.com, but it is rather curt.

In C++, you can easily append strings and characters, so you might try something like:

#include <iostream>
#include <string>
using namespace std;

string spacesToTilde10( string s ) {
  string result;
  for (int i = 0; i < s.length(); i++)
    if (s[ i ] == ' ') result += "~10";
    else               result += s[ i ];
  return result;
  }

int main() {
  string s;
  cout << "Please enter a string with spaces> ";
  getline( cin, s );
  cout << "I don't like spaces, so I changed your string to \""
       << spacesToTilde10( s )
       << "\". So there! Nyah!"
       << endl;
  return EXIT_SUCCESS;
  }

Like Salem said, there is no need to dink with a char array. Just use the stuff that std::string gives you.

Hope this helps.

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.