Thread: Problem in very simple code

  1. #16
    Registered User
    Join Date
    Jan 2006
    Location
    Washington D.C.
    Posts
    25
    Quote Originally Posted by richdb
    Just so no one is confused, maybe I should have said earlier, in the input prompts at the beginning, the values "number_of_a" through d, are first being used to accept the users input in the scanf statements, then that value is being added to the total_number_of_*fines and total_number_fines, and then finally is multiplied by the fine for the corresponding letter (a_charge, b_charge, etc.) Sloppy coding I know. I should be using another variable like "final_a_charge" or something. Hope that clears any confusion.
    And, because you do this, the charges you multiply it by pretty much invalidate the conditional statement altogether. 1 violation in b gives you 20, which is double 10. and 1 in c is 30. Keep in mind the total number of violations isn't even above 20 yet. You really should rework the logic, and think about what you're trying to do there, and why. Your program works fine if you remove it.

  2. #17
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    if you put 10 5 5 and 3 for a b c and d, this should be your output:

    using over twenty fine is 0. (still missing)
    number of d is 150. (just a test)
    The total fine for the person is 500. (correct amount)
    The total number of fines is 23. (correct amount)

    10 is <= 10. Its equal

    the rest is obviously less than ten: 5, 5, 3

  3. #18
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    wait I think I see what your saying.......

  4. #19
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    lol, yeah. I put
    Code:
    printf("\nnumber of c is %d." , number_of_c);
    after the c violation prompt and got this:

    number of c is 150. So I think I need to declare a different variable for the totals in the prompts instead of reusing one three times!

    So even though it would have never reached the if statement in the first place, your saying there was something wrong with the logic in using && instead of || ?

  5. #20
    Registered User
    Join Date
    Jan 2006
    Location
    Washington D.C.
    Posts
    25
    Quote Originally Posted by richdb
    if you put 10 5 5 and 3 for a b c and d, this should be your output:

    using over twenty fine is 0. (still missing)
    number of d is 150. (just a test)
    The total fine for the person is 500. (correct amount)
    The total number of fines is 23. (correct amount)

    10 is <= 10. Its equal

    the rest is obviously less than ten: 5, 5, 3
    But you apply the charges, and when you get to that conditional point where you check all against less than or equal to 10

    number_of_a = 10 * 10 = 100
    number_of_b = 5 * 20 = 100
    number_of_c = 5 * 30 = 150

  6. #21
    Registered User
    Join Date
    Jan 2006
    Location
    Washington D.C.
    Posts
    25
    Quote Originally Posted by richdb
    lol, yeah. I put
    Code:
    printf("\nnumber of c is %d." , number_of_c);
    after the c violation prompt and got this:

    number of c is 150. So I think I need to declare a different variable for the totals in the prompts instead of reusing one three times!

    So even though it would have never reached the if statement in the first place, your saying there was something wrong with the logic in using && instead of || ?
    && = AND
    || = OR

    So just simply think, do you want All of them to have to be true? If so you use And (&&).

    If you want only 1 of them to have to be true, you use Or (||).

  7. #22
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    They all need to be true, but individually. So yeah: &&

  8. #23
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Thanks folks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  2. problem with some simple code =/
    By Josh Norton in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 06:27 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM