#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    10
    Rep Power
    0

    Problem With getline.cin


    I have been writing a simple calender program to test out my knowledge of C++ so far. If you could compile the code below, you'll see the problem that I'm having. It's very, very strange and anyone who would correspond would be welcome.

    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;

    enum evenodd { EVEN, ODD };
    int numofdays;

    int EvenOrOdd(int mselect);
    int IsItFeb(int mselect);

    int main()
    {
    // Variables and such
    char months[12] [20] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    int mselect;
    int i = 1;
    int d;
    string tempbuff = "Hi";
    string jan[31];

    // First Menu
    for(;;)
    {
    cout << "Enter a month to work with (1-12): ";
    cin >> mselect;
    cout << endl << endl;
    if(mselect>0 && mselect<=12)
    break;
    }

    cout << setw(25) << months[mselect-1] << endl << endl;

    switch(EvenOrOdd(mselect)) //
    { //
    case EVEN: numofdays = 31; //
    break; // Find number of days in month and if it's Feb
    case ODD: numofdays = 30; //
    break; //
    } //
    IsItFeb(mselect); //

    for(int j=0; j<5; j++) //
    { //
    for(int k=0; k<7; k++, i++) //
    { //
    if(i<=numofdays) //
    { // Display the month
    cout << setw(6) << i; //
    } //
    } //
    cout << endl << endl; //
    } //

    cout << endl << "Select a day on the calender to work with: ";
    cin >> d;

    if(d<1 || d>numofdays)
    {
    return 0;
    }
    else
    {
    cout << endl << endl << setw(25) << months[mselect-1] << " " << d << " " << endl;


    // cout << "Enter a string (now it should work): " << flush;
    // getline(cin, s);
    // cout << "Good! s = '" << s << "'" << endl;


    cout << "You may now enter in a note (Less than 100 characters): ";
    getline(cin, tempbuff);
    cout << "tempbuff = '" << tempbuff << "'" << endl;
    cout << "Enter a string (now it should work): " << endl;
    getline(cin, tempbuff);
    cout << "Good! tempbuff = '" << tempbuff << "'" << endl;
    }

    return 0;
    }

    int EvenOrOdd(int mselect)
    {
    if(mselect%2)
    {
    return ODD;
    }
    else
    {
    return EVEN;
    }
    }

    int IsItFeb(int mselect)
    {
    if(mselect==2)
    {
    numofdays=28;
    return numofdays;
    }
    else
    {
    return mselect;
    }
    }
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    I encountered no problems with the code compiled on linux. What is the problem that you are having?
  4. #3
  5. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Just some thoughts to consider:

    Code:
    int EvenOrOdd(int mselect) 
    { 
        if(mselect%2) 
        { 
            return ODD; 
        } 
        else 
        { 
            return EVEN; 
        } 
    }
    Have a think about the function above. Basically, you pass it an integer and you want to know whether the number is odd or even. There are only two possible outcomes, the number is odd, or it's even. How about revising the function to simply return whether the number is odd? Either it is, or it isn't. If it's not, you know it's even. At the same time, instead of naming the paramater passed to the function 'mselect', which is very specific to this particular use of the function, rename it to be more generic. For example:

    Code:
    bool isOdd(int value) 
    { 
        return (value%2);
    }
    Now you have a more generally useful function.

    The following function could also use some rework.

    Code:
    int IsItFeb(int mselect) 
    { 
        if(mselect==2) 
        {  
            numofdays=28; 
            return numofdays; 
        } 
        else 
        { 
            return mselect;
        } 
    }
    The name of the function, IsItFeb, suggest that the return value depends on the value being passed to the function representing February. Oddly, if the month is February the function returns the number of days in a non-leap year February, otherwise it returns the month number. This is confusing.

    If the function is expected to return a value indicating whether the month is February or not, then it needs to be rewritten to do just that. If the function is to perform some other function then it should be renamed to reflect the actual purpose. Assuming the former we can rewrite the function as follows:

    Code:
    bool isFebruary(int month)
    {
        return (month==2);
    }
    Note that the use of mselect as a variable name in your program is itself a little confusing. It represents a month, so month is the most obvious name for it, or some other name reflecting that it's the month input by the user.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    10
    Rep Power
    0
    Sorry for the really long delay of a response. Thanks for your tips Bob, they are appreciated.
    My problem is I want people to enter a string, store it in a slot in a string array, and then display the string by referencing the slot in the string array that it was stored in. Right now, when you compile the code, tempbuff is blank. This problem is really odd and it has really delayed a cpp newb like myself. If anyone could help, it would be appreciated.
  8. #5
  9. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Try modifying your code as follows:

    Code:
    cout << endl << "Select a day on the calender to work with: "; 
    cin >> d; 
    cin.ignore();  // <--- add this line
    The following link might be useful for you:
    http://www.parashift.com/c++-faq-lit....html#faq-15.6
    Last edited by BigBadBob; August 25th, 2003 at 06:53 AM.

IMN logo majestic logo threadwatch logo seochat tools logo