3 struct variables excluding '/'

This is a discussion on 3 struct variables excluding '/' within the C++ Programming forums, part of the General Programming Boards category; im following robert lafores 'OOP programming in c++' and im having trouble with an exercise (no its not for school). ...

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    Unhappy 3 struct variables excluding '/'

    im following robert lafores 'OOP programming in c++' and im having trouble with an exercise (no its not for school).

    its asking for the user to enter a date in "mm/dd/yy" format and for the struct variables to be of type int. i need to store the month in date1.month, day in date1.day, and year in date1.year.

    ive tried using getche() but all my efforts leave me a little frustrated. how do i ignore the '/' yet store the 2 digits before that in one variable? im not asking someone to do this exercise for me, but a push in the right direction would be appreciated. thanks in advance.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    If you can count on the user entering the data in that exact format and not have to worry about validating that it is the correct format, then it should be easy enough. Just have a dummy char variable that you read into and ignore using cin.
    Code:
    char c;
    cout << "Enter a date MM/DD/YY format: ";
    cin >> date1.month >> c >> date1.day >> c >> date1.year;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    this is where i feel like an idiot. thanks..that worked. i always try to make a problem harder than it is. i saw the dummy char scenario somewhere in this book but couldnt find it and didn't know how to implement. thanks. =)

    but how does c++ know what to assign the variables to? it seems like no matter how many numbers...it knows the '/' is a dummy char.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by xion
    but how does c++ know what to assign the variables to? it seems like no matter how many numbers...it knows the '/' is a dummy char.
    Code:
    cin >> date1.month >> c >> date1.day >> c >> date1.year;
    cin is smart enough to know what to do based on the type of variables you are trying to extract from the input stream. In this case, we are telling cin to expect an integer, a character, another integer, another character, and finally another integer. So, when cin is extracting info off the input stream into the first int it will stop once it reaches the first '/' character because it doesn't make sense to try to convert and store that character along with the other numbers we have already read. Then it extracts a single character from the input stream, the aforementioned '/' character, because we told it to read one character. And the process repeats...

    The program doesn't know that the '/' is a dummy character, it just knows that it is a simple character that it needs to read and store into a certain location. As the programmer, if you are doing nothing with that value in the program, that is what makes it a dummy variable.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Replies: 4
    Last Post: 12-12-2002, 01:32 PM

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