0

I am new to this. And even more new to c++, here is my code, but I can not figure out how to create this loop which will continue executing programm and also will print out message if input will be incorrect. Will be happy to receive any help, thank you!

#include <iostream>
using namespace std;

int main() {

    int n, reversedNumber = 0, remainder;

    cout << "Enter an integer: ";
    cin >> n;

    while(n != 0) {
        remainder = n%10;
        reversedNumber = reversedNumber*10 + remainder;
        n /= 10;
    }

    cout << "Reversed Number = " << reversedNumber;

    return 0;
}
  • Exit the program if the input is not an integer: `if ( !(cin >> n) ) return -1;`. The parsing will stop at first invalid character. Valid: `123` - return `123`. Valid: `123abc` - returns `123`. Valid: `1.23` - returns `1`. Invalid: `abc`. Invalid: `.123`. – zdf Sep 21 '21 at 07:12

0 Answers0