Thread: Array problem

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    Array problem

    Ok I want to make an array to hold a name, char name2[12]; or int name[12] should have made me an array but when I type name[1]=3; for example, it comes up with alot of errors, should I use a string? I tried the same things with a string but the same errors came up.
    Code:
    #include <iostream.h>
    struct database
    
    {
    
    int id_number;
    
    int age;
    
    float salary;
    
    int name[12];
    
    char name2[12];
    
    };
    
    int main()
    
    {
    
    database employee;
    
    employee.age=22;
    
    employee.id_number=19;
    
    employee.salary=12000;
    
    cout<<"ID NUMBER :"<<employee.id_number<<endl;
    cout<<"Age :"<<employee.age<<endl;
    cout<<"salary earned :"<<employee.salary<<endl;
    
    return 0;
    
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You can use the strcpy function to copy a string into a character array:
    Code:
    #include <string.h>
    strcpy(employee.name2, "Monster");
    Or just use a string:
    Code:
    #include <string>
    string name2; 
    name2 = "Monster";

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    18
    It doesn;t work! Sorry but your code seems to bring up the same errors.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <iostream>
    #include <string>
    
    using std::string;
    using std::cout;
    using std::endl;
    
    struct database{
    
    int id_number;
    int age;
    float salary;
    string name;
    string name2;
    
    };
    
    int main()
    
    {
    
    	database employee;
    
    	employee.age=22;
    	employee.id_number=19;
    	employee.salary=12000;
    	employee.name = "Fordy";
    	employee.name2 = "Fordy2";
    
    	cout << "ID NUMBER :" << employee.id_number << endl;
    	cout << "Age :" << employee.age << endl;
    	cout << "salary earned :" << employee.salary << endl;
    	cout << "name1 :" << employee.name << endl;
    	cout << "name2 :" << employee.name2 << endl;
    
    	return 0;
    
    }

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    18
    It works but I don;t know why, when typing in it doesn;t compile but copy and paste works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM