What's wrong with this code?

This is a discussion on What's wrong with this code? within the C++ Programming forums, part of the General Programming Boards category; Haha, that's great guys, I really appreciate your help. I was a bit scared I would be screamed at and ...

  1. #16
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    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?
    Last edited by stillwell; 10-02-2004 at 12:36 PM.

  2. #17
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    nah.....hackers and future hackers are quite the friendly bunch...as long as you try first

  3. #18
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    /* 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.

  4. #19
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    if that was code someone fixed for you, then the /* y == 10 */ was probably a mistake
    that they wanted to point out

  5. #20
    Registered User
    Join Date
    Oct 2004
    Posts
    26
    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.

    Good job on your program!

  6. #21
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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;

  7. #22
    Registered User
    Join Date
    Oct 2004
    Posts
    26
    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 //).

  8. #23
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    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;
    
    }

  9. #24
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ah....yet another chapter written...now we bid our farewell to this thread...

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

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