Variable types?

This is a discussion on Variable types? within the C++ Programming forums, part of the General Programming Boards category; Is it possible to store variables of more than one character? Because on the tutorial it says only one...I've been ...

  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
    21,390
    Not sure what you mean, but perhaps you are looking to use arrays (and the standard containers)?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,046
    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
    21,390
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,317
    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
    21,390
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21