Thread: Problems with Strings and Arrays.

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079

    Problems with Strings and Arrays.

    I've been writing a program for the past few days. It mostly checks out, but the only thing the compiler is complaining about is the "string arrays". I've been thinking about how to do these and tried a few methods, but I always got the same error output.

    First, I'll say that I'm using Dev-C++ for a compiler.

    Here are the "String array" methods I tried:


    Code:
    char str[10][100]; /* In otherwords, 10 strings of 100 characters. */
        str[0][100] = "First string text"; /* I've tried this with and without the [100] there. */
        str[1][100] = "Second string text";
        str[2][100] = "Third string text";
        /* ...and so on. */
    and this...

    Code:
    string str[10]; 
        str[0] = "First string text";
        str[1] = "Second string text";
        str[2] = "Third string text";
        /* ...and so on. */
    Neither of these compiled and gave me the errors:

    Code:
    28   C:\Documents...   ISO C++ forbids declaration of `descriptions' with no type. **This is where I was 
                                                                                     setting values for each 
                                                                                     part of the array.**
    
    28   C:\Documents...   Building.cpp conflicting types for `int str[0][200]'   **As was this one...**
    
    27   C:\Documents...   previous declaration as `char descriptions[10][200]'   **This was the variable 
                                                                                   declaration line.**

    Does anyone know why these methods are generating these errors? Is it improper syntax? Does anyone know a better way of doing this?
    Last edited by SlyMaelstrom; 04-14-2005 at 11:33 PM.

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    if thats ur exact code where is the ";" statement ending, for the array calls?

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Oh... no I was just writing that as an example and forgot the ;

    I'll add them now. In the mean time pretend they're there.

    Thanks.

  4. #4
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    Code:
    #include <string>
    using namespace std;
    ...
    string first_name = "Bjarne";
    this is how you declare a string
    see the following link for more details
    http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm
    this topic has been brought up many times on the board.
    do a search, or try google
    here is another good string link.
    http://www.cppreference.com/cppstring/

    i found this by searching string array on google
    Code:
    string jmena[pocetJmena]; //note: the [] appears after the variable
    //populate array: jmena[0] = "Ruzicka Vit"; jmena[1] = "Bily Saruman"; jmena[2] = "Veliky Saruman"; jmena[3] = "Calculator Veliky"; jmena[4] = "Jakekoliv Jmeno"; jmena[5] = "Nekdo Veliky"; jmena[6] = "Ahoj Vitek"; jmena[7] = "Whatever Name";
    here is the link
    http://forums.devx.com/showthread.php?t=142001

    give it a try, and tell us if it works.

    i would use string arrays, if you can.
    i feel it is easier to manipulate then a character array.
    but if you want details on character arrays. the following does single and multiple arrays
    http://www.cplusplus.com/doc/tutorial/tut3-1.html
    Last edited by xviddivxoggmp3; 04-14-2005 at 11:47 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I found the same link on google. It's the same way that I'm declaring it if you look at my code. It's still generating the errors.

    From everything I read, this seems like it should work. I was thinking that maybe it was just my compiler.
    Last edited by SlyMaelstrom; 04-15-2005 at 12:10 AM.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    #include <iostream.h>
    #include <string.h>
    
    using namespace std;
    
    struct room {
           string roomNum;
           };
           
    room desc;
    string descriptions[10];
           descriptions[0] = "This is a description.";
           descriptions[1] = "This is also a description.";
    int main() {
        int roomIndex = 0;
        desc.roomNum = descriptions[roomIndex];
        
        do {
        cout << desc.roomNum;
        roomIndex = roomIndex + 1;
        } while (roomIndex < 2);
        
        cin.get();
    }
    Here is a little example program that I just wrote. Can someone tell me why this doesn't compile?

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    struct room {
           string roomNum;
           };
           
    
    int main() {
        int roomIndex = 0;
        room desc;
        string descriptions[10];
           descriptions[0] = "This is a description.";
           descriptions[1] = "This is also a description.";
               
        do {
        desc.roomNum = descriptions[roomIndex];
        cout << desc.roomNum;
        roomIndex = roomIndex + 1;
        } while (roomIndex < 2);
        
        cin.get();
    }
    Last edited by JoshR; 04-15-2005 at 12:18 AM.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    As xviddivxoggmp3 mentioned, it's not <string.h> you need to include. Just <string>

    Other than that, with a few adjustments, this code almost works:
    Code:
    #include <iostream.h>
    #include <string>
    
    using namespace std;
    
    struct room
    {
    	string roomNum;
    };
           
    room desc;
    string descriptions[10];
    
    int main(void)
    {
    	descriptions[0] = "This is a description.";
    	descriptions[1] = "This is also a description.";
    
        int roomIndex = 0;
        desc.roomNum = descriptions[roomIndex];
        
        do
    	{
    		cout << desc.roomNum;
    		roomIndex = roomIndex + 1;
        } while (roomIndex < 2);
        
        cin.get();
    
    	return 0;
    }
    Except for the cout line. But I have never used cout, so I don't know how to fix it.

    EDIT:
    Awww, beaten to it
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    ya but
    Code:
    desc.roomNum = descriptions[roomIndex];
    needs to be stated in the loop so that roomIndex has the ability to be changed by the loop, or else the next string location wont be called.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Heh... you're silly epo. <string> and <string.h> mean the exact same thing.

    The problem was that I wasn't declaring the variable within " int main() "

    That's what JoshR was showing me. Thank you very much, Josh. It works perfectly.

    Again, thanks JoshR and everyone else who put effort into helping.

    Here is the working code for the example. Feel free to throw it into a compiler:

    Code:
    #include <iostream.h>
    #include <string.h>
    using namespace std;
    
    struct room {
           string roomNum;
           };
           
    
    int main() {
        int roomIndex = 0;
        room desc;
        string descriptions[10];
           descriptions[0] = "This is a description.";
           descriptions[1] = "This is also a description.";   
        do {
        desc.roomNum = descriptions[roomIndex]; 
        cout << desc.roomNum << "\n";
        roomIndex = roomIndex + 1;
        } while (roomIndex < 2);
        
        cin.get();
    }
    Last edited by SlyMaelstrom; 04-15-2005 at 12:26 AM.

  11. #11
    *this
    Join Date
    Mar 2005
    Posts
    498
    if you really wanted a global variable you could declare it outside of main so it could be used by any function in your program, but only declerations. outside of main you could not do descriptions[0] = "this and this";

    nice work, glad to help.

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Just on a note, string and string.h are two very different things

    Using your code I get the following errors:
    Deleting intermediate files and output files for project 'asdfasdfasdf - Win32 Debug'.
    --------------------Configuration: asdfasdfasdf - Win32 Debug--------------------
    Compiling...
    main.cpp
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(3) : error C2871: 'std' : does not exist or is not a namespace
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(6) : error C2146: syntax error : missing ';' before identifier 'roomNum'
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(6) : error C2501: 'string' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(6) : error C2501: 'roomNum' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(13) : error C2065: 'string' : undeclared identifier
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(13) : error C2146: syntax error : missing ';' before identifier 'descriptions'
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(13) : error C2065: 'descriptions' : undeclared identifier
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(13) : error C2109: subscript requires array or pointer type
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(14) : error C2109: subscript requires array or pointer type
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(14) : error C2440: '=' : cannot convert from 'char [23]' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(15) : error C2109: subscript requires array or pointer type
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(15) : error C2440: '=' : cannot convert from 'char [28]' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(17) : error C2039: 'roomNum' : is not a member of 'room'
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(5) : see declaration of 'room'
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(17) : error C2109: subscript requires array or pointer type
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(18) : error C2039: 'roomNum' : is not a member of 'room'
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(5) : see declaration of 'room'
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(23) : warning C4508: 'main' : function should return a value; 'void' return type assumed
    Error executing cl.exe.
    Creating browse info file...

    asdfasdfasdf.exe - 15 error(s), 1 warning(s)
    Changing <string.h> to <string> leaves me only with one error and a warning:
    Deleting intermediate files and output files for project 'asdfasdfasdf - Win32 Debug'.
    --------------------Configuration: asdfasdfasdf - Win32 Debug--------------------
    Compiling...
    main.cpp
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(18) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<
    char> >' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\asdfasdfasdf\main.cpp(23) : warning C4508: 'main' : function should return a value; 'void' return type assumed
    Error executing cl.exe.
    Creating browse info file...

    asdfasdfasdf.exe - 1 error(s), 1 warning(s)
    The warning being common to both (tossing in a "return 0" at the end of the program will fix the warning, and again, I don't know how to fix the cout error).

    These are the first 47 lines of <string.h>
    Code:
    /***
    *string.h - declarations for string manipulation functions
    *
    *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
    *
    *Purpose:
    *       This file contains the function declarations for the string
    *       manipulation functions.
    *       [ANSI/System V]
    *
    *       [Public]
    *
    ****/
    
    #if     _MSC_VER > 1000
    #pragma once
    #endif
    
    #ifndef _INC_STRING
    #define _INC_STRING
    
    #if     !defined(_WIN32) && !defined(_MAC)
    #error ERROR: Only Mac or Win32 targets supported!
    #endif
    
    
    #ifdef  __cplusplus
    extern "C" {
    #endif
    
    
    
    /* Define _CRTIMP */
    
    #ifndef _CRTIMP
    #ifdef  _DLL
    #define _CRTIMP __declspec(dllimport)
    #else   /* ndef _DLL */
    #define _CRTIMP
    #endif  /* _DLL */
    #endif  /* _CRTIMP */
    
    /* Define __cdecl for non-Microsoft compilers */
    
    #if     ( !defined(_MSC_VER) && !defined(__cdecl) )
    #define __cdecl
    #endif
    And these are the first 41 lines of <string> (note, copyright was found at the bottom, but I added it up top so as not to infringe on any rights here)
    Code:
    /*
     * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED. 
     * Consult your license regarding permissions and restrictions.
     */
    // string standard header
    
    #if     _MSC_VER > 1000
    #pragma once
    #endif
    
    #ifndef _STRING_
    #define _STRING_
    #include <istream>
    
    #ifdef  _MSC_VER
    #pragma pack(push,8)
    #endif  /* _MSC_VER */
    _STD_BEGIN
            // basic_string TEMPLATE OPERATORS
    template<class _E, class _Tr, class _A> inline
        basic_string<_E, _Tr, _A> __cdecl operator+(
            const basic_string<_E, _Tr, _A>& _L,
            const basic_string<_E, _Tr, _A>& _R)
        {return (basic_string<_E, _Tr, _A>(_L) += _R); }
    template<class _E, class _Tr, class _A> inline
        basic_string<_E, _Tr, _A> __cdecl operator+(const _E *_L,
            const basic_string<_E, _Tr, _A>& _R)
        {return (basic_string<_E, _Tr, _A>(_L) += _R); }
    template<class _E, class _Tr, class _A> inline
        basic_string<_E, _Tr, _A> __cdecl operator+(
            const _E _L, const basic_string<_E, _Tr, _A>& _R)
        {return (basic_string<_E, _Tr, _A>(1, _L) += _R); }
    template<class _E, class _Tr, class _A> inline
        basic_string<_E, _Tr, _A> __cdecl operator+(
            const basic_string<_E, _Tr, _A>& _L,
            const _E *_R)
        {return (basic_string<_E, _Tr, _A>(_L) += _R); }
    template<class _E, class _Tr, class _A> inline
        basic_string<_E, _Tr, _A> __cdecl operator+(
            const basic_string<_E, _Tr, _A>& _L, const _E _R)
        {return (basic_string<_E, _Tr, _A>(_L) += _R); }
    template<class _E, class _Tr, class _A> inline
        bool __cdecl operator==(const basic_string<_E, _Tr, _A>& _L,
            const basic_string<_E, _Tr, _A>& _R)
        {return (_L.compare(_R) == 0); }
    All credit for that code is given to the authors stated in the copyrights...or whoever deserves credit...and if you still have problems with me posting this up here (even though it's available on any machine with C++), then sue me for all my nickels.

    My understanding of it is that .h files are from back in the day of C Programming, and files without the .h are structured for C++ Programming. When using C++ I'm fairly sure you never need to include any .h files. (There's a non .h file with what you need). Perhaps someone more familiar with the distinction can explain (or a quick google search will probably turn it up).
    Last edited by Epo; 04-15-2005 at 08:20 AM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Quote Originally Posted by SlyMaelstrom
    Heh... you're silly epo. <string> and <string.h> mean the exact same thing.
    As epo has pointed out, you are the one being silly. <string> and <string.h> are completely different.

    http://www.parashift.com/c++-faq-lit....html#faq-27.4

    <string> is for the C++ string class that you are using. <string.h> and <cstring> are basically equivalent and are part of the C standard library. They include helper functions for working with character arrays.

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    As for your post Epo, header files without the 'h' extension are called "standard C++ header files", anything that has a .h isn't a standard C++ header file and might have been a deprecated header file from the old C libraries, as are headers that are prefixed with a 'c', in this case: <cstring> or something.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing strings in arrays
    By smp in forum C Programming
    Replies: 6
    Last Post: 12-16-2008, 09:37 AM
  2. help understanding arrays of strings!
    By smoking81 in forum C Programming
    Replies: 18
    Last Post: 02-23-2008, 04:24 AM
  3. Strings and Substrings using arrays
    By dcwang3 in forum C Programming
    Replies: 12
    Last Post: 02-18-2008, 07:28 AM
  4. strings or arrays of characters?
    By Callith in forum C++ Programming
    Replies: 13
    Last Post: 12-26-2004, 11:28 AM
  5. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM