Limiting input values

This is a discussion on Limiting input values within the C++ Programming forums, part of the General Programming Boards category; I am trying to limit my program to only accept four possible numbers, 1,2,3 and 4. the current working code ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    Limiting input values

    I am trying to limit my program to only accept four possible numbers, 1,2,3 and 4.
    the current working code i have is
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	double x; //choice
    	double r; //radius
    	double a; //volume
    	double b; //surface area
    	double c; //area of a circle
    	double h; //height
    	double l; //length
    	double w; //width
    	
    	do
    	{
    	cout<< "What shape would you like?"<<endl;
    	cout<< "1) Sphere"<< endl;
    	cout<< "2) Cone"<< endl;
    	cout<< "3) Rectangular Prism"<< endl;
    	cout<< "4) Cylinder"<< endl;
    	cout<< "Choice is ";
    	cin>>x;
    	}
    	while (x<1||x>4);//only want numbers 1, 2, 3, or 4
    When i use the code
    Code:
    while (x<1||1<x<2||2<x<3||3<x<4||x>4)
    it returns me to the input command every time even if i input 1,2,3 or 4

    any help is useful
    thx

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    It should be the other way around and use AND:
    while ( x >= 1 && x <= 4 )

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    wont that make the loop run every time i input a value between 1 and 4 instead of excluding all the values outside 1 and 4?
    I want the program to run that section every time i don't input a value of 1, 2, 3 or 4

  4. #4
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Oops, sorry. Wasn't thinking. In that case, all you need is the &&. So:
    while ( x < 1 && x > 4 )

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    it returns me to the input command every time even if i input 1,2,3 or 4
    No it doesn't! The originally posted code with
    Code:
    	while (x<1||x>4);//only want numbers 1, 2, 3, or 4
    is already correct! The loop is exited if you type 1, 2, 3, or 4. If you type a number less than 1 or greater than 4 then the loop is repeated. That is what you have written.

    Did you want it to do something else besides ask for the input again when it was out of range?
    Last edited by iMalc; 02-12-2010 at 12:59 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by yaya View Post
    Oops, sorry. Wasn't thinking. In that case, all you need is the &&. So:
    while ( x < 1 && x > 4 )
    How many numbers do you know that are both less than one AND greater than four?!?!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Haha, damn, I'm having a bad day. I should sleep more before I start answering these.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    By the way, instead of:
    Code:
    double x; //choice
    double r; //radius
    double a; //volume
    double b; //surface area
    double c; //area of a circle
    double h; //height
    double l; //length
    double w; //width
    You might as well write:
    Code:
    double choice;
    double radius;
    double volume;
    double surface_area;
    double circle_area;
    double height;
    double length;
    double width;
    Though choice probably should be of an integer type.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by iMalc View Post
    How many numbers do you know that are both less than one AND greater than four?!?!
    Code:
    int x = -1;
    while (x < 1 && (unsigned int)x > 4)
    Btw, don't take this seriously. It was more of a joke on how you can make even the simplest bit of code really confusing

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Quote Originally Posted by _Mike View Post
    Code:
    int x = -1;
    while (x < 1 && (unsigned int)x > 4)
    Btw, don't take this seriously. It was more of a joke on how you can make even the simplest bit of code really confusing
    Don't worry I'm not going to take it seriously. I haven't yet read up on using the "int" command or yet been taught how to use it in class yet, so you've confused me from the very beginning

    @Laserlight i prefer to use single character variables, since its easier to use them in the math equations later on in the program.

    and im just going to keep my code the way it is now since trying to figure out a way for it to only accept the values 1, 2, 3 or 4 at this point in time for me is beyond my capabilities. Thank you all for helping/trying to help me with my little conundrum
    Last edited by Creatlv3; 02-12-2010 at 01:40 AM. Reason: gramatical fixes

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by Creatlv3
    @Laserlight i prefer to use single character variables, since its easier to use them in the math equations later on in the program.
    Resist the temptation. Code should be readable, not just "writable".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Tis be a strong temptation with 10 years of math drilling single variables into my head, eventually i will probably be able to break it but for the time being im going to use appropriate letters to keep things simple. However "a" for volume and "b" for surface area definitely not the best choices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  3. Displaying Minimum and Maximum values from input
    By mgardnertech in forum C Programming
    Replies: 1
    Last Post: 06-29-2008, 08:47 PM
  4. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 06:38 AM
  5. limiting length of users input
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-20-2001, 08:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21