string inside of a structure?

This is a discussion on string inside of a structure? within the C++ Programming forums, part of the General Programming Boards category; alright so im making a program to practice strings, pointers and structures. Its a structure that holds information about the ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    38

    string inside of a structure?

    alright so im making a program to practice strings, pointers and structures. Its a structure that holds information about the members of my band and I want it to output the instrument and gender as a string but im getting errors heres my code so far:
    Code:
    struct band {
    	int age;
    	char instrument [20];
    	char gender [10];
    };
    
    int main ()
    {
    	char name [50];
    	int a;
    
    	band matt;
    
    	matt.age = 14;
    	matt.gender = "male";
    	matt.instrument = "Lead Guitar";
    and im getting these 2 errors:
    Code:
    error C2440: '=' : cannot convert from 'const char [5]' to 'char [10]'
    //and
    error C2440: '=' : cannot convert from 'const char [12]' to 'char [20]'

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You can only assign string literal to char arrays when you initialize the array. In order for your code to work, you need to use strcpy.

    Why don't you use the string class instead? That would make your example work.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    If you change your struct to this:
    Code:
    struct band {
    	int age;
    	std::string instrument;
    	const char *gender;
    };
    then you can practice strings, pointers and structures all at once!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    memloop: whats the string class? I'm using #include <cstring>

    iMalc: would changing my structure to that make the example work or would I have to change it further? and whats up with the std::string thing?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Do you have a C++ book? If you don't understand what std::string means, then you are sorely in need of a good book to read up the basics before you continue.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56

    Talking

    thats simple... You cant just use '=' to give some value to a string of char like you would in some other language because normally they use overloaded operators...

    Well, use this simple function:

    Code:
    char *cpy(char *target, char *source){
    
        char *start = target;
    
        while(*source)
            *target++ = *source++;
        *target = 0x00;
        return(start);
    }
    like this in your code:

    Code:
    int main (void)
    {
            int a;
    
            band matt;
    
            matt.age = 14;
    
            printf("%s", cpy(matt.instrument, "Something")); // see?
            getchar();
            return(0);
    }
    You could make a class "String" and overload the '+' operator, but I can see that its beyond your current level...

    Ps: what you're doing is much more C than C++

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    That example is pure C. I would not recommend you post it at the C++ board.
    A (better?) approach is to simply use std::string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    alright so i have almost no idea what you guys are talking about. I've been learning off the tutorials on this site but it looks as though they haven't included a lot of stuff. how would you guys recommend I go about learning c++ because it seems like the tutorials on here aren't cutting it.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    I recommend learning with the book Accelerated C++ by Koenig and Moo.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making an address book in C++ with arrays inside of structures?
    By boblablabla in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2009, 11:18 PM
  2. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21