c++ string

i'm using string.h header file in c++. and i'm trying to use
string name[100]; //to store 100 name examples

why is it it says undefined symbol string?
is there something wrong with my compiler or with my code?

--many many thanks to whom will answer my question
not sure if you can make an array of strings like that, I've never tried.

I know you can make a vector of strings or a 2d char array..
just never tried to use the delimiter like that.
^ my name says it all
Last edited on
I think you cannot use string data type as an array. You should create an another array containing that string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    vector<string> v_name;
    string s_name;
    cout << "Enter 0 to stop loop.\n";
    while (s_name != "0")
    {
        cout << "Enter a name: ";
        cin >> s_name;
        cout << '\n';
        v_name.push_back(s_name);
    }

    for (int x=0; x != v_name.size(); x++)
    {
        cout << v_name.at(x) << "\n";
    }
}


There's kind of a basic example you can adapt to meet your needs
".at()" is pretty much like "[]" for arrays
".push_back()" increases the length of the vector by one and assigns the argument to the new element
".size()" returns length of vector
Last edited on
>>cPlusN00b&m1rv9n41v5
so how am I suppose to solve it.. i would like to store different string data and to display it one by one?
>cPlusN00b

but it say unable to open vector..is my compiler has a problem?
ah, yeah.. I dunno I thought everyone had that header file :/
but if you can get it working that's the way you want to go.

using the vector, you can store over 100 strings in sequence
access them using vector_name.at(delimiter)
and if you use less than 100, you won't be allocating memory you don't need
there is no other option for me??
well, like I said earlier you can use a 2d array of char
but that allows you a lot less flexibility and you won't have the benefits I mentioned above.. you have to statically allocate the array which means you'll almost always have more or less memory than you actually need. If you always put exactly 100 names in it, a 2d array would actually be better, but vectors dynamically allocate the memory you need and not much more.. try to find that header file and learn vectors! you'll thank me later ;3
Last edited on
It's perfectly possible to use an array of strings, as you have done. Since you haven't posted your code, there's no way we can tell what's wrong with it, but my guess would be that it's a namespace qualification issue. If so, you need to do one of the following:

(1) Use the complete, namespace-qualified class name:

 
std::string name[100]; //to store 100 name examples 


(2) Bring std::string into your translation unit, by putting

 
using std::string;

somewhere near the top of your .cpp file

(3) Bring the entire std namespace into your translation unit, by putting

 
using namespace std;

somewhere near the top of your .cpp file.

As cPlusN00b says, you're better off using vectors rather than arrays. The header file for the std::vector class will almost certainly be in the same place as the header file for std::string.

Last edited on
@charmcaster2549


string.h is a header that contains declarations of C standard functions. It does not contain the definition of the template class std::string.
To use std::string you have to include header <string>

For example

1
2
3
4
5
6
#include <string>

int main()
{
   std::string name[100];
}
Last edited on
use using namespace std above main

for information on string you can visit
http://codesmesh.com/understanding-strings-in-c/
Last edited on
yeah I think vlad pretty much pin pointed charm's problem ^
<string> instead of string.h and should have worked

still a statically allocated array of strings is wack biscuits when you're dealing with a lot of data that may be more or less than 100 strings

vectors are a great solution for stuff like this, eh?
Last edited on
#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
clrscr();
string name[10]={"charm", "caster"};
int i;
for(i=0; i<5; i++){
cout<<"name: ";
cin>>name[i];
cout<<endl;
}
getch();
}

this is my code . . .
im using borlad compiler
Last edited on
MikeyBoy & vlad

i'm using <string> and std now..but it says unable to open include file
and declaration syntax error..and many more errors... huhu what is the problem??

here's my code>>

#include<iostream.h>
#include<conio.h>
#include<string>

using namespace std;
using std::string;

void main()
{
std::string name[100];
clrscr();

int i;
for(i=0; i<5; i++){
cout<<"name: ";
cin>>name[i];
cout<<endl;
}
getch();
}
nd many more errors... huhu what is the problem??


My guess is you're using an outdated compiler that hasn't been updated in many years.
You'll need to find out where on your computer the string header file is located, and add it to your include path.

Unless, as cire says, you're using software that is so ancient that it doesn't have the STL library, in which case you should update your IDE and/or compiler to something less antediluvian. Programming in C++ without the STL is ridiculous nowadays!
thanks guys for your replies. actually i'm new in this language. i'm learning it by my self. so how am i going to update it? where can i get updated c++ compiler?

as i search and review my header files, i never seen string . . .
Topic archived. No new replies allowed.