Limiting an input based on the value of another.

I'm writing a program to calculate the eccentricity of an ellipse, that's the easy part. My problem is that I have 2 inputs one for "M", and one for "m" however, "M" must always be larger in value than "m" from a mathematical standpoint. How can I make it so that if the user inputs "M = 9" than the program would only accept a value less than M (in this case 9) for m.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        //designation of variables
        double M, m;

   //inputs of variables.
        std::cout << "Enter the value of Major (M): \n";
        std::cin >> M;
        std::cout << "Enter the value of minor (m): \n";
        std::cin >> m;

//formula for the calculation is: square root of 1-m^2/M^2
        auto eccentricity = sqrt(1-(m * m) / (M * M));

        //output of eccentricity
        std::cout << "\nThe eccentricity of the ellipse is "
            << std::fixed
                << std::setprecision(4)
                    << eccentricity << "\n";


There is a lot more to the code, but the code works as is. It just doesn't work as well as I want it to.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
    double M ;
    std::cout << "Enter the value of Major (M): ";
    std::cin >> M;
    // TO DO: add check for positive M (M>0)

    double m ;
    do
    {
        std::cout << "Enter the value less than " << M  << " of minor (m): ";
        std::cin >> m;
    }
    while( m >= M ) ;
    // TO DO: add check for positive m (m>0)

    std::cout << "M: " << M << " m: " << m << '\n' ;
}
Thanks for your quick response JLBorges, it works thank you very much.
Why not let the user enter the axes in any order and then have the program determine which is major and which is minor?
well dhayden if do that I then the user could go on not knowing that Major has to be larger than minor.
This is my first time ever writing a program so I'm completely new to this, so if you would like to point me in the direction of learning how to do what your suggesting I'd love to learn it for future reference.
Last edited on
This is what has been suggested:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <algorithm>

int main()
{
    double first, second ;
    std::cout << "enter the two axises in any arbitrary order: " ;

    // if the inputs are successful and the two values are positive and one is less than the other
    if( std::cin >> first >> second && first > 0 && second > 0 && first != second )
    {
        const double M = std::max(first,second) ; // larger of the two is the major axis
        const double m = std::min(first,second) ; // smaller of the two is the minor axis
        std::cout << "major axis: " << M << "  minor axis: " << m << '\n';

        // calculate eccentricity etc.
    }
    
    // TO DO: add loop to retry on invalid input
}


Is this clearly better then the your original idea?
I do not know; I suppose it is a matter of opinion.
Topic archived. No new replies allowed.