tags:

views:

670

answers:

4

I keep getting this error: initializer-string for array of chars is too long Even if I change num and length to 1, it still gets the error:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    const int num = 11;
    const int length = 25;
    char array[num][length] = { "Becky Warre, 555-1223"
                                "Joe Looney, 555-0097"
                                "Geri Palmer, 555-8787"
                                "Lynn Presnell, 555-1212"
                                "Holly Gaddis, 555-8878"
                                "Sam Wiggins, 555-0998"
                                "Bob Kain, 555-8712"
                                "Tim Haynes, 555-7676"
                                "Warren Gaddis, 555-9037"
                                "Jean James, 555-4939"
                                "Ron Palmer, 555-2893" };

    char search[length];
    cout << "Enter a string to search: ";
    cin.getline(search, length);

    char *ptr = NULL;
    int i;
    for (i = 0; i < num; i++)
    {
        ptr = strstr(array[num], search);
        if (ptr != NULL)
            cout << array[i];
    }
    if (ptr == NULL)
        cout << "No match found" << endl;

    return 0;
}
+2  A: 

You're entering one big string in your array. You need to separate the strings with commas.

JRL
And make sure your longest string is no longer than 24 chars, as one more character is needed for terminating 0.
Arkadiy
+5  A: 

I think it's because there aren't any commas in your array initialization...

char array[num][length] = { "Becky Warre, 555-1223",
                                                    "Joe Looney, 555-0097",
                                                    "Geri Palmer, 555-8787",
                                                    "Lynn Presnell, 555-1212",
                                                    "Holly Gaddis, 555-8878",
                                                    "Sam Wiggins, 555-0998",
                                                    "Bob Kain, 555-8712",
                                                    "Tim Haynes, 555-7676",
                                                    "Warren Gaddis, 555-9037",
                                                    "Jean James, 555-4939",
                                                    "Ron Palmer, 555-2893" }
Quintin Robinson
wow thank you, cant believe i missed that
Raptrex
@Raptrex It happens, I'm sure we've all been there.
Quintin Robinson
Adjacent string literal concatenation is very handy - when you intend to use it. When you don't, it can lead to mystifying problems.
Jonathan Leffler
+4  A: 

Seems you forgot to add comma's. Initializing a char* array is done like this:

char entries [number_of_items][lenght]  = { "entry1", "entry2", .... };

Apart from that, you can save yourself a lot of trouble by using an array of std::strings:

std::string entries[] = { "entry1", "entry2", ... };
xtofl
+1 for std::string.. no vector though?
Quintin Robinson
Good idea! I save the `vector` for next time :)
xtofl
+1  A: 

Aside from the missing commas in your set of initializers, why not just skip making it a 2-dimensional array of char? You're only passing the array data to strstr(), and all it wants are null terminated strings. Why not use the more flexible:

char* array[] = { 
    "Becky Warre, 555-1223",
    "Joe Looney, 555-0097",
    "Geri Palmer, 555-8787",
    "Lynn Presnell, 555-1212",
    "Holly Gaddis, 555-8878",
    "Sam Wiggins, 555-0998",
    "Bob Kain, 555-8712",
    "Tim Haynes, 555-7676",
    "Warren Gaddis, 555-9037",
    "Jean James, 555-4939",
    "Ron Palmer, 555-2893",
    NULL
};

Now that the array is just a set of pointers (and it's terminated with a NULL pointer), the loop can look something like:

char *ptr = NULL;
int i;
for (i = 0; array[i] != NULL; i++)
{
        ptr = strstr(array[i], search);
        if (ptr != NULL)
                cout << array[i];
}

One advantage of this is that you can add new strings to the data without having to update any corresponding 'size' numbers. And you'll never have to worry about the compiler complaining that the array is too small in one dimension or another - it'll just make the array the size it needs to be.

Michael Burr