Thread: I can't find the answer.

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    I can't find the answer.

    I went to google and I even searched the message board and I can't find out how to make class's using character variables.
    If you don't know what I mean I will give an example
    Code:
    class Machine1 
    {
    public:
         char MachineType('triple vend', '2 inch capsule'):
    private:
         char Mac1Type;
    }
    I don't know if that will even work. Will I have to use string variables, using #include <cstring>? I just want the member functions to use characters instead of intergers.
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    1479
    Join Date
    Aug 2003
    Posts
    253
    Well this is what I have came across so far.

    Code:
    #include <string>
    
        class Person
        {
            string 
                d_name,             // name of person
                d_address,          // address field
                d_phone;            // telephone number
            unsigned
                d_weight;           // the weight in kg.
    Would this be the correct way of starting to do this? Also how would I set this up in int main()? Would I do something like this?
    Code:
    cout<<"What is the employee's name?" <<endl;
    cin >>d_name;
    Any help would be most appreciative.
    Knowledge is power and I want it all

    -0RealityFusion0-

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Yeah, you have to use a string... Either a C-style string, which is an array of "char's", Or, a C++ string-class style string.

    You can use the single-quotes ONLY for a single character. The char type variable only holds a single value (often one byte... and at least one byte, depending on the OS). If you make something equal to 'A' ( int x = 'A' ; ), you are making that variable equal to the number which converts to an A, per the ASCII code.

    Take a look at the String Tutorial and maybe the Array Tutorial.

    Strings can get confusing because most functions work with pointers to the string... If you are comparing strings, the function will test "Is the string pointed-to by this pointer equal to the string pointed-to that pointer.
    Last edited by DougDbug; 08-13-2003 at 02:37 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    You can use an array of chars:

    Code:
    class Machine1 
    {
    public:
         char * MachineType(char word[], int length);
    private:
         char Mac1Type;
    }
    
    int main()
    {
    Machine1 machine;
    
    machine.MachineType("triple vend", 11);
    
    return 0;
    }
    Would this be the correct way of starting to do this? Also how would I set this up in int main()?
    Using your class example,
    Code:
    Person person1;
    cout<<"What is the employee's name?" <<endl;
    cin >>person1.d_name; //if d_name is public
    Last edited by funkydude9; 08-13-2003 at 02:29 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  5. #5
    1479
    Join Date
    Aug 2003
    Posts
    253
    Using the array of char's how would I implement spaces in the program? This is what I got working so far.

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
    char TypeOFMachine[20];
    cout <<"What kind of machine is it?" <<endl;
    cin >>TypeOFMachine;
    cout <<TypeOFMachine;
    system("pause");
    return 0;
    }
    It works fine but only if I don't use spaces in the cin>> statment. It just cuts off the name at the first space character I would use.
    Knowledge is power and I want it all

    -0RealityFusion0-

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Code:
    cin.getline(TypeOFMachine, 20); //Inputs until the end of a line is detected
    
    for (int i = 0; i < 20; i++)
         cout<<TypeOFMachine[i];
    Last edited by funkydude9; 08-13-2003 at 02:33 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Replies: 3
    Last Post: 06-09-2006, 09:53 AM
  3. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. I have used the search, but cant find an answer
    By sayword in forum C Programming
    Replies: 11
    Last Post: 02-12-2003, 06:45 PM