-2

when user inputs 'y' or 'Y' for "Do you wish to enter another year? (Y/N): ", loops "the month has 0 days" forever. Why? I tried to look at the values and it seems like the values stored are being used again. Maybe I am not using cin.clear() correctly?

//variables
bool ucontinue = true; //answer to continue
int year = 0;
int month = 0;
int days = 0;
char answer = 'a';

//loop
while (ucontinue == true)
{
    /*
    Enter a year (Must be a positive integer): 2016
    Enter a month (Must be a between 1 and 12): 2
    The month has 29 days.
    Do you wish to enter another year? (Y/N): y
    */


    //year input
    while (year <= 0)
    {
        cout << "Enter a year (Must be a positive integer): ";
        cin >> year;
    }

    //month input
    while (month <= 0)
    {
        cout << "Enter a month (Must be a between 1 and 12):";
        cin >> month;
    }

    //# of days in the month 
    cout << "The month has " << days << " days." << endl << endl;

    //continue?
    while (answer != toupper('y') && answer != toupper('n'))
    {
        cout << "Do you wish to enter another year? (Y/N): ";
        cin >> answer;
        answer = toupper(answer);

        if (answer == toupper('n'))
        {
            ucontinue = false;
        }

    }
        cin.clear();

}
Kimmie
  • 1
  • 3
    Your code does not change the `days` value. You need to change it according to the input of `month` and `year`. You may need a function to check 'leap year' also. – Shadi May 14 '17 at 15:55
  • Yes, I have the psuedocode for the days, but I am just trying to make sure the other while loops are working fine. Thanks! – Kimmie May 15 '17 at 19:32
  • The class hasn't gone over do-while, so I am avoiding putting it in my code. I thought the cin.ignore() would clear all user input -- is there a reason why it doesn't? – Kimmie May 15 '17 at 19:39

3 Answers3

1

you code loops forever because of you while loops the the first time you run the program it works fine but the second time it goes around all the values are set for example the second time you go through the loop this while statement

while (year <= 0)
{
    cout << "Enter a year (Must be a positive integer): ";
    cin >> year;
}

won't run because year is already greater than 0 and this happens for all the while statements in your code. what would work is if you have do while statements instead of while statements because do while statements will run through the loop once before testing the condition it. like this:

do
{
    cout << "Do you wish to enter another year? (Y/N): ";
    cin >> answer;
    answer = toupper(answer);


    if (answer == toupper('n'))
    {
        ucontinue = false;
    }

}while(answer != 'Y' && answer != 'N');
Manvir
  • 769
  • 1
  • 7
  • 15
  • Thank you. I just decided to move all variables except ucontinue into the outer while loop. I was also trying to stay away from do-while loops, because the class hadn't gone over do-while yet. I was wondering why the cin.ignore() does not clear user input? – Kimmie May 15 '17 at 19:35
  • I think you misunderstood cin.ignore() [link](https://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input) – Manvir May 15 '17 at 19:55
  • if by clear user input do you mean it should clear the values inside `year, month, answer` that's not possible but you can set `year = 0, month = 0, answer = 'a'` at the begin of the first loop that should also solve the problem – Manvir May 15 '17 at 19:59
0

If I understand correctly you do the following:

Enter a year (Must be a positive integer): 2017
Enter a month (Must be a between 1 and 12):5
The month has 0 days.

Do you wish to enter another year? (Y/N): y <ENTER>

and your program loops. What happens it the following:

cin.clear();

is executed and your loop starts again. At this point year and month are still set. So when in the new loop iteration the line

while (year <= 0)

is encountered the condition is false and the loop continues to

while (month <= 0)

for this line the same is true. After that

cout << "The month has " << days << " days." << endl << endl;

is printed and the condition in

while (answer != toupper('y') && answer != toupper('n'))

is checked. As I have just entered y this condition is not true and

cin.clear();

is executed next, after this the loop restarts ad infinitum.

Bob Jansen
  • 1,215
  • 2
  • 12
  • 31
  • So: move all variables except `ucontinue` into the outer loop. That fixes the looping issues, leaving only the days computation to be added. And as suggested elsewhere: toupper on a constant is not useful - test the uppercase constant instead. – Zastai May 14 '17 at 17:04
  • Also, month can be 0 or more than 12. – Bob Jansen May 14 '17 at 17:17
  • He or she needs do while statements instead of while statements this will also fix it. Right? – Manvir May 14 '17 at 17:20
  • Yes, agreed the `do-while` in your answer is better than the `while` in the question. – Bob Jansen May 14 '17 at 17:22
  • Now I understand your confusion: `cin.clear()` clears the input in the command window not values that have been assigned to your variables. It's less powerful than you have been led to believe. – Bob Jansen May 15 '17 at 19:42
  • The class hasn't gone over do-while, so I am avoiding putting it in my code. I moved all variables except ucontinue into the outer while loop. However, I thought the cin.clear() would clear all user input -- is there a reason why it doesn't? – – Kimmie May 15 '17 at 19:57
0

Your programs have many problems

  1. you never updated or input something on the days variable so it will always be 0 as you set it in the beginning of the program

  2. if (answer == toupper('n')) can be reduced to if (answer=='N')

abaghel
  • 14,783
  • 2
  • 50
  • 66