# What's wrong with this code?

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 10-02-2004
stillwell
Haha, that's great guys, I really appreciate your help. I was a bit scared I would be screamed at and told to go look at tutorials, after reading some of the stickies. I hope you'll be as friendly and helpful in the future.

What do the /* and */ mean in this string of code
Code:

`else /* y==10 */ r = 10;`
And can't I just end it with?
Code:

`else if (y==10) r = 10;`
Do I have to end with an else?
• 10-02-2004
misplaced
nah.....hackers and future hackers are quite the friendly bunch...as long as you try first
• 10-02-2004
/* starts a comment and */ ends a comment. The comment may be multiline

// is a one line comment only
yes you can end with an else if as you posted, but you may get a warning that not all cases are handled, although if you use the compound conditional as zzzzaahh did, then everything is covered.
• 10-02-2004
misplaced
if that was code someone fixed for you, then the /* y == 10 */ was probably a mistake
that they wanted to point out
• 10-03-2004
zzzaaahhh
You can certainly end with
Code:

`    else if (y==10) r = 10;`
That's perfectly good code. The only reason I wrote
Code:

`    else /* y==10 */ r=10;    //the stuff in between /* */ is a comment`
is because sometimes I like to make the last case end with just an else. But then I also like to put in a little reminder about what that last case was -- which is why I put the comment in. But your way is totally valid. In fact, it's probably even better, since it's totally self-explanatory and doesn't need any comment.

• 10-03-2004
no,

in this instance

else /* y == 10 */

was eqivalent to

else if(y == 10)

because y had been restricted to values of 3, 5 or 10 previously and the cases for both 3 and 5 were handled previously. Therefore, there was no need for an else if(), an else would do. The comment made it "clear" that this case was only desired for y == 10. Because the author continued with desired code on the same line

else /* y == 10 */ r = 10;

they couldn't use the // to make the comment. However, it may have been even more clear like this:
Code:

```else //y == 10   r = 10;```
• 10-03-2004
zzzaaahhh
That's a better explanation that what I gave elad.

But just in case it's not clear that your disagreement is with my explanation and not with stilwell's code, I do want to emphasize that the code stilwell wrote
Code:

`    else if (y==10) r=10;`
is just as valid as
Code:

`    else /* y==10 */ r=10;`
Legally, it will compile. Logically, it will do what he wants it to do. Stylistically, it's very clear what's going on.

Also, the explanation of the difference between // and /* */ has been complete, but spread out over a couple posts. Here it is in one place:
1. Both // and /* */ are used to indicate comments.
2. // makes everything after it on the same line into a comment. Code resumes on the next line.
3. /* starts a comment, and */ ends the comment. The comment can be of any length. Code resumes after the */.
4. C++ compilers will recognize both // and /* */ style comments.
C compilers only recognize /* */ style comments. (At least, my C compiler doesn't recognize //).
• 10-03-2004
stillwell
Ok, I got it. This is really good help.

My program ended up looking like this:
Code:

```main() { const int i = 500; int n = 0, y = 0; double r, a; char c[20];   cout << "---------------------Banky-bank loans--------------------" << endl         << "We offer loans at only 8.5, 9 or 10%, "                    << endl         << "depending on how long the repayment period will be."      << endl         << "---------------------------------------------------------" << endl         << "We charge a minor 500 credit administration fee."          << endl         << "---------------------------------------------------------" << endl;         cout << endl << "What currency do you wish to use? ";         cin  >> c;         cout << endl << "Inset amount you want to borrow ";         cin  >> a;         while (!( y == 1 || y == 3 || y == 10))         {         cout << endl << "Do you want the 1, 3 or 10 year repayment plan? ";         cin  >> y;         }         if      (y == 1) r = 8.5;         else if (y == 3) r = 9;         else            r = 10;         while (n < y)         {         a += (a/100)*r;         n++;         }   cout << fixed << setprecision(2)                                    << endl         << "You'll pay a monthly rate of " << (i+a)/(12*y) << " " << c << endl         << "You will have payed " << a+i << " " << c << " in all"      << endl; }```
• 10-03-2004
misplaced
ah....yet another chapter written...now we bid our farewell to this thread...
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12