Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > newbie question concerning strings

Reply
Thread Tools

newbie question concerning strings

 
 
Dominic van Berkel
Guest
Posts: n/a
 
      11-04-2004
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
----
Windows Bluescreen: Cannot write to diskette in drive C:\ //AARGH
 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      11-04-2004
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
> ----
> Windows Bluescreen: Cannot write to diskette in drive C:\ //AARGH



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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      11-04-2004
Dominic van Berkel wrote:

> 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.

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-04-2004

"Dominic van Berkel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
Tim Love
Guest
Posts: n/a
 
      11-04-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Dominic van Berkel) writes:

>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";
}

 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      11-04-2004
In message <(E-Mail Removed) >, Dominic
van Berkel <(E-Mail Removed)> writes
>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.

--
Richard Herring
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      11-04-2004
"Dominic van Berkel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question concerning pipes and large strings math math Perl Misc 29 07-08-2012 07:53 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Newbie - question concerning page behaviour Griff ASP .Net 3 12-03-2005 02:47 PM
Newbie question concerning page load event =?Utf-8?B?RmxhY2s=?= ASP .Net 3 05-17-2005 07:00 PM
newbie question concerning the "partical effect" Traddles000@gmail.com Java 1 04-19-2005 09:46 AM



Advertisments