-3

I'm trying to make program in which user is asked to enter 3 numbers. Each number must be between 1 and 1000. I have problems with while loop because I can't figure out the proper statement in which the loop won't run infinitely. If the number is less than 1 but bigger than 1000, the user will be asked to enter a number again.

# include <iostream>

using namespace std;

int a, b,c, sum;


int main (){


    cout << "Enter first number:";
    cin >> a;
        while ( a <=1 || a<=1000)
            cout<< "Entered number must between 1 .. 1000";

    cout << "Enter second number:";
    cin >> b;
        if ( b <=1 || b <=1000)
            cout<< "Entered number must between 1 .. 1000";

    cout << "Enter third number:";
    cin >> c;

        if ( c <=1 || c <=1000)
            cout<< "Entered number must between 1 .. 1000";

    sum = a+b+c;

    cout << a <<"+"<<b << "+"<< c << "Your sum is: " << sum << endl;


    return 0;

}
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
  • 1
    I voted this question down because it showed a serious lack in doing any research yourself. You should read a book about C++ before asking. This guide may help you: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Brotcrunsher Oct 22 '17 at 20:44
  • Thank you for letting me know about books. I appreciate your advice. – BloodySandwich Oct 22 '17 at 20:52
  • *"less than 1 but bigger than 1000"* doesn't happen, but if you mean *"less than 1 **or** bigger than 1000"*, that is written `if (a < 1 || a > 1000)`. – Bo Persson Oct 22 '17 at 20:55
  • Yes, sorry I had made mistake when I type what I meant was less than 1 or less than 1000. – BloodySandwich Oct 22 '17 at 21:20

1 Answers1

0

Here's a densely packed example that should show you some techniques:

Live On Coliru

#include <iostream>
#include <limits>

namespace {
    void ignore_rest_of_line(std::istream& is) {
        is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    void try_recover(std::istream& is) {
        if (is.eof())
            throw std::runtime_error("Unexpected end of input");
        is.clear();
        ignore_rest_of_line(is);
    }
}

template <typename T, typename Validator>
T prompt(std::string label, Validator f) {
    T value;
    while (true) {
        std::cout << "Enter " << label << ": ";
        if (std::cin >> value) {
            if (f(value))
                return value;
        } else {
            std::cerr << "Invalid input\n";
            try_recover(std::cin);
        }
    }

    // unreachable
}

int main() {
    auto validate = [](int i) { return i>=2 && i<=999; };
    int a = prompt<int>("first number (2..999)", validate);
    int b = prompt<int>("second number (2..999)", validate);
    int c = prompt<int>("third number (2..999)", validate);

    int sum = a + b + c;

    std::cout << a << "+" << b << "+" << c << " Your sum is: " << sum << std::endl;
}

Prints

Enter first number (2..1000): a
Invalid input
Enter first number (2..1000): 0
Enter first number (2..1000): 99999
Enter first number (2..1000): 2
Enter second number (2..1000): 2
Enter third number (2..1000): 2
2+2+2 Your sum is: 6
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Improved version to detect eof too http://coliru.stacked-crooked.com/a/5948c69bba3c2f01 – sehe Oct 22 '17 at 21:38
  • Thank you, is there any way I could send a pm to you? – BloodySandwich Oct 22 '17 at 21:44
  • Nope. But if you get 20 rep you can start a chat room or visit an existing one like https://chat.stackoverflow.com/rooms/116940/c-questions-and-answers – sehe Oct 22 '17 at 21:45
  • You think stuff like `std::cin.ignore(16u<<20, '\n');` is good practice? –  Oct 22 '17 at 22:16
  • @NeilButterworth It's entirely too easy to spot the limitations of anyone's answers. I can visit yours too and be pedantic. Feel free to mentally substitute `std::numeric_limits::max()` or whatever is the most appropriate again (quoting from memory here). Can't you see that obsessing over details like this is quite fruitless? Unless you mean it's the spelling of 16MiB that irks you? In that case I'll happily edit to `16u*1024*1024`. – sehe Oct 22 '17 at 22:18
  • I'm not obsessing, I simply think that stuff like `16u<<20` is obviously bad practice. And what's so special about 16Mb? –  Oct 22 '17 at 22:30
  • @NeilButterworth what would you write? This is not a language-lawyering or scientific question. It's about the (dubious) craft of using C++ to do console IO. – sehe Oct 22 '17 at 22:30
  • https://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input –  Oct 22 '17 at 22:37
  • @NeilButterworth Thanks for the authoritative link. In practice, I'll take the risk of a user typing in over 16 megabyte of text data at the console, all without ever being tempted to hit Enter. To the OP: the linked question shows you the details of how to _best_ skip the remaining input till the end of line. – sehe Oct 22 '17 at 22:40
  • Thanks guys for interesting conversation in my post. – BloodySandwich Oct 22 '17 at 22:54
  • New, [improved version](https://stackoverflow.com/posts/46879354/revisions) that rights my wrongdoings, hopefully /cc @NeilButterworth – sehe Oct 22 '17 at 22:55
  • @sehe: I will posit that it was better previously. When you're ignoring data through the end of the line, you should set the maximum to ignore to a large enough value that if it's exceeded, you're convinced that there's a problem--and when that occurs, you should *report* the problem. Programming the computer to read and ignore data for what could be hours or even years is a **bad** idea and should be avoided. If anything, the 16 megabytes you initially chose was too *large* a number. – Jerry Coffin Oct 22 '17 at 23:35
  • @JerryCoffin I know right, but there's just no pleasing the style cops. I'll defer to qualitity standards (TM) today. And happily write something stupid like `ignore(1024, '\n')` in production code (haha, as if that uses `iostreams` for receiving interactice input! But _when I do_...) – sehe Oct 22 '17 at 23:37