Thread: Int and Character in the same variable

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    Int and Character in the same variable

    Im pretty new to C++ and was wondering, how can you store both a integer and a character in the same variable. I was wondering this because I am making a program which could store all my passwords and such but I dont know, if there is, a variable to store both. If not any one have any advice how to store a password in I guess an array that contains both characters and integers.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no such thing. However, you can store it as a string and, if it turn out later that it is indeed a number, you can convert the string to number.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    I dont know much about strings. What exactly are they? They store like words right?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    std::string mystr = "My string";
    std::string mystr2 = "123";
    That should give you an idea of what a string is.
    It basically holds text or characters.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    But not both. Im sorry for asking so many questions but how would you convert it and how does that allow both characters and integers?

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    An array of int can be used to stored ascii characters. The extraction method has however to be coded.

    As a simple example you can do this:

    Code:
    char foo = 'a';
    int bar = foo;
    char baz = bar;
    
    std::cout << baz; // prints 'a'
    You can thus create an array of ints and code the extraction of those elements you need to use as characters.

    Meanwhile char is a integral numeric type. This means 'a' is actually stored as an integer. char size is described to be 8bits minimum. This means you can store numbers on it.

    signed char -128 to +127 (off the top of my head)
    unsigned char: 0 - 256
    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.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    So you kind of route the char foo which is = to a through a int so its a char variable in a int variable then route i back through another char variable so then how do you add int into.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'm sorry, you need a few commas or periods in there. I can't make what you are saying.

    But I planned just to show you how characters are actually stored. You can even cout << bar. That will still print 'a' because that, in short, how cout operates. It does an implicit conversion.

    The idea is then to show you you can make an array of char and store integers in it, or an array of int and store characters in it.
    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.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Sorry for the Grammar. Is there any ways to store them together or for like a password would you have to have:
    Code:
    int first_number = 1;
    char first_letter = a;
    int second_number = 2;
    char second_letter = b;
    
    cout<< first_number << first_letter << second_number << second_letter<<;

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    A string stores text. Numbers are part of text. This is fine:
    Code:
    std::string: alphanum = "1a2b";
    std::cout << alphanum << std::endl;
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Thank you very much.

  12. #12
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by A13W
    But not both. Im sorry for asking so many questions but how would you convert it and how does that allow both characters and integers?
    You need to understand this basic concept very well!

    string a = "6358";
    int b = "6358";


    a and b are two far different type of data:

    The string contains 4 "sub-variables": one for "6", one for "3", etc.

    The int contains the real value of "6358" instead.

    Maybe this link explains it better:
    http://www.cplusplus.com/doc/tutorial/variables.html

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by carlorfeo View Post
    You need to understand this basic concept very well!

    string a = "6358";
    int b = "6358";


    a and b are two far different type of data:

    The string contains 4 "sub-variables": one for "6", one for "3", etc.

    The int contains the real value of "6358" instead.

    Maybe this link explains it better:
    http://www.cplusplus.com/doc/tutorial/variables.html
    You are confusing the issue. The second line of that code is not what you intended.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by carlorfeo View Post
    string a = "6358";
    int b = "6358";

    [/url]
    You mean:
    Code:
    int b = 6358;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM