Thread: ignoring '/' when inputed by user

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    38

    ignoring '/' when inputed by user

    I'm writing code that involves people putting in dates... They must use month/day format (2 digits each... 02/01)... My output is suppose to determine what Zodiac sign they fall under.

    I know I'm doing it the long way with if/else if statements, but I don't know another way.

    My problem is that the user MUST according to the assignment enter the '/' between numbers. my program wants to divide... I've tried to used
    Code:
    cin.ignore(/)
    , but that doesn't seem work... I need to be able to grab the first 2 digits and last 2 into a variable. Is
    Code:
    cin.get
    the answer? If so, what do I put in paren to grab those numbers?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    One way is to use streams:
    Code:
    int Month;
    int Day;
    char Dummy;
    std::cin >> Month >> Dummy >> Day;
    The / will be read into the dummy variable.
    This method will NOT make any difference on 2 and 02 though. If this is important you could try to read 5 characters, 2 for month, 1 dummy, 2 for day.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you used ignore, it would be cin.ignore(), not cin.ignore(/). You just have to assume that the character that is being ignored is '/'.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    Quote Originally Posted by Magos
    One way is to use streams:
    Code:
    int Month;
    int Day;
    char Dummy;
    std::cin >> Month >> Dummy >> Day;
    The / will be read into the dummy variable.
    This method will NOT make any difference on 2 and 02 though. If this is important you could try to read 5 characters, 2 for month, 1 dummy, 2 for day.
    Can I limit the space that each takes up like?

    cin>>Month[2]>>Dummy[1]>>Day[2];

    Or would I limit it within int

    int Month[2];
    char Dummy[1];
    int Day[2];

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    char Month[2];
    char Day[2];
    char Dummy;
    
    std::cin >> Month[0] >> Month[1] >> Dummy >> Day[0] >> Day[1];
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    This isn't doing it... I was trying to get it to display... make sure that it's reading what's being inputed and I get wierd character in between the numbers

    Code:
    cin >> Month[0] >> Month[1] >> Dummy >> Day[0] >> Day[1];
    cout<<Month<<" "<<Day;
    I was trying to display the numbers back to myself to verify they went in correctly... i'll try it with some math and see if they work.


    edit: didn't capitalize
    Last edited by liquidcourage1; 03-02-2006 at 08:44 PM.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Month and day is an array of characters, NOT an integer. Trying to print it like you do is dangerous since the compiler thinks it's a string, but it has no terminating NULL character.

    What exactly are you trying to do, if you need the month/day as a whole number I still recomend my initial suggestion.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    Here's the exact program from the assignment:

    Write a complete program that tells zodiac signs. Ask the user for their birth month and day. Let the user that thye MUST enter the information as follows:
    numeric month/numeric date. For example: 4/13

    Given that information, tell the user which zodiac sign they are. Here's a list:

    * Aries - March 21 - April 20
    * Taurus - April 21 - May 21
    * Gemini - May 22 - June 21
    * Cancer - June 22 - July 22
    * Leo - July 23 -August 21
    * Virgo - August 22 - September 23
    * Libra - September 24 - October 23
    * Scorpio - October 24 - November 22
    * Sagittarius - November 23 - December 22
    * Capricorn - December 23 - January 20
    * Aquarius - January 21 - February 19
    * Pisces - February 20- March 20
    I think I need to use logic statements so I can define month/day and where they fall... like if month = x and day is < y then you are a capricorn, etc.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    Here's what I'm working with now... Lots of <b>if</b> statements to do it though...
    Code:
    //This program will help determine zodiac sign
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int month,day;
    
    	cout<<"Please enter your Birth month (2 digit form)\n";
    	cout<<"Example:  February = 02\n\n";
    	cin>>month;
    	cout<<"\n\nPlease enter the Day of your birth (2 digit form)\n";
    	cin>>day;
    	if (month==01 && day<=20)
    		cout<<"Capricorn\n\n";
    	if (month==01 && day>20)
    		cout<<"Aquarius\n\n";
    	if (month==02 && day <=19)
    		cout<<"Aquarius\n\n";
    	
    return 0;
    }

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'd use some kind of structure for easy lookup of the sign. Are you allowed to use structs/STL? Perhaps your latest lecture may hint what to use to solve it?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    Quote Originally Posted by Magos
    I'd use some kind of structure for easy lookup of the sign. Are you allowed to use structs/STL? Perhaps your latest lecture may hint what to use to solve it?
    No lecture hints unfortunately. This is an online class. I have it almost built until I come into the 8th, 9th month. Says:

    illegal digit '8' for base '8'
    Error for 8 and 9... I don't know how to fix that.

  12. #12
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The leading 0 (08) means you're typing in base 8 rather than base 10. Remove the 0 and it'll be fine.

    Footnote: Prefix with 0x to use base 16.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    nevermind... i deleted the zero's from 08 to 8 and it works.

    Thanks

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    ha... you typed that when I did... lol

    Just so I know, though... why does it only do that with 8 and 9?
    Last edited by liquidcourage1; 03-02-2006 at 09:15 PM.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The leading 0 means that the number is base 8 (octal). Only digits 0-7 are valid in octal, like digits 0-9 are valid in decimal and 0 and 1 are valid in binary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to see if a user inputed a string
    By herocks in forum C++ Programming
    Replies: 9
    Last Post: 01-23-2007, 02:31 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Replies: 4
    Last Post: 04-21-2004, 04:18 PM
  4. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM
  5. Stopping a user from typeing.
    By knave in forum C++ Programming
    Replies: 4
    Last Post: 09-10-2001, 12:21 PM