Thread: Problem with setting a character array in a class member.

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    13

    Question Problem with setting a character array in a class member.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <stdio.h>
    #include <iostream.h>
    #define MAX 10
    
    class inputOutputList
    {
          public:
                  char listFileName;
                  char carInputNames[10][50]; //Create ten-car entries; Used for searching text documents
                  char carOutputNames[10][50];//" " "; Used for outputting the correct name to corresponding input name list item.
          
                  void SetCarInputName(char Input[50], int entryNumber)
                  {
                         carInputNames[entryNumber][0] = *Input;   //Leaving it like this, it only processes that one element in the array so using the string "Buick", it just stores it as "B". Removing the [0] to try to copy the whole string over reports "incompatible types in assignment of `char' to `char[50]'     
                  }
                  void SetCarOutputName(char Output[50], int entryNumber)
                  {
                         carOutputNames[entryNumber][0] = *Output;       
                  }     
    };
    
    inputOutputList theMainCarList;
    
    
    //Prototypes are above
    /////////////////////////////////////////////////////
    //MAIN PROGRAM below
    /////////////////////////////////////////////////////
    int main()
    {
        FILE *f;
        char input[10000];
        char output[10000];
        char temp_char[1];
        
        int outputIndex = 0;
        int dummy = 0;
        cin>>dummy;
    
        f=fopen("inline.txt","r");
        if (!f)
            return 1;
        fgets(input,10000,f);
    
    
        theMainCarList.SetCarInputName("Buick", 1);
        cout<<theMainCarList.carInputNames[1]<<endl;
        cin>>dummy; //Ask for input to "halt" the program.
        
        
        
    
        system("pause"); //Ineffectual.
        fclose(f);
        return 0;
        
        
    }
    Hello. My problem is highlighted in red. Running the program as it is now, only a "B" is shown which tells me the assignment in the class's function is only assigning the first element in the array the letter. Removing the "[0]" to attempt copying the whole string instead of that specific element reports the error "incompatible types in assignment of `char' to `char[50]' ". I'm a noob to manipulating characters/strings in classes. ***_ _ _ ***

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    To copy C style string, you'd use strcpy

    Code:
    strcpy( carInputNames[entryNumber], Input );
    Using C++'s std::string class makes things a little more intuitive though. You'd do something like:

    Code:
                  string carInputNames[10];
          
                  void SetCarInputName(string Input /* const string & input for correctness*/, int entryNumber)
                  {
                         carInputNames[entryNumber] = Input;
                  }
    There are many benefits to using this type, some of which you can find using a simple google query. http://www.google.com/search?q=c%2B%2B+string And unless your assignment requires use of C strings, then this is highly advised.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    13
    Gracias Seņor. The first example worked like a charm; The string class sput out tons of seemingly unrelated errors(And, yeah, I included the C++ string header file using #include <string>). I'm using Bloodshed Dev-C++ which I think uses the Ming compiler; Maybe that has something to do with it.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > I'm using Bloodshed Dev-C++ which I think uses the Ming compiler; Maybe that has something to do with it.

    Not really. Can you show us the errors you get when trying to use the string class?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > Maybe that has something to do with it
    No. Just as char *s == t compares a string's location and not lexicographically (probably not even a word) you notice that this

    carInputNames[entryNumber][0] = *Input;

    will only copy one char because that is exactly what it does. Input, loosely speaking, mearly points to one place (a char) at all times. So does foo[r][c], foo[r], and foo. To copy correctly you would need to use a loop, like strcpy provides.
    Last edited by whiteflags; 12-15-2006 at 02:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Base(Abstract) Class Problem
    By Aramil in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2008, 02:58 PM
  2. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM