Thread: Variable types?

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    10

    Variable types?

    Is it possible to store variables of more than one character? Because on the tutorial it says only one...I've been trying to figure it out but can't.

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not sure what you mean, but perhaps you are looking to use arrays (and the standard containers)?
    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

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or std::strings?

    Reading both char[] arrays and std::strings from the user is covered in this FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    Sorry, I wasn't that clear. What I mean is, there are int, float and char which store variables. I wish to store more than one character so I can't use any of them. Is it possible to do that?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What I mean is, there are int, float and char which store variables. I wish to store more than one character so I can't use any of them. Is it possible to do that?
    Yes, by using (statically or dynamically allocated) arrays, structs, std::string, std::vector, other standard containers, your own containers, etc.

    What problem are you trying to solve?
    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

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    Not really a problem I was just testing things. Thanks for the help. I'll try it now.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In most cases if you want to store multiple characters you are looking for a string. Use the C++ string class as a variable type just like int, double and char. A string literal is enclosed in double quotes (rather than single quotes like a character literal). So rather than:
    Code:
    char c = 'x';
    you can do:
    Code:
    std::string s = "hello";
    Make sure you #include <string> and otherwise it works pretty close to the same as the other types.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    It sounds to me like he's asking if C++ has some sort of Variant type like they have in Visual Basic. Is that right? If that's the case, then there aren't any good ways to do it (why would you want to anyways?). The closest thing I can think of is a union.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds to me like he's asking if C++ has some sort of Variant type like they have in Visual Basic. Is that right? If that's the case, then there aren't any good ways to do it (why would you want to anyways?).
    Checked out VB's Variant, and it seems to be similiar to boost::any.
    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

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    More like boost::variant.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. newb question~
    By Anima in forum C++ Programming
    Replies: 10
    Last Post: 08-24-2003, 05:54 PM
  4. storing different variable types in arrays.........
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-28-2002, 11:45 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM