Starting a class and may need help

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Postby Blueteeth » Fri Sep 26, 2003 10:46 am

you must define the variable Name as "string" not "char" and add
#include <string>.
User avatar
Blueteeth
 
Posts: 616
Joined: Thu Sep 25, 2003 9:54 pm
Location: Egypt

Postby Tronn » Sat Sep 27, 2003 6:05 am

Shouldn't that be <cstring> ?
User avatar
Tronn
 
Posts: 21
Joined: Thu Sep 25, 2003 3:02 pm
Location: Essex, UK

Postby icujc » Sat Sep 27, 2003 6:19 am

Tronn wrote:Shouldn't that be <cstring> ?


Nope he had it right. You could also just make this a char array like this:

char Name[15];
--

"Computers are like air conditioners - They can't do their job
properly if you open windows."
User avatar
icujc
 
Posts: 83
Joined: Mon Sep 22, 2003 3:41 pm
Location: Alabama

Postby omnius » Sat Sep 27, 2003 7:24 am

C++ supports two different types of strings. It inherited C-style strings from C, represented by null-terminated character arrays, and also supports a string type as part of the C++ standard library.

C-style strings are declared as an array of characters, thus:

char myString[size];

where size if the number of elements (characters) in the string. This is usually the maximum number of characters your string is expected to hold, plus an extra element for a null-terminator. This is required for the string functions that are in header <string.h>, or the C++ equivalent <cstring>.

C++ style strings using std::string are declared this:

string myString;

The std::string class includes many useful functions for manipulating your strings. To use it you include the header <string>.

Learning how to use strings in C++ is pretty fundamental. Your C++ reference book should provide details of both types above, and typically they start with the inherited C-style although for many purposes the std::string class should be preferred as a general case.
omnius
 
Posts: 496
Joined: Wed Sep 24, 2003 12:03 pm

Postby Tronn » Sat Sep 27, 2003 8:09 am

icujc wrote:
Tronn wrote:Shouldn't that be <cstring> ?


Nope he had it right. You could also just make this a char array like this:

char Name[15];


I don't think it's a case of "Nope he had it right" because you can use <cstring> as well.

Code: Select all
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    string name;
   
    cout << "Please enter your name: ";
    cin >> name;
    cout << "\nHello " << name;
   
    return 0;
}


So we were both right.
User avatar
Tronn
 
Posts: 21
Joined: Thu Sep 25, 2003 3:02 pm
Location: Essex, UK

Postby icujc » Sat Sep 27, 2003 8:30 am

Code:

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
string name;

cout << "Please enter your name: ";
cin >> name;
cout << "\nHello " << name;

return 0;
}




That didn't work for me ;)

This works:

Code: Select all
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string name;
   
    cout << "Please enter your name: ";
    cin >> name;
    cout << "\nHello " << name;
   
    return 0;
}
--

"Computers are like air conditioners - They can't do their job
properly if you open windows."
User avatar
icujc
 
Posts: 83
Joined: Mon Sep 22, 2003 3:41 pm
Location: Alabama

Postby Tronn » Sat Sep 27, 2003 8:35 am

So they both work then. :D
User avatar
Tronn
 
Posts: 21
Joined: Thu Sep 25, 2003 3:02 pm
Location: Essex, UK

Postby icujc » Sat Sep 27, 2003 8:49 am

Tronn wrote:So they both work then. :D


The <cstring> didn't work for me. As far as I can remember <string> is used for Null-terminated character arrays often referred to as C strings and <cstring> is a wrapper that defines the macros traditionally defined in the Standard C library header <string.h>.
--

"Computers are like air conditioners - They can't do their job
properly if you open windows."
User avatar
icujc
 
Posts: 83
Joined: Mon Sep 22, 2003 3:41 pm
Location: Alabama

Postby Tronn » Sat Sep 27, 2003 8:52 am

Hmmm. Strange. cstring worked fine for me.

I'm using Borland 5.5.1 compiler.
User avatar
Tronn
 
Posts: 21
Joined: Thu Sep 25, 2003 3:02 pm
Location: Essex, UK

Postby omnius » Sat Sep 27, 2003 9:30 am

Tronn wrote:Hmmm. Strange. cstring worked fine for me.

I'm using Borland 5.5.1 compiler.

The std::string requires the header <string>. The C-style strings require either <string.h> or the equivalent <cstring>. Any other behaviour is down to your particular compiler, and is quite likely to fail if you write code on (or port your code to) another compiler.
omnius
 
Posts: 496
Joined: Wed Sep 24, 2003 12:03 pm

Postby omnius » Sat Sep 27, 2003 9:32 am

icujc wrote:As far as I can remember <string> is used for Null-terminated character arrays often referred to as C strings

No, definitely not. <string> is for std::string, not C-strings. Any reasonably modern beginner's book should cover this.
omnius
 
Posts: 496
Joined: Wed Sep 24, 2003 12:03 pm

Postby Tronn » Sat Sep 27, 2003 11:13 am

So the following code shouldn't work then?

Code: Select all
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    string name;
   
    cout << "Please enter your name: ";
    cin >> name;
    cout << "\nHello " << name;
   
    return 0;
}
User avatar
Tronn
 
Posts: 21
Joined: Thu Sep 25, 2003 3:02 pm
Location: Essex, UK

Postby omnius » Sat Sep 27, 2003 11:40 am

I would expect it to fail to compile on most compilers.
omnius
 
Posts: 496
Joined: Wed Sep 24, 2003 12:03 pm

Postby raimo » Sat Sep 27, 2003 11:58 am

Indeed, <cstring> should contain the basic functions to manipulate null-terminated character arrays, like strcpy, strcat and so.
User avatar
raimo
 
Posts: 372
Joined: Fri Sep 26, 2003 6:50 am
Location: Finland

Next

Return to For Beginners

Who is online

Users browsing this forum: No registered users and 2 guests