Thread: Noob here, having trouble with simple program

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    4

    Unhappy Noob here, having trouble with simple program

    Hi, new programmer here, having trouble with very simple assignment, everytime i run this program it keeps printing zero as the average.




    int main()
    {
    int numExercises, totalScore = 0, pointsPossible = 0;
    bool condition = true;

    cout << "Score received for exercise N: ";
    cin >> totalScore;
    cout << "Total points possible for exercise N: ";
    cin >> pointsPossible;


    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(4);

    cout << "Your average is " << totalScore / pointsPossible;

    return 0;



    can anyone tell me whats wrong with it?
    Last edited by NoobMan; 09-19-2016 at 10:09 PM.

  2. #2
    Guest
    Guest
    You can put your code in [code][/code] tags, which makes is easier for others to read.

    Your issue comes from the use of integers in the division. C++ will truncate these values to whole numbers, so e.g. 0.76 becomes 0. To get the behavior you want, you can either do:
    Code:
    double totalScore = 0, pointsPossible = 0;
    Or cast (one of) the values to floating point during the calculation:
    Code:
    static_cast<double>(totalScore) / pointsPossible;

  3. #3
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    A way to debug that kind of problem would be to feed the program some data it probably shouldn't see but might help diagnose the issue.

    If you feed it "10" and "2", you'll get "5". This tells you that the division part actually does "something"* right when totalScore > pointsPossible. Now feed it "10" and "3" - you'll get "3". But is 10/3 == 3? Yes, if you take into account that we're dealing with integral division here.

    Standardese:
    5.6 Multiplicative operators [expr.mul] N4582
    4 [...] For integral operands the / operator yields the algebraic quotient with any fractional part discarded; [...]
    Both operants of / in your case are of integral type, thus integral division.
    *) which also means the division part actually did everything right, just not necessarily what you wanted.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Guest View Post
    Or cast (one of) the values to floating point during the calculation:
    Code:
    static_cast<double>(totalScore) / pointsPossible;
    I like to do
    1.0 * TotalScore / PointsScore
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Guest
    Guest
    Oh yes, that's less verbose looking Elysia!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Didn't say it was better/worse, more verbose/less verbose, just that it's another way doing it and I like to do it that way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2016
    Posts
    4
    Thank you all so much!

  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I get the point of reinterpret_cast and I guess dynamic_cast is sometimes a thing (though I've seen its use be discouraged) but I really don't get static_cast vs just (double ) my_value.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If my_value is a pointer, (double)my_value will happily work, but static_cast will fail.
    If my_value is const, (double)my_value will happily work, but static_cast will fail.
    And so on...

    (type) is an Evil™ type cast that bundles static_cast, reinterpret_cast and const_cast all into one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I like to do
    1.0 * TotalScore / PointsScore
    Didn't say it was better/worse, more verbose/less verbose, just that it's another way doing it and I like to do it that way.
    The cast does make your intentions explicit at a glance though. If I deleted the "1.0 *" thinking it did nothing, the only thing between me and committing the code is a run-time error. You better hope it happens.

    Sometimes it's not about how slick you can make something. The point of implicit type promotion is so you don't have to think about it.
    Last edited by whiteflags; 09-24-2016 at 08:17 AM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To me, the 1.0 is making it explicit, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I know because you brought it up. I'm just expressing my preferences as well. Code is about communication, bla bla bla.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah I know. You make a good point.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Elysia View Post
    If my_value is a pointer, (double)my_value will happily work, but static_cast will fail.
    If my_value is const, (double)my_value will happily work, but static_cast will fail.
    And so on...

    (type) is an Evil™ type cast that bundles static_cast, reinterpret_cast and const_cast all into one.
    The pointer conversion is a good point. C++'s type system is pretty strong though so it's easy to understand what you're casting unless you obfuscate behind a bunch of typedefs. Okay, so maybe static_cast should always be used then because any decent library has typedefs out the wazoo.

    Also, I didn't know that, is it really that easy to cast away constness using that? I always thought you had to do more nefarious things to effectively undermine const-ness

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, a C-cast WILL remove constness.
    In C++, you do have to go to pretty great lengths to remove it, that is, if you're using C++ casts. Neither static_cast nor reinterpret_cast will remove const.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-21-2011, 10:21 PM
  2. Replies: 1
    Last Post: 04-12-2009, 04:21 AM
  3. noob help on simple program
    By halfpint101 in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2007, 07:29 PM
  4. Replies: 2
    Last Post: 11-17-2007, 04:25 PM
  5. Having trouble with a simple program......
    By Redpred in forum C Programming
    Replies: 9
    Last Post: 08-11-2006, 04:39 AM

Tags for this Thread