|
-
Nov 16th, 2002, 09:30 PM
#1
Thread Starter
New Member
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
-
Nov 16th, 2002, 09:50 PM
#2
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
-
Nov 16th, 2002, 10:23 PM
#3
Thread Starter
New Member
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
-
Nov 16th, 2002, 10:28 PM
#4
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
-
Nov 17th, 2002, 08:25 AM
#5
Monday Morning Lunatic
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
-
Nov 17th, 2002, 08:53 AM
#6
Fanatic Member
Because he wants a number from 1 to 10?
-
Nov 17th, 2002, 09:12 AM
#7
<?="Moderator"?>
then Bebo could use
Code:
int myLook;
cout<<"Enter a Number"<<endl;
cin>>myLook;
if(myLook<=10)
cout<<myLook;
-
Nov 17th, 2002, 11:44 AM
#8
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
-
Nov 17th, 2002, 11:46 AM
#9
Monday Morning Lunatic
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
-
Nov 17th, 2002, 11:48 AM
#10
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
-
Nov 17th, 2002, 11:52 AM
#11
transcendental analytic
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.
-
Nov 17th, 2002, 11:55 AM
#12
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
-
Nov 18th, 2002, 05:13 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|