lundi 2 décembre 2019

Program skips a do while loop, also how do I check the contents of a string in an if statement?

I was practicing coding having not done it in a while, and for some reason my program was skipping a do while loop, even though to my understanding do while loops are supposed to run through at least once before checking if they should keep going.

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int num1;
int num2;
string function = " ";

int main()
{
    cout << "Enter num1: ";
    cin >> num1;

    do
    {
        cout << "Enter a function ('P' 'M' 'T' 'D'):";
        getline(cin, function);

        if (function == "P" || "M" || "T" || "D")
        {
            break;
        }
        else
        {
            cout << function << " IS AN INVALID FUNCTION" << endl;
        }
    } while (function != "P" || "M" || "T" || "D");

    cout << "/nEnter num2: ";
    cin >> num2;

    //cout << "\nnum1 is " << num1 << ", num2 is " << num2 << endl;

    system("pause");

    return 0;
}

From what I can tell, the getline() is completely skipped, as it does not prompt me to input the value of my string. Incidently, is the if statement where I check the value of the string correct?

Aucun commentaire:

Enregistrer un commentaire