1

I am trying to minimize hardcoding numbers into my program and allowing for users to define max and min parameters along with making sure that the input is valid.

#include <iostream>   

int main(){ 
    int max, A=0;
    do
    {
        std::cout << "What is the max?\n";
        std::cin >> max;
        if (std::cin.fail()) {
          std::cin.clear();      
          std::cin.ignore();
          std::cout << "not an integer, try again\n";
          continue;
        }
        if(max < -1000){
            std::cout << "That doesnt make much sense, please enter the max again.\n";
        }
    } while (max <A); \\HERE IS WHERE THE PROBLEM IS.
    std::cout << "The max number of steps are " << max <<std::endl;
    return 0;
    }

If A is 0 or less, the program doesn't ask for user input again. instead the program just exits the loop. If A is 1 or more, then then the program loops until a valid input is provided.

I would like the max number to be any int number, including negatives. This is working for positive numbers, but not for maximums that are 0 or less.

2
  • 1
    do you have a question? Commented Jul 27, 2020 at 22:26
  • Fixed a missing line from isolating relevant code. I want the code to keep looping until there is valid input. I want to know how to make the code to work for any integer number for -1000, 1000. Commented Jul 27, 2020 at 22:30

3 Answers 3

1
 do
{
  //ask for input      
  //input taken

} while (A>=1); 

This will the code you have to use for the scenario described at the last line. One more point you just forget to assign any value to A according to your logic.

Thanks!

Sign up to request clarification or add additional context in comments.

Comments

0

If A is 1 or more, then then the program loops until a valid input is provided. - You are saying exactly what the while loop needs to do. Just implement it.

} while (A >= 1); \\If A is greater than or equal to 1 then loop until valid.
    std::cout << "The max number of steps are " << max <<std::endl;
    return 0;
    }

To answer your follow up question:

} while (A >= 1 && max <= 0); \\If A is greater than or equal to 1 then loop until valid.
    std::cout << "The max number of steps are " << max <<std::endl;
    return 0;
    }

1 Comment

What if I want to allow the max number to be a negative or 0.
0

I would suggest writing a custom function that takes an acceptable range of min/max values as input parameters, eg:

int promptForInt(const string &prompt, int minAllowed, int maxAllowed)
{
    int value;

    std::cout << prompt << "\n";

    do
    {
        if (!(std::cin >> value)) {
            std::cin.clear();      
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "not an integer, try again\n";
            continue;
        }

        if ((value >= minAllowed) && (value <= maxAllowed)){
            break;
        }

        std::cout << "not an allowed integer, enter a value between " << minAllowed << " and " << maxAllowed << ".\n";
    }
    while (true);

    return value;
}

int main(){ 
    int max = promptForInt("What is the max?", -1000, 1000);
    std::cout << "The max number of steps are " << max << std::endl;
    return 0;
}

3 Comments

It might also help to explain what (std::numeric_limits<std::streamsize>::max(), '\n') is doing. She asked a separate question a couple hour ago about that specific if statement.
@GenoC I posted a comment about that on the other question
Cool, just thought it would help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.