Thread: VERY simple mistake

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    100

    VERY simple mistake

    Code:
    //Write a program that calculates the amount of days
    //from day "X" to day "Y"
    
    #include <iostream>
    
    using namespace std;
    
    int Day_X; //first day to be compared to
    int Day_Y; //second day for the amount of days in between
    int Month_X;
    int Month_Y;
    char January[31];
    char February[28];
    char March[31];
    char April[30];
    char May[31];
    char June[30];
    char July[31];
    char August[31];
    char September[30];
    char October[31];
    char November[30];
    char December[31];
    
    int main() {
    cout << "DAY CALCULATING PROGRAM" << endl;
        cin.get();
    cout << "Please give the Month of the starting day" << endl;
      cin >> Month_X;
      cin.get();
    cout << "Please give the Day of the month" << endl;
        cin.get();
          cin >> Day_X;
    cout << "Please give the Month of the ending day" << endl;
       
          cin >> Month_Y;
    cout << "Please give the Day of the month" << endl;
         cin >> Day_Y;
         cout << "The amount of days between " << endl; 
         cout << Day_X << Month_X << endl;
         cout << "AND" << endl;
         cout << Day_Y << Month_Y << endl;
         
        
        
        cin.get(); //perhaps implement a "Yes, No" option
        return 1; //later make it return a value 
    }
    Okay so when I run this thing, it allows me to give input all the way up until it asks for Month_X. It just terminates the whole function/program. WHY IS THIS DOING THAT? I'm probably missing something so minute. Anyway, is the setup for my program okay? I'm doing a program in which calculates the amount of days from ...well just read the program.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    cin leaves the '\n' in the input stream, so every other cin gets stuck on it. every time you use cin, follow it up with cin.ignore(1);
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    17
    Code:
    cout << "Please give the Day of the month" << endl;
        cin.get();
          cin >> Day_X;
    change it around

    Code:
    cout << "Please give the Day of the month" << endl;
      cin >> Day_X;
       cin.get();

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    I think the correct way to do it would be
    Code:
    cout << "Please give the Day of the month" << endl;
    cin >> Day_X;
    cin.ignore();
    cin.ignore() should 'ignore' the last character input

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You don't have to use a cin.get() or ignore() when you are using all cin>> but if you start mixing with cin.get() or cin.getline() that you have to ignore() so your gets won't read the '\n'.
    Woop?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
        return 1; //later make it return a value
    Your programs should return 0 if they executed normally, and 1 only if there was an error.
    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.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Incidentally, you may be interested in some algorithms that work with dates (i.e. calculating difference in days, taking leap years properly into account): http://library.lanl.gov/numerical/bookcpdf/c1-1.pdf
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  2. what SHOULD be a painfully simple API call...
    By Citizen Bleys in forum Windows Programming
    Replies: 3
    Last Post: 09-17-2003, 03:20 PM
  3. Simple simple graphics
    By triplem in forum C Programming
    Replies: 2
    Last Post: 05-19-2003, 02:52 AM
  4. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM