Thread: string inside of a structure?

  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,318
    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
    Location
    Inside my computer
    Posts
    24,654
    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
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    I recommend learning with the book Accelerated C++ by Koenig and Moo.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 10:18 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02: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, 10:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM