mardi 6 octobre 2015

Checking for Char/Int

Alright so I am doing an assignment and am getting frustrated. The assignment wants me to ask the user for a number then say if the number is even or odd, but if the user types "done" the program will exit.

So my question is how do you check the input for character/int at the same time, then decide which it is.

// ConsoleApplication2.cpp : Defines the entry point for the console application.
//    
#include "stdafx.h"
#include <iostream>
#include <string>
bool isEven(int userAnswer);
using namespace std;
int userAnswer;

int main()
{
    cout << "Which number would you like to check?" << endl;
    cin >> userAnswer; 

    if (isEven(userAnswer) == false)
    {
        cout << userAnswer << " is a odd number." << endl;
    }   
    else if (isEven(userAnswer) == true)
    {
        cout << userAnswer << " is a even number." << endl;
    }

    cin.get();
    cin.get();

    return 0;
}

bool isEven(int userAnswer)
{
    if (userAnswer % 2 == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Aucun commentaire:

Enregistrer un commentaire