0

I use Visual Studio Community 2017 for C++ Programming. I have the following code. Here, the do while loop runs several times and doesn't stop for asking input where it is supposed to. But, in the last switch case program, if I enter 1 instead of n, the program works great. PLEASE HELP!!!

// Welcome2018.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<bitset>
using namespace std;

int main()
{
    string month[] = { "January", "February", "March", "April", "May", "June", "July", "August" "September", "October", "November", "December" };
    int m, d, answer;
    cout << "Welcome 2018!!!" << endl;
    do
    {
        cout << "Enter the number corresponding to the month you want displayed" << endl;
        cin >> m;
        switch (m)
        {
        case 1:
            cout << month[0] << endl;
            cout << "Enter the date to know the day it is/will be" << endl;
            cin >> d;
            if (d == 7 || d == 14 || d == 21 || d == 28)
            {
                cout << "The day on " << d << " January is Sunday!" << endl;
            }
            else if (d == 1 || d == 8 || d == 15 || d == 22 || d == 29)
            {
                cout << "The day on " << d << " January is Monday!" << endl;
            }
            else if (d == 2 || d == 9 || d == 16 || d == 23 || d == 30)
            {
                cout << "The day on " << d << " January is Tuesday!" << endl;
            }
            else if (d == 3 || d == 10 || d == 17 || d == 24 || d == 31)
            {
                cout << "The day on " << d << " January is Wednesday!" << endl;
            }
            else if (d == 4 || d == 11 || d == 18 || d == 25)
            {
                cout << "The day on " << d << " January is Thursday!" << endl;
            }
            else if (d == 5 || d == 12 || d == 19 || d == 26)
            {
                cout << "The day on " << d << " January is Friday!" << endl;
            }
            else if (d == 6 || d == 13 || d == 20 || d == 27)
            {
                cout << "The day on " << d << " January is Saturday!" << endl;
            }
        }
        cout << "Are you sure you want to quit?" << endl;
        cout << "Enter Y/N based on your choice:";
        cin >> answer;
        switch (answer)
        {
        case 1:
            answer = 1;

        case 'n':
            answer = 1;

        default:
            answer = 2;
        }
    } while (answer = 1);
    cout << "Thank You and Come Again!!!" << endl;
    return 0;
}
Anonymous
  • 1
  • 1
  • 4
    you have while(answer = 1), must be while(answer == 1) – Zharios Jan 03 '18 at 09:17
  • You need to show, verbatim, your input, your desired output, and your actual output. "Works great" or "is not working" are no valid status reports. – n. m. could be an AI Jan 03 '18 at 09:19
  • You can't ask for an 'n' if you are using cin with an int! – csabinho Jan 03 '18 at 09:21
  • If you actually type an `n` for `cin >> answer`, the stream will enter a failed state and not input anything else until the error is cleared. – Bo Persson Jan 03 '18 at 09:24
  • While not directly related to your problem, can I suggest a read of http://en.cppreference.com/w/cpp/chrono/c/time - which will do all this for you? – UKMonkey Jan 03 '18 at 10:26
  • @n.m. How can I show my desired output if I am not getting it. The problem itself is that I am not getting my desired output. – Anonymous Jan 03 '18 at 12:58
  • Your *desired* output is what you *want* to get. Your *actual* output is what you are getting instead (may be none, just say "no output" if that's the case). – n. m. could be an AI Jan 03 '18 at 13:42
  • Ok so what I am expecting is that I take an input from the user. If he enters 1, then January should be shown. Then, the user can enter the date and then according to the If statement, the day is displayed. The user is then asked whether he really wants to quit. In the end, if the user types n or N then the code shall run again, if anything else, the code exits successfully. This is what I want help for. – Anonymous Jan 03 '18 at 16:51

2 Answers2

2

A few issues with your code:

a. in the last while statement, you should use '==' operator which checks equality, instead of '=' operator which performs assignment.

while (answer == 1)

b. in the last switch case you should add a break command at the end of each case. Otherwise, it automatically performs default the code block under default option as well.

    switch (answer)
    {
        case 1:
            answer = 1;
            break;

        case 'n':
            answer = 1;
            break;

        default:
            answer = 2;
            break;
    }

c. The first switch-case block currently includes one case only. Therefore, the it is not really needed.

ibezito
  • 5,782
  • 2
  • 22
  • 46
-1

The reason for this behavior is that there will always be an 'n' in the keyboard buffer. Here's the solution for this!

(apart from the infinite loop that already was mentioned... )

I'm citing the answer I've linked:

The reason the program goes into an infinite loop is because std::cin's bad input flag is set due to the input failing. The thing to do is to clear that flag and discard the bad input from the input buffer.

//executes loop if the input fails (e.g., no characters were read)
while (std::cout << "Enter a number" && !(std::cin >> num)) {
    std::cin.clear(); //clear bad input flag
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //discard input
    std::cout << "Invalid input; please re-enter.\n";
}

See the C++ FAQ for this, and other examples, including adding a minimum and/or maximum into the condition.

Another way would be to get the input as a string and convert it to an integer with std::stoi or some other method that allows checking the conversion.

csabinho
  • 1,579
  • 1
  • 18
  • 28