-8

I'm a C++ programmer, who's still in the nest, and not yet found my wings. I was writing a Calendar program, and I discovered, that C++ does not support a string type. How do I make an Array, that will be able to store strings of characters?

I've thought of creating an enumerated data type, as the array type. While, it will work, for my Calendar, it won't work if say I was creating a database of the names of students in my class.

http://prntscr.com/7m074w I got; "error, 'string' does not name a type."

Tobi Alafin
  • 269
  • 2
  • 13
  • 1
    The `string` class has been part of C++ for about two decades by now, and arrays of `char` since the very beginning. Perhaps you should invest in a book? – molbdnilo Jun 26 '15 at 22:22
  • Book? If Google's first hit for "C++ string" isn't cppreference.com's [string](http://en.cppreference.com/w/cpp/string/basic_string) page, it's because it's Cplusplus.com's – user4581301 Jun 26 '15 at 22:32
  • `and I discovered, that C++ does not support a string type.` Huh? http://coliru.stacked-crooked.com/a/064380233cd870e0 – PaulMcKenzie Jun 26 '15 at 22:53
  • I have not yet started using classes. I'm still on procedural programming. I tried to create a type, for string http://prntscr.com/7m074w. I understand arrays of type char. I'm thinking of creating a database, for names. Using multiple char arrays, for that is not very robust. – Tobi Alafin Jun 27 '15 at 16:40
  • I am using a book : "progcpp.pdf" But, I haven't gotten to arrays, yet and I'm trying to follow the book sequentially. The link I added, displays the error i got. I was trying to create a string array, for a Calendar. One dimension for the year, one for the month, one for the week days, and one, for the days on each week day. – Tobi Alafin Jun 27 '15 at 16:48

4 Answers4

1
that C++ does not support a string type.

Wrong info, you can create an character array as follows

   char array[length]; 
//Where length should be a constant integer

Otherwise you can depend on standard template library container, std::string

If you have C++11 compiler you can depend on std::array

Steephen
  • 14,645
  • 7
  • 40
  • 47
1

You can create an array of characters by char name[length];. C++ also has a data type string. You can create an array of strings and store what values you'd like. here .

So

  1. use array of characters
  2. use string data type

For Example -

#include <iostream>
#include <string>
int main ()
{
  //To Create a String
  std::string s0 ("Initial string");
  return 0;
}
Community
  • 1
  • 1
Divyavrat
  • 56
  • 6
1

The C++ Standard Library includes a string type, std::string. See http://en.cppreference.com/w/cpp/string/basic_string

The Standard Library also provides a fixed-size array type, std::array. See http://en.cppreference.com/w/cpp/container/array

But you may also want to learn about the dynamically-sized array type, std::vector. See http://en.cppreference.com/w/cpp/container/vector

The language also includes legacy support for c-strings and c-arrays, which you can find in a good C++ or C book. See The Definitive C++ Book Guide and List

An example of how to use an array/vector of strings:

#include <string>
#include <array>
#include <vector>
#include <iostream>

int main() {
    std::array<std::string, 3> stringarray;
    stringarray[0] = "hello";
    stringarray[1] = "world";
    // stringarray[2] contains an empty string.

    for (size_t i = 0; i < stringarray.size(); ++i) {
        std::cout << "stringarray[" << i << "] = " << stringarray[i] << "\n";
    }

    // Using a vector, which has a variable size.
    std::vector<std::string> stringvec;

    stringvec.push_back("world");
    stringvec.insert(stringvec.begin(), "hello");
    stringvec.push_back("greetings");
    stringvec.push_back("little bird");
    std::cout << "size " << stringvec.size()
              << "capacity " << stringvec.capacity()
              << "empty? " << (stringvec.empty() ? "yes" : "no")
              << "\n";

    // remove the last element
    stringvec.pop_back();
    std::cout << "size " << stringvec.size()
              << "capacity " << stringvec.capacity()
              << "empty? " << (stringvec.empty() ? "yes" : "no")
              << "\n";

    std::cout << "stringvec: ";
    for (auto& str : stringvec) {
        std::cout << "'" << str << "' ";
    }
    std::cout << "\n";

    // iterators and string concatenation
    std::string greeting = "";
    for (auto it = stringvec.begin(); it != stringvec.end(); ++it) {
        if (!greeting.empty()) // add a space between words
            greeting += ' ';
        greeting += *it;
    }
    std::cout << "stringvec combined :- " << greeting << "\n";
}

Live demo: http://ideone.com/LWYevW

Community
  • 1
  • 1
kfsone
  • 23,617
  • 2
  • 42
  • 74
-2

C++ does have a string type: string from #include <string> If you don't want to use that, you can also use char* name = "YourTextHere..." or `char[length+1] name = "YourTextHere"

Mikemk
  • 17
  • 5
  • 1
    Wrong include file. The include for `std::string` is `#include `, note the missing 'c'. The `cstring` header file is for support of C-style character arrays. – Thomas Matthews Jun 26 '15 at 22:44