Thread: int variables

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    6

    int variables

    Ok, I'm fairly new at this. I am trying to understand how variables work. So I am going by my book here and trying to write a simple birthday script.

    It asks for your birthday you type it in like say if your birthday was march 7 1986 you type 03071986. Then it will repeat your birthday. Its a very simple concept.

    Now, I got 2 questions.

    1: Is it possible to store these numbers in a char so it can write mm/dd/yyyy or do I HAVE to use int?

    2: This is where i'm lost. I am wondering how to make it so what the user typed is now the value of the variable. I figured it'd be something to do with the cin.getline() but i'm still unsure.

    Any help you could give would be much appreciated.

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    cin>>variable;

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    6
    Much thanx =D

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    1) Anything is possible (with the exception of 5 things, actually). But to be more helpful, yes, I'm sure you could that. I'd write you some code to explain how, but you'll obviously need to give a little more detail about what you want done. You want be able to store it in A SINGLE char, as a char wouldn't be anywhere near big enough. But there are other ways to format it.

    2) I'm not quite sure what you mean here. When you read in user input, like from a keyboard (as shown above by vasanth) you tell the compiler what variable to store the data in. That variable, unless something went really wrong, will be what the user typed.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by sean_mackrory

    ...

    When you read in user input, like from a keyboard (as shown above by vasanth) you tell the compiler what variable to store the data in. That variable, unless something went really wrong, will be what the user typed.
    That's not exactly true. Run the following, and enter 03071986 when prompted. (That's the example the original poster used.)

    If you really want to see what the user typed, use strings or chars or something literal.

    Now using this example, you could deduce what legal input that the user might have entered to get the int result, but why would you do this?

    Code:
    #include <iostream>
    using std::cin;
    using std::cout;
    int main()
    {
      int xxx;
      cout << "Enter your birthday in the form mmddyyyy: " ;
      cin >>xxx;
      cout << "You entered " << xxx;
      return 0;
    }
    Dave
    Last edited by Dave Evans; 08-19-2004 at 12:20 PM.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    6
    my code ended up looking like this:
    Code:
    #include <iostream>
    using namespace std;
    char bday[250];
    int main(int argc, char *argv[])
    
    
        {
        cout <<"What is your birthday? (dd/mm/yyyy)"<< endl;
        cin>>bday;
        cout <<"Your Birthday is:"<< endl;
        cout <<bday<< endl;
        system("PAUSE");	
        return 0;
    }
    I'm not too sure what the 250 is there for. I seen it like that in another source code where a char was defined. It seems to screw up without it.

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    char bday[250] allocates 250 characters for you. If you use char bday, you are only allocating one character, which is clearly not enough to hold the entire string.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    6
    oooooh, I get it. I have to declare how many characters I am using in that variable. So If I put bday[5], then I can only store 5 chars? If so, thats pretty straight forward.


    Much thanx.

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You've got it .
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    yes but be careful...when you do something like
    Code:
    char bday[5] = "1234";
    you are actually storing a null character ( \0 ) at the end of the string...and the null character takes up one char...so now bday is full
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    That is why string and getline exists
    Code:
    #include <string>
    ...
    std::string aString;
    std::getline(std::cin, aString);

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    6
    Unfortunately, such syntax doesnt come knocking on my door

    I'll write it down though lol

    much thanx

  13. #13
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb Got Books?

    ...then I can only store 5 chars? If so, thats pretty straight forward.
    RIGHT! A variable in C++ can only contain one value, i.e. one character. Each variable is associated with one memory address.


    Unfortunately, such syntax doesnt come knocking on my door
    OK. Some topics for you to look up:

    ARRAY - A series of values (numbers) stored sequentially in memory.

    POINTER - A variable which holds the memory address of another variable. For example it may point-to the first character (or another character) in a character array.

    STRING - A general programming term meaning "a string-of-characters". In C/C++, your bday[250] string is also called a character array, or a C-style string.

    C++ (but not C) includes the string class, as shown by Shakti. You can often use a string object as easily as a simple-single variable, but it is more complex "under the hood". Most books will introduce C-style strings first, and the string class later, after you've learned about classes and objects.
    Last edited by DougDbug; 08-19-2004 at 06:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM