|
-
Feb 7th, 2001, 10:56 PM
#1
Thread Starter
Hyperactive Member
Alright. I am real new at C++ and have a simple bracket { question.
I am not sure of the placement in a nested if, else if statement. Please help!
Here is an example...It gives the same response no matter what number you enter, although I don't know why:
#include <iostream.h>
void main()
{
//declare and initialize variable
short code = 0;
//get code from user
cout << "Enter the code (1 through 5): ";
cin >> code;
//display salary
if (code = 1)
cout << "$45,000" << endl;
else
if (code == 2 || code == 5)
cout << "$33,000" << endl;
else
if (code == 3 || code == 4)
cout << "$25,000" << endl;
else
cout << "Entry error" << endl;
//end if (code == 3 || code == 4)
//end if (code == 2 || code == 5)
//end if (code == 1)
} //end of main function
 Mahalo 
VB6(SP5), VC++, COBOL, Basic, JAVA
MBA, MCSD, MCSE, A+
Computer Forensics
-
Feb 7th, 2001, 11:15 PM
#2
Lively Member
//display salary
if (code = 1)
cout << "$45,000" << endl;
Use "==" instead. Here you are assigning the number 1 to code instead of checking to see if it equals 1.
-
Feb 7th, 2001, 11:25 PM
#3
PowerPoster
Also, for me at least, it is easier to read 'if' statments if the you put {} around the statment you want to be executed.
Like:
Code:
if (x==y) {do this}
else if (x==z) {do this}
else {do this}
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Feb 7th, 2001, 11:32 PM
#4
Thread Starter
Hyperactive Member
Thanks! I spaced on the "=="
VB was a breeze, this is tough!
You guys are awesome!
Cheers!
Lee
 Mahalo 
VB6(SP5), VC++, COBOL, Basic, JAVA
MBA, MCSD, MCSE, A+
Computer Forensics
-
Feb 8th, 2001, 01:10 AM
#5
Addicted Member
You use the {}'s if there is more than one code line of events to do.
if( a == 4 ) {
cout << "This is a test" << endl;
cout << "This is only a test" << endl;
}
else
cout << "Bye" << endl;
-
Feb 8th, 2001, 08:14 AM
#6
PowerPoster
yeah i know, but when using nested if statments you need them and i usually use more than one conditional. So i just always put them and it seems to make it easier for me to read.
Just my opinion though
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Feb 8th, 2001, 04:22 PM
#7
Monday Morning Lunatic
Also if you indent each bracket level it makes it a lot easier to follow the flow:
Code:
if(x) {
if(y) {
cout << "x && y" << 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
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
|