newbie question concerning strings

  • Thread starter Dominic van Berkel
  • Start date
D

Dominic van Berkel

Can anybody help me? I'm desperately trying even to *create* strings,
but it simply won't work. I don't want to use c-strings, and i've
tried about every possible header file. What am i doing wrong?
Dominic
 
J

JKop

Dominic van Berkel posted:
Can anybody help me? I'm desperately trying even to *create* strings,
but it simply won't work. I don't want to use c-strings, and i've
tried about every possible header file. What am i doing wrong?
Dominic


Well first of all, here's a C++ program:


int main()
{

}


If that doesn't compile, the burn your "compiler".


Firstly, there's a variable type in C++ called "char". This type is used
to store a character. For instance:

char blah = 't';

When you write a character in C++, you surround it with apostrophes.

Anyway, a string is a load of characters. What we do is stick a load of
"char"'s side by side in memory (in an array), like so:

char hello[6] = { 'H', 'e', 'l', 'l', 'o', 0 };


That zero at the end signals the end of the string. Without it, they CPU
would got reading through memory until it reaches a dead end.

Anyway, you say you don't want to use "C strings", which is what I have
described above.

I presume you're on about the class called "std::string".


#include <string>

int main()
{
std::string blah("Hello!");

blah = "Monkey!";

blah += " and Ape!";
}


-JKop
 
R

Rolf Magnus

Dominic said:
Can anybody help me?

I don't know.
I'm desperately trying even to *create* strings, but it simply won't work.

That's not really a useful error description. How did you try to create them
and what was the problem you ran into?
I don't want to use c-strings, and i've tried about every possible header
file. What am i doing wrong?

You're not providing enough information.
 
J

John Harrison

Dominic van Berkel said:
Can anybody help me? I'm desperately trying even to *create* strings,
but it simply won't work. I don't want to use c-strings, and i've
tried about every possible header file. What am i doing wrong?
Dominic

#include <string>

std::string a_string = "hello";

The header file is <string> without a .h, the name string is in the std
namespace. These are the two commonest newbie mistakes.

john
 
T

Tim Love

Can anybody help me? I'm desperately trying even to *create* strings,
but it simply won't work. I don't want to use c-strings, and i've
tried about every possible header file. What am i doing wrong?
Without more info it's hard to answer. Maybe your compiler can't cope
with ANSI C++. Here's a minimal program - it doesn't print anything,
but it should compile/run.

#include <string>
using namespace std;
int main() {
string s="hello";
}
 
R

Richard Herring

Dominic said:
Can anybody help me? I'm desperately trying even to *create* strings,
but it simply won't work. I don't want to use c-strings, and i've
tried about every possible header file. What am i doing wrong?

Failing to post any code. We're not mind readers.
 
M

Mike Wahler

Dominic van Berkel said:
Can anybody help me? I'm desperately trying even to *create* strings,
but it simply won't work. I don't want to use c-strings, and i've
tried about every possible header file. What am i doing wrong?

#include <iostream>
#include <string>

int main()
{
std::string s("Hello"); /* create a string */

std::cout << s << '\n'; /* output the above string */

return 0;
}

That's all there is to it. Simple, huh? :)

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top