Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Christos

C++, Help needed! I need a Program that does this: http://screencast.com/t/BFLvrQUh5b Apparently there is something wrong with my code but I don't know why! The behaviour is not correct.

  • 11 months ago
  • 11 months ago

  • This Question is Closed
  1. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    #include <iostream> using namespace std; int main() { int InitialValue,NumberOfYears,counter=9,counter2=1,counter3=1,ValueAfterAYear; cout << "Give me the initial value : "; cin >> InitialValue; cout << "Give me the number of the years of that car : "; cin >> NumberOfYears; ValueAfterAYear = InitialValue-InitialValue*30/100; if (NumberOfYears <= 8) { while (counter3 <= NumberOfYears) ValueAfterAYear = ValueAfterAYear-ValueAfterAYear*10/100; counter3++; } if (NumberOfYears > 8) { while (counter2 <= 8) { ValueAfterAYear = ValueAfterAYear-ValueAfterAYear*10/100; counter2++; } while (counter < NumberOfYears) { ValueAfterAYear = ValueAfterAYear-ValueAfterAYear*5/100; NumberOfYears--; } } if (ValueAfterAYear < 500) ValueAfterAYear = 500; cout << "The value of the car is " << ValueAfterAYear; return 0; }

    • 11 months ago
  2. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    I'll open up Visual Studio in a minute, just tell me exactly what you want to the program to do. I'm assuming you're getting a logic error since you say "The behaviour is not correct".

    • 11 months ago
  3. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    Yes right, I am getting a logic error. My code's syntax is 100% correct. I don't want you to do me the program. If it's possible just tell me where it lacks logic so that I can learn to make better

    • 11 months ago
  4. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    * * * The program has to do this: http://screencast.com/t/BFLvrQUh5b

    • 11 months ago
  5. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    Look here: while (counter3 <= NumberOfYears) ValueAfterAYear = ValueAfterAYear-ValueAfterAYear*10/100; counter3++; It's an infinite loop. Remember that if you don't include {} only the FIRST line is included in the loop. Wrap it in {}.

    • 11 months ago
  6. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    Oh right! Hold on lemme check!

    • 11 months ago
  7. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    okay

    • 11 months ago
  8. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    Ok now runs and it gives me results. for example this: Give me the initial value : 5000 Give me the number of the years of that car : 5 The value of the car is 2068 But with the calculator it gives me 2551.5

    • 11 months ago
  9. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    Are you using an IDE, like Visual Studio, or a CLI compiler like gpp/g++? Just wondering since debugging things like this is easy with breakpoints, etc

    • 11 months ago
  10. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    hm I am using Xcode (Mac application)

    • 11 months ago
  11. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    Alright, well I haven't used that, but just for future reference learning how to use breakpoints will really help things :) Anyway: I'll check the logic of your program, sec

    • 11 months ago
  12. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    I also have Visual Studio 2010 and Visual Studio 2012 I can run the code on those programs too on Virtual Machine

    • 11 months ago
  13. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    check the answer of the equation: Initial value: 5000 years: 5 5000 - 5000 * 30/100 = 3500 3500 - 3500 * 10/100 = 3150 3150 - 3150 * 10/100 = 2835 2835 - 2835 * 10/100 = 2551.5 (2552) That accounts for 5 years Your program IS, in fact, actually getting that result consider the value of counter 3, and how many times it's looping

    • 11 months ago
  14. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    I hope that's a good hint for you

    • 11 months ago
  15. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    Really? For you it gets this result? Look here: http://screencast.com/t/3uQDOhBe This is the result from me

    • 11 months ago
  16. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    You're changing the value from 5000 to 3500 BEFORE the loop; so: First iteration: 3150 Second iteration: 2835 ###Third iteration: 2552 ### Fourth iteration: 2297

    • 11 months ago
  17. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    also, you'll notice that it proves the wrong results consistently except for the third iteration in this case

    • 11 months ago
  18. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    i'm 90% sure i know why that is, let me just check

    • 11 months ago
  19. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    ok

    • 11 months ago
  20. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    Actually, it's fine

    • 11 months ago
  21. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    ??

    • 11 months ago
  22. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    Think of this

    • 11 months ago
  23. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    Initial value: 5000 years: 5 5000 - 5000 * 30/100 = 3500 3500 - 3500 * 10/100 = 3150 3150 - 3150 * 10/100 = 2835 2835 - 2835 * 10/100 = 2551.5 (2552)

    • 11 months ago
  24. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    and then this

    • 11 months ago
  25. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    You're changing the value from 5000 to 3500 BEFORE the loop; so: First iteration: 3150 Second iteration: 2835 ###Third iteration: 2552 ### Fourth iteration: 2297

    • 11 months ago
  26. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    Do you see the problem?

    • 11 months ago
  27. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    You're looping through 5 times even though a) the first year (5000) is taken care of, and the second year (removing 30%) is taken care of

    • 11 months ago
  28. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    ooh

    • 11 months ago
  29. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    see the problem?

    • 11 months ago
  30. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    I removed that "="

    • 11 months ago
  31. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    well consider how many years have already passed when the while loop starts

    • 11 months ago
  32. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    i would advise setting your counter to start at that

    • 11 months ago
  33. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    thanks dude

    • 11 months ago
  34. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    counter3=2, and while (counter3 < NumberOfYears) fixes your problem

    • 11 months ago
  35. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    Yea I did this and the first part is completely done

    • 11 months ago
  36. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    so it's all fixed? :)

    • 11 months ago
  37. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    Hey something last, Would it be possible to tell me how to make use of those "breakpoints" in Visual Studio?

    • 11 months ago
  38. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    sure

    • 11 months ago
  39. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    Ok Should I open 2010 or 2012 ??

    • 11 months ago
  40. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    2010

    • 11 months ago
  41. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    ok

    • 11 months ago
  42. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    http://i.imgur.com/BMAdkTm.png see that red dot the arrow points to?

    • 11 months ago
  43. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    yes

    • 11 months ago
  44. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    That's a breakpoint. By putting that there, it tells the program to pause execution at that point.

    • 11 months ago
  45. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    You can then press F10 to step through the program, line by line, or F5 to continue until the next breakpoint

    • 11 months ago
  46. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    the value of this is it lets you see the values stored in your variables when the program isn't finished yet

    • 11 months ago
  47. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    so you can see the use of this when it comes to debugging. For example, I figured out your problem by putting a breakpoint inside the while loop, and checking the value of valueAfterAYear every iteration.

    • 11 months ago
  48. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    So put a breakpoint next to ValueAfterAYear = ValueAfterAYear-ValueAfterAYear*10/100; (You can do this by clicking on the gray bar on the far left)

    • 11 months ago
  49. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    lemme know when you're put a breakpoint there :)

    • 11 months ago
  50. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    Ok done! Now F10??

    • 11 months ago
  51. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    have you started the program in debug mode, and entered your values (5000, and 5) in this case?

    • 11 months ago
  52. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    yes done!

    • 11 months ago
  53. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    http://i.imgur.com/uoHs6hZ.png look at where ive highlighted in a red box

    • 11 months ago
  54. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    do you see something similar to this?

    • 11 months ago
  55. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    yea there is the same exact box on me too

    • 11 months ago
  56. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    k put your mouse over ValueAfterAYear in the code

    • 11 months ago
  57. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    when you do that it should come up with "3500", you can do that with any variable within the scope when you're debugging

    • 11 months ago
  58. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    try it with other variables too

    • 11 months ago
  59. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    ooh I see!! very useful!!

    • 11 months ago
  60. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    ok press F10

    • 11 months ago
  61. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    you'll notice the error jump down a line

    • 11 months ago
  62. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    and ValueAfterAYear has now changed to 3150

    • 11 months ago
  63. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    you can look at it from either the box or from mouseovering it

    • 11 months ago
  64. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    I noticed the 3150 but what do you mean by an error??

    • 11 months ago
  65. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    sorry i mean arrow***

    • 11 months ago
  66. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    the arrow on the left

    • 11 months ago
  67. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    aaah yea there is one

    • 11 months ago
  68. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    ok, keep pressing F10 until you're back at where we are now

    • 11 months ago
  69. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    it's 2835 now

    • 11 months ago
  70. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    if you want to skip going line by line, you can just pres F5 and it will go to the next breakpoint (since were in a loop it goes back to the same breakpoint)

    • 11 months ago
  71. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    you can also set breakpoints while your code is running

    • 11 months ago
  72. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    and if you want, double click the value of ValueAfterAYear in the auto's box, you can actually change the value if you want

    • 11 months ago
  73. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    things like this are so useful when trying to fix a logic problem

    • 11 months ago
  74. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    but if I change the value will the code change too, so that it corresponds to the changed value?

    • 11 months ago
  75. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    the actual code itself doesn't change, but within the program itself, the value changes, so it executes as per your changed value

    • 11 months ago
  76. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    you don't change the value mid program much, it's just a useful thing to know in case you ever need to do that

    • 11 months ago
  77. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    oh I see

    • 11 months ago
  78. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    think you understand breakpoints now?

    • 11 months ago
  79. Christos
    Best Response
    You've already chosen the best response.
    Medals 0

    Yea I will use them a lot with nested loops and such! You have no idea how this helps !! :)

    • 11 months ago
  80. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    haha np

    • 11 months ago
  81. Anteara
    Best Response
    You've already chosen the best response.
    Medals 2

    glad i could help

    • 11 months ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.