Like Tree1Likes

How to do this?

This is a discussion on How to do this? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by laserlight If you want to get into a style argument with me (and mind you, I'm open ...

  1. #16
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    Quote Originally Posted by laserlight View Post
    If you want to get into a style argument with me (and mind you, I'm open to the use of various reasonable styles, as long as there is consistency: in fact, how I styled your code is not my preferred style), kindly state what benefit does your style provide. In particular, explain:
    • Why did you consistently forget the terminating semi-colon? Could it be that your closing brace is not well placed?
    • Did you intend to do the string concatenation so many times? Could it be that your indentation is misleading?



    Not in this way, yes. But why guess? I already told you to check your compiler's error messages.


    No, that is not the case. A character array, used as a string, can (and cannot) "process numbers" similiar to how a std::string can (and cannot) "process numbers".

    Use std::string if you need a string type. The approach I recommend here is to convert the string into an integer, or simply read as an integer.
    I dont wanna argue.... anywho yeah i think it needs to be an integer and i need to use modulus to get the 2nd digit

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,742
    Quote Originally Posted by ferfy
    I dont wanna argue
    Then format your code according to a reasonable style. Put it another way: if you consistently write code that is hard to read and understand, people will eventually give up reading. In this community, that means that you won't get any help.

    Quote Originally Posted by ferfy
    i need to use modulus to get the 2nd digit
    Yes. Well, if you read it as a string first then you don't need to do it that way, but it would probably be easier to understand.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    Quote Originally Posted by laserlight View Post
    If you want to get into a style argument with me (and mind you, I'm open to the use of various reasonable styles, as long as there is consistency: in fact, how I styled your code is not my preferred style), kindly state what benefit does your style provide. In particular, explain:
    • Why did you consistently forget the terminating semi-colon? Could it be that your closing brace is not well placed?
    • Did you intend to do the string concatenation so many times? Could it be that your indentation is misleading?



    Not in this way, yes. But why guess? I already told you to check your compiler's error messages.


    No, that is not the case. A character array, used as a string, can (and cannot) "process numbers" similiar to how a std::string can (and cannot) "process numbers".

    Use std::string if you need a string type. The approach I recommend here is to convert the string into an integer, or simply read as an integer.
    Dont wanna argue about style....anywho this is the updated code:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    
    {
    
            int grade;
    
    
    
            std::cout << "Enter your grade %: ";
            std::cin >> grade;
    
            if (grade <= 60) {
                    std::cout << "You scored an F\n"; }
    
    
    
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    
    
            if (grade > 71 && grade <= 80) {
                    std::cout << "You scored a C\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    
            if (grade > 81 && grade <= 90) {
                    std::cout << "You scored a B\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    
            if (grade > 91 && grade <= 100) {
                    std::cout << "You scored an A!\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    
    }

    It compiles fine, but for some reason the output is wrong.... -_-
    not sure why, anyone mind helping me out?

  4. #19
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    Quote Originally Posted by laserlight View Post
    If you want to get into a style argument with me (and mind you, I'm open to the use of various reasonable styles, as long as there is consistency: in fact, how I styled your code is not my preferred style), kindly state what benefit does your style provide. In particular, explain:
    • Why did you consistently forget the terminating semi-colon? Could it be that your closing brace is not well placed?
    • Did you intend to do the string concatenation so many times? Could it be that your indentation is misleading?



    Not in this way, yes. But why guess? I already told you to check your compiler's error messages.


    No, that is not the case. A character array, used as a string, can (and cannot) "process numbers" similiar to how a std::string can (and cannot) "process numbers".

    Use std::string if you need a string type. The approach I recommend here is to convert the string into an integer, or simply read as an integer.

    i dont wanna argue about coding style... here's the updated code, it compiles fine, but for some reason the outputs wrong.... WHY DO MY POSTS KEEP NOT SHOWING UP/DELETED? WTF

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    
    {
    
            int grade;
    
    
    
            std::cout << "Enter your grade %: ";
            std::cin >> grade;
    
            if (grade <= 60) {
                    std::cout << "You scored an F\n"; }
    
    
    
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    
    
            if (grade > 71 && grade <= 80) {
                    std::cout << "You scored a C\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    
            if (grade > 81 && grade <= 90) {
                    std::cout << "You scored a B\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    
            if (grade > 91 && grade <= 100) {
                    std::cout << "You scored an A!\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    
    }

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,742
    Quote Originally Posted by ferfy
    It compiles fine, but for some reason the output is wrong...
    You should tell us the test input, the expected output and the actual output.

    Quote Originally Posted by ferfy
    not sure why, anyone mind helping me out?
    Why are you ignoring my suggestion to indent the code properly?

    Look, yes, this is an issue of subjective style. But your style sucks, plain and simple. If you don't fix it, I will mind helping you out. No, not because I want you to follow my own style (which I have not used in this thread), but because I want to be able to read and understand your code without having to constantly get it re-formatted to be readable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    test input: 71, expected output: C-
    Actual output: -----


    Another example: test input: 50 expected output: F+
    actual output: You scored an F
    -+-+-+-+



    I dont like your style of indenting.... but since u can't read mine from now on i'll try to use your style......and i dont see why u can't read mine, it's very easy.....

    EDIT: Besides.... So far i haven't received much help... i've received more help from my friends on irc/skype and none of them complained that my code wasn't readable.... they didn't complain at all about my style for that matter.
    Last edited by ferfy; 03-22-2012 at 10:16 PM.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,742
    Quote Originally Posted by ferfy
    I dont like your style of indenting....
    Why do you indent your code?

    To answer my own question: I indent code so that the reader can easily see where blocks of scope are marked out. Thus, I indent the body of a function by one level so it is easily to see what is the body of the function. I indent the body of the block of an if statement or loop by one level for the same reason.

    What's your reasoning, and how does your style match it?

    Quote Originally Posted by ferfy
    i dont see why u can't read mine, it's very easy.....
    You cannot read your own code either. If you could, you would have easily discovered the reason for the incorrect output that you are getting now. In fact, as soon as I indented your code properly, your mistake became so blatantly obvious that I pointed it out to you in post #15.

    I am not insisting that you indent properly because I am trying to impose my style on you. I am insisting that you indent properly because it helps you to write correct code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This is not a style issue. Your code is improperly indented. Indentation indicates the block structure of the code and shows what statements a conditional or loop statement controls.

    This:
    Code:
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    Is wrong.
    It should be like this (keeping your brace style)
    Code:
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n"; }
            if (grade % 10 <= 3) {
                    std::cout << '-'; }
            if (grade % 10 > 7 || grade % 10 == 0) {
                    std::cout << '+'; }
    If you feel it should be indented differently, then you are probably misreading what your braces are saying. Maybe you mean this
    Code:
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n";
                    if (grade % 10 <= 3) {
                            std::cout << '-'; }
                    if (grade % 10 > 7 || grade % 10 == 0) {
                            std::cout << '+'; } }
    Notice (with some difficulty) that I've moved the brace from the second line down to the end of the last line. Normal brace placement would make this more obvious:
    Code:
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n";
                    if (grade % 10 <= 3) {
                            std::cout << '-';
                    }
                    if (grade % 10 > 7 || grade % 10 == 0) {
                            std::cout << '+';
                    }
            }
    Or
    Code:
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n";
                    if (grade % 10 <= 3)
                            std::cout << '-';
                    if (grade % 10 > 7 || grade % 10 == 0)
                            std::cout << '+';
            }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #24
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    Quote Originally Posted by laserlight View Post
    Why do you indent your code?

    To answer my own question: I indent code so that the reader can easily see where blocks of scope are marked out. Thus, I indent the body of a function by one level so it is easily to see what is the body of the function. I indent the body of the block of an if statement or loop by one level for the same reason.

    What's your reasoning, and how does your style match it?


    You cannot read your own code either. If you could, you would have easily discovered the reason for the incorrect output that you are getting now. In fact, as soon as I indented your code properly, your mistake became so blatantly obvious that I pointed it out to you in post #15.

    I am not insisting that you indent properly because I am trying to impose my style on you. I am insisting that you indent properly because it helps you to write correct code.

    i indent 1 level per if or loop...... same as you.. idk what you mean... b/c that's exactly what i'm doing..... ?!?!?!?


    modified the code a bit, works better, but still wrong output.... 1-3 works fine (except it gives additional '-'s), 4-7 works fine, 8-9 & 0 do not work...... (These are for the 2nd digits for the - or +)
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    
    {
    
            int grade;
    
    
    
            std::cout << "Enter your grade %: ";
            std::cin >> grade;
    
            if (grade <= 60) {
                    std::cout << "You scored an F\n"; }
    
    
    
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    
    
            if (grade > 70 && grade <= 80) {
                    std::cout << "You scored a C\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    
            if (grade > 80 && grade <= 90) {
                    std::cout << "You scored a B\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    
            if (grade > 90 && grade <= 100) {
                    std::cout << "You scored an A!\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    
    }

  10. #25
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Wow.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #26
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    thanks oogabooga.... final code:

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    
    {
    
            int grade;
    
    
    
            std::cout << "Enter your grade %: ";
            std::cin >> grade;
    
            if (grade <= 60) {
                    std::cout << "You scored an F\n"; }
    
    
    
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n";
                            if (grade % 10 <= 3)
                                    std::cout << '-';
                            if (grade % 10 > 7 || grade % 10 == 0)
                                    std::cout << '+'; }
    
    
            if (grade > 70 && grade <= 80) {
                    std::cout << "You scored a C\n";
                            if (grade % 10 <= 3)
                                    std::cout << '-';
                            if (grade % 10 > 7 || grade % 10 == 0)
                                    std::cout << '+'; }
    
            if (grade > 80 && grade <= 90) {
                    std::cout << "You scored a B\n";
                            if (grade % 10 <= 3)
                                    std::cout << '-';
                            if (grade % 10 > 7 || grade % 10 == 0)
                                    std::cout << '+'; }
    
            if (grade > 90 && grade <= 100) {
                    std::cout << "You scored an A!\n";
                            if (grade % 10 <= 3)
                                    std::cout << '-';
                            if (grade % 10 > 7 || grade % 10 == 0)
                                    std::cout << '+'; }
    
    }

    works for the most part.... few bugs.... but i'm getting too nerd raged over this code to work on it any longer
    Last edited by ferfy; 03-22-2012 at 10:49 PM.

  12. #27
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    Final Code:
    [C++] grade.cpp - Pastebin.com

    Bug free now time for a smoke

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    
    {
    
            int grade;
    
    
    
            std::cout << "Enter your grade %: ";
            std::cin >> grade;
    
            if (grade <= 60) {
                    std::cout << "You scored an F\n"; }
    
    
    
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n";
                            if (grade % 10 > 0 && grade % 10 <= 3)
                                    std::cout << '-';
                            if (grade % 10 > 7 || grade % 10 == 0)
                                    std::cout << '+'; }
    
    
            if (grade > 70 && grade <= 80) {
                    std::cout << "You scored a C\n";
                             if (grade % 10 > 0 && grade % 10 <= 3)
                                    std::cout << '-';
                            if (grade % 10 > 7 || grade % 10 == 0)
                                    std::cout << '+'; }
    
            if (grade > 80 && grade <= 90) {
                    std::cout << "You scored a B\n";
                             if (grade % 10 > 0 && grade % 10 <= 3)
                                    std::cout << '-';
                            if (grade % 10 > 7 || grade % 10 == 0)
                                    std::cout << '+'; }
    
            if (grade > 90 && grade <= 100) {
                    std::cout << "You scored an A!\n";
                             if (grade % 10 > 0 && grade % 10 <= 3)
                                    std::cout << '-';
                            if (grade % 10 > 7 || grade % 10 == 0)
                                    std::cout << '+'; }
    
    }

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,742
    Quote Originally Posted by ferfy
    i indent 1 level per if or loop...... same as you.. idk what you mean... b/c that's exactly what i'm doing..... ?!?!?!?
    Looking at the code that you posted in post #19, consider this part:
    Code:
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n"; }
                            if (grade % 10 <= 3) {
                                    std::cout << '-'; }
                            if (grade % 10 > 7 || grade % 10 == 0) {
                                    std::cout << '+'; }
    Notice that you placed a closing brace on this line:
    Code:
    std::cout << "You scored a D\n"; }
    This means that your later two if statements are not nested within the first one. As oogabooga noted in post #23, "Normal brace placement would make this more obvious".


    You fixed this in your code in post #26:
    Code:
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n";
                            if (grade % 10 <= 3)
                                    std::cout << '-';
                            if (grade % 10 > 7 || grade % 10 == 0)
                                    std::cout << '+'; }
    But now you removed the braces, which introduces the possibility of errors later if you should somehow need to make the bodies of the inner if statements more than one line. Also notice that you indented the inner if statements extra. This creates the illusion that they are somehow in a "more local" block of scope that the std::cout statement.

    For your final code, I note that you can simplify it. Notice that you repeat the code for '+' and '-'. With a little tweaking of the text to be printed, and excluding the case for 'F', you can have the code for '+' and '-' after the code to print the letter grade. This was the whole reason why you wanted to use modulo, if you recall
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    ah didn't know i could do that hahahaha

    here's the simplified code:

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    
    {
    
            int grade;
    
    
    
            std::cout << "Enter your grade %: ";
            std::cin >> grade;
    
            if (grade <= 60) {
                    std::cout << "You scored an F\n"; }
    
    
    
            if (grade > 61 && grade <= 70) {
                    std::cout << "You scored a D\n"; }
    
    
            if (grade > 70 && grade <= 80) {
                    std::cout << "You scored a C\n"; }
    
    
    
            if (grade > 80 && grade <= 90) {
                    std::cout << "You scored a B\n"; }
    
    
            if (grade > 90 && grade <= 100) {
                    std::cout << "You scored an A!\n"; }
    
    
                             if (grade % 10 > 0 && grade % 10 <= 3) {
                                    std::cout << '-';
                            if (grade % 10 > 7 || grade % 10 == 0)
                                    std::cout << '+'; }
    
    }
    thanks indentation is a little wacky... but meh

  15. #30
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    my bad..... still was bugged... here's the new fix:

    [C++] grade.cpp - Pastebin.com

    Code:
    1. #include <iostream>
    2. #include <string>
    3. int main()
    4. {
    5. int grade;
    6. std::cout << "Enter your grade %: ";
    7. std::cin >> grade;
    8. if (grade <= 60) {
    9. std::cout << "You scored an F\n"; }
    10. if (grade > 60 && grade <= 70) {
    11. std::cout << "You scored a D\n"; }
    12. if (grade > 70 && grade <= 80) {
    13. std::cout << "You scored a C\n"; }
    14. if (grade > 80 && grade <= 90) {
    15. std::cout << "You scored a B\n"; }
    16. if (grade > 90 && grade <= 100) {
    17. std::cout << "You scored an A!\n"; }
    18. if (grade % 10 > 0 && grade % 10 <= 3)
    19. std::cout << '-';
    20. if (grade % 10 > 7 || grade % 10 == 0)
    21. std::cout << '+';
    22. }

    it's still not efficient cuz i could use if elses.... but i really dont feel like revising it for the 30th time....x.x
    Last edited by ferfy; 03-23-2012 at 03:44 AM.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

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