2

See code below. It works fine for numbers but if I enter a letter (eg 'a'), it will output 'first number 0 is divisible by second number, 6487956' without asking for input of second number.

#include<iostream>
using namespace std;

int main()
{
    int first, second;

    cout << "Please enter your first number (must be an integer) : "  ;
    cin >> first  ;
    cout << "Please enter your second number (must be an integer) : ";
    cin >> second ;

    if (first % second == 0)
        cout << "The first number, " << first << ", is divisible by the second number, "     << second << ".";
        else
        cout << "The first number, " << first << ", is not divisible by the second number, " << second << ".";
return 0;
}
LWeaver
  • 45
  • 4
  • You cannot limit C++ input to integers. All you can do is read a string, and check if the string is an integer and convert it if it is. – john Oct 03 '20 at 18:35
  • Check `cin` state after reading. It will fail if the user did not input any numbers. – Quimby Oct 03 '20 at 18:36
  • @Quimby That is true, but it will not fail if the user entered a number followed by some non-number, `123abc` for instance. I don't think that input counts as an integer, but it would read as one. – john Oct 03 '20 at 18:37
  • @john You are right but that is the behaviour of cin. If the author needs the full line to be a number, use `getline`. – Quimby Oct 03 '20 at 18:39
  • 1
    @Quimby Yes, which is what my comment was essentially saying. Unfortunately the partial solution of checking the `cin` state gets mentioned too often, and I imagine many newbies implement it without realising it doesn't really do what they wanted. – john Oct 03 '20 at 18:41
  • @john Fair enough. – Quimby Oct 03 '20 at 18:41
  • 1
    I use: `int get_int(istream& in) { string line; if (!getline(in, line)) throw runtime_error("istream"); stringstream ss{line}; int result; if (!(ss >> result)) throw runtime_error("bad input"); return result; }` then I can use `int first = get_int(cin);` – Eljay Oct 03 '20 at 18:47
  • @Eljay would that go after 'int main' and would I need to include any '{' anywhere? Would you possibly be able to put your comment as an answer so I can see how to lay it out? Many thanks :) – LWeaver Oct 03 '20 at 18:56
  • I can't add it as an answer, because the question is closed. That would be a freestanding function, which could appear before main. The `int first = get_int(cin);` would be in the body of main. – Eljay Oct 03 '20 at 18:58

1 Answers1

0

Here is an example which will not accept if input has any characters. Function to check if string has only numbers.

#include <iostream>
#include <string>
#include <algorithm>

bool is_digits(const std::string& str)
{
    return std::all_of(str.begin(), str.end(), std::isdigit);
}

void get_number(std::string& number, const char* string) {
    std::cout << string;
    std::getline(std::cin, number);
    if (!is_digits(number)) {
        get_number(number, string);
    }
}

int main()
{
    std::string first, second;
    get_number(first, "Please enter your first number (must be an integer) : ");
    get_number(second, "Please enter your second number (must be an integer) : ");

    if (std::stoi(first) % std::stoi(second) == 0)
        std::cout << "The first number, " << first << ", is divisible by the second number, " << second << ".";
    else
        std::cout << "The first number, " << first << ", is not divisible by the second number, " << second << ".";
    return 0;
}

or

#include <iostream>
#include <string>
#include <algorithm>

bool is_digits(const std::string& str)
{
    return std::all_of(str.begin(), str.end(), std::isdigit);
}

int get_number(const char* string) {
    static std::string number;
    std::cout << string;
    std::getline(std::cin, number);
    if (!is_digits(number)) {
        get_number(string);
    }
    return std::stoi(number);
}

int main()
{
    int first = get_number("Please enter your first number (must be an integer) : ");
    int second = get_number("Please enter your second number (must be an integer) : ");

    if (first % second == 0)
        std::cout << "The first number, " << first << ", is divisible by the second number, " << second << ".";
    else
        std::cout << "The first number, " << first << ", is not divisible by the second number, " << second << ".";
    return 0;
}
pesuww
  • 105
  • 11