2 simple things with strings in C++

Hi there.
I just need some help to do two stupid things (I´m really a beginner).

Question 1: How can I initialize an empty string?
I´m doing:

char name[20]
strcpy(name,output);
cout<<name<<endl;

, but then some extrange characters appear at the beginnin of the string.

Question 2: How can I concatenate blank spaces?
I´m doing:
strcat(name," ");

,but it seems that strcat erases blank spaces.

Any ideas? I think you can help me.
Ty in advance.
Regards
You are mixing C (eg char[]) and C++ (eg cout) here.

If your intention is to use C++, you should try to use C++ string.

http://www.cplusplus.com/reference/string/string/
Ty.
I have declared an struct, which has a field called name:

struct Ficha{
string name ();

...
}

So, now i´m triyng to do:
Ficha FichaProducto;
...
string nombre(output);//It creates nombre with "output" content.
nombre+=(" ");//It adds a blank space an the end

FichaProducto.name.append(nombre);

, and there C++ message:

Insufficient Contextual information to determine type.

Whats wrong here?

Thanks again
Last edited on
This works for me:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

struct Ficha  {
  string name;
  static void Test() {
    Ficha FichaProducto;
    string output( "foobar" );
    string nombre(output);//It creates nombre with "output" content.
    nombre += " ";//It adds a blank space an the end
    FichaProducto.name.append(nombre);
    cout << "'" << FichaProducto.name << "'" << endl;
  }
};

int main ()
{
  Ficha::Test();
  return 0;
}


Please use code blocks when posting code - makes it easier to read.
Sorry about code block use. Didnt know that.

Man, u just did the same than me? And yours works?
I cannot understand why...
Topic archived. No new replies allowed.