Results 1 to 13 of 13

Thread: Help with complex if statements

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    6

    Help with complex if statements

    Code:
    #include <iostream.h>
    int main()
    {
    	int myLook;
    	cout << "Rate my looks on a scale from 1 to 10\n";
    	cin >> myLook;
    	{
    	if (myLook == 1)
    		cout << "1\n";
    	if (myLook == 2)
    		cout << "2\n";
    	if (myLook == 3)
    		cout << "3\n";
    	if (myLook == 4)
    		cout << "4\n";
    	if (myLook == 5)
    		cout << "5\n";
    	if (myLook == 6)
    		cout << "6\n";
    	if (myLook == 7)
    		cout << "7\n";
    	if (myLook == 8)
    		cout << "8\n";
    	if (myLook == 9)
    		cout << "9\n";
    	if (myLook == 10)
    		cout << "10\n";
    	return 0;
    	}
    }
    uhh I basically want to make it so if you type in a number it will say something I have it set to say the number you type in right now, I want it so if you type in a number that is not between 1-10 that it says error or something, could someone give me some ideas or examples of what I could do to make this work the way I want it to?

    Thanks

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I would use a switch statement.
    Code:
    #include <iostream.h>
    
    int main()
    {
    	int myLook;
    
    	cout << "Rate my looks on a scale from 1 to 10\n";
    	cin >> myLook;
    	
    	switch (myLook)
    	{
    		case 1:
    			cout << "1\n";
    			break;
    		case 2:
    			cout << "2\n";
    			break;
    		case 3:
    			cout << "3\n";
    			break;
    		case 4:
    			cout << "4\n";
    			break;
    		case 5:
    			cout << "5\n";
    			break;
    		case 6:
    			cout << "6\n";
    			break;
    		case 7:
    			cout << "7\n";
    			break;
    		case 8:
    			cout << "8\n";
    			break;
    		case 9:
    			cout << "9\n";
    			break;
    		case 10:
    			cout << "10\n";
    			break;
    		default:
    			cout << "Please enter a number between 1 and 10.\n";
    	}
    
    	return 0;
    
    }
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    6
    hmm that works was trying to get something working using if and else statements... reading a book and havent yet gotten to switch statements :\

    Thanks for help

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Well, you can use if and else if's as well, but generally a switch block is considered easier to read, especially with your type of example.

    Code:
    #include <iostream.h>
    
    int main()
    {
    	int myLook;
    
    	cout << "Rate my looks on a scale from 1 to 10\n";
    	cin >> myLook;
    
    	if (myLook == 1)
    		cout << "1\n";
    	else if (myLook == 2)
    		cout << "2\n";
    	else if (myLook == 3)
    		cout << "3\n";
    	else if (myLook == 4)
    		cout << "4\n";
    	else if (myLook == 5)
    		cout << "5\n";
    	else if (myLook == 6)
    		cout << "6\n";
    	else if (myLook == 7)
    		cout << "7\n";
    	else if (myLook == 8)
    		cout << "8\n";
    	else if (myLook == 9)
    		cout << "9\n";
    	else if (myLook == 10)
    		cout << "10\n";
    	else
    		cout << "Please enter a number between 1 and 10.\n";
    
    
    	return 0;
    }
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Why not just use cout << mylook << endl ?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Fanatic Member Kings's Avatar
    Join Date
    Aug 2001
    Posts
    673
    Because he wants a number from 1 to 10?
    K i n g s

  7. #7
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    then Bebo could use

    Code:
    int myLook;
    cout<<"Enter a Number"<<endl;
    cin>>myLook;
    if(myLook<=10)
    cout<<myLook;

  8. #8
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by parksie
    Why not just use cout << mylook << endl ?
    I just assumed that outputting the number again was an example, not the actual purpose of the program.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Oh. An array could be used for a lookup, as well
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Meh, he hasn't even gotten to switch yet.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    why should switch be taught before arrays?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I don't know, its a crazy world.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Not before arrays, but before using arrays as lookup tables.

    Code:
    int myLook;
    cout<<"Enter a Number"<<endl;
    cin>>myLook;
    if(myLook<=10)
    cout<<myLook;
    should be

    Code:
    int myLook;
    cout<<"Enter a Number"<<endl;
    cin>>myLook;
    if(myLook<=10 && myLook >=1)
      cout<<myLook;
    else
      cout << "Enter a number between 1 and 10" << endl;
    And it should include <iostream>, not <iostream.h>:

    Code:
    #include <iostream>
    using namespace std;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width