Strings in Structures


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Strings in Structures

  1. #1
    Join Date
    Oct 2006
    Posts
    16

    Strings in Structures

    Hey guys, I'm writing this program that uses structures, and have ran into some troubles with the strings inside it. When I went to compile i got this weird error message, so i decided to remove the strings and but in char and it worked fine, i was wondering why this happens, and if there was anyway of putting in the string variables in the structure. The following is the code that I'm working with:

    Code:
    /*Structure
     *Holds information about schduled events
     */
    struct data
    {
        int eNum;            //event number
        /*The following is what is giving me to error message
        string event;        //name of event
        string location;     //Location of the event
        string time;         //start time of the event
        string day;          //day of the event
        */
        /*This works fine - but i would like to us strings so that way i won't run into troubles with the '[]' part of the char variables*/
        char event[];
        char loaction[];
        char time[];
        char day[];
    };
    I'm pretty sure there is a way to use strings in structure, and i would really like to know how, so thanks in advance
    S0n1C!

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    either put a size

    char whatever[100];

    or

    make it a pointer and assign it later

    char *whatever;

    or, use a string instead:

    string whatever; //preferred for most tasks

  3. #3
    Join Date
    Oct 2006
    Posts
    16
    What I'm saying is that I want to use a string, but it won't work, I don't know why. I don't want to use char variable types because i don't want to deal with the unused values. I don't know how long each input is going to be.
    Last edited by S0n1C; 03-30-2007 at 03:08 PM.

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    there is no reason this should not work, what is it not doing (compile, link, or execute) ?

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    You can use std::string in a struct. However, did you #include <string> ? Do you have compilation errors or linkage errors? What exactly is the problem, in other words?
    Danny Kalev

  6. #6
    Join Date
    Oct 2006
    Posts
    16
    I currently changed all of the string in the structures to char *[] just to get it to compile. When running the program i have not gotten to the part where the structure needs to be using but i'm pretty sure that it isn't going to work properly with char *[] which is why i wanted to u string.
    The following all the libraries that i have included and the structure.

    Code:
    /*Libraries*/
    #include <iostream>
    #include <conio.h>
    #include <fstream>
    #include <windows.h>
    #include <string.h>
    using namespace std;
    using std::string;
    
    /*Structure
     *Holds information about schduled events
     */
    struct data
    {
    /*Doesn't work*/
        int eNum;
        string event;
        string loaction;
        string time;
        string day;
    /*Works
        int eNum;            //event number
        char event[];       //event name
        char location[];    //loaction of event
        char time[];        //start time of event
       char day[];         //day of event
    */
    };
    I'm currently getting 4 error messages saying, syntax error before ';' all of them in front of the strings. I'm fairly new to c++, so i'm guessing that it is a small syntax error or something like that.

    Thanks in advance
    S0n1C!

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    string.h is the problem. just use <string> ... string.h is a very old C library for dealing with char arrays!

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    of course it doesn't work. you need to #include <string>, not <string.h>, which officially even doesn't exist in C++. BTW, char []* is not the same as char[] which isn't the same as char*. These are three different things, which is a good reason to avoid all three of them and stick to std::string.
    Danny Kalev

  9. #9
    Join Date
    Oct 2006
    Posts
    16
    Thanks guys!

Similar Threads

  1. Replies: 6
    Last Post: 02-23-2009, 02:13 PM
  2. deleting empty strings
    By Matrix.net in forum .NET
    Replies: 6
    Last Post: 10-16-2006, 01:10 PM
  3. Replies: 5
    Last Post: 06-19-2006, 10:48 AM
  4. Replies: 3
    Last Post: 10-03-2005, 12:57 AM
  5. Allocating memory in VB (Custom strings)
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 4
    Last Post: 03-04-2002, 08:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center