A community for students.
Here's the question you clicked on:
 0 viewing
Christos
 3 years ago
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.
Christos
 3 years ago
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.

This Question is Closed

Christos
 3 years ago
Best ResponseYou've already chosen the best response.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 = InitialValueInitialValue*30/100; if (NumberOfYears <= 8) { while (counter3 <= NumberOfYears) ValueAfterAYear = ValueAfterAYearValueAfterAYear*10/100; counter3++; } if (NumberOfYears > 8) { while (counter2 <= 8) { ValueAfterAYear = ValueAfterAYearValueAfterAYear*10/100; counter2++; } while (counter < NumberOfYears) { ValueAfterAYear = ValueAfterAYearValueAfterAYear*5/100; NumberOfYears; } } if (ValueAfterAYear < 500) ValueAfterAYear = 500; cout << "The value of the car is " << ValueAfterAYear; return 0; }

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0I'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".

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0Yes 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

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0* * * The program has to do this: http://screencast.com/t/BFLvrQUh5b

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0Look here: while (counter3 <= NumberOfYears) ValueAfterAYear = ValueAfterAYearValueAfterAYear*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 {}.

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0Oh right! Hold on lemme check!

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0Ok 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

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0Are 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

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0hm I am using Xcode (Mac application)

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0Alright, 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

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0I also have Visual Studio 2010 and Visual Studio 2012 I can run the code on those programs too on Virtual Machine

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0check 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

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0I hope that's a good hint for you

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0Really? For you it gets this result? Look here: http://screencast.com/t/3uQDOhBe This is the result from me

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0You're changing the value from 5000 to 3500 BEFORE the loop; so: First iteration: 3150 Second iteration: 2835 ###Third iteration: 2552 ### Fourth iteration: 2297

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0also, you'll notice that it proves the wrong results consistently except for the third iteration in this case

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0i'm 90% sure i know why that is, let me just check

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0Initial 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)

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0You're changing the value from 5000 to 3500 BEFORE the loop; so: First iteration: 3150 Second iteration: 2835 ###Third iteration: 2552 ### Fourth iteration: 2297

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0Do you see the problem?

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0You'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

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0well consider how many years have already passed when the while loop starts

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0i would advise setting your counter to start at that

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0counter3=2, and while (counter3 < NumberOfYears) fixes your problem

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0Yea I did this and the first part is completely done

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0so it's all fixed? :)

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0Hey something last, Would it be possible to tell me how to make use of those "breakpoints" in Visual Studio?

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0Ok Should I open 2010 or 2012 ??

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0http://i.imgur.com/BMAdkTm.png see that red dot the arrow points to?

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0That's a breakpoint. By putting that there, it tells the program to pause execution at that point.

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0You can then press F10 to step through the program, line by line, or F5 to continue until the next breakpoint

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0the value of this is it lets you see the values stored in your variables when the program isn't finished yet

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0so 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.

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0So put a breakpoint next to ValueAfterAYear = ValueAfterAYearValueAfterAYear*10/100; (You can do this by clicking on the gray bar on the far left)

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0lemme know when you're put a breakpoint there :)

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0have you started the program in debug mode, and entered your values (5000, and 5) in this case?

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0http://i.imgur.com/uoHs6hZ.png look at where ive highlighted in a red box

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0do you see something similar to this?

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0yea there is the same exact box on me too

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0k put your mouse over ValueAfterAYear in the code

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0when you do that it should come up with "3500", you can do that with any variable within the scope when you're debugging

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0try it with other variables too

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0ooh I see!! very useful!!

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0you'll notice the error jump down a line

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0and ValueAfterAYear has now changed to 3150

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0you can look at it from either the box or from mouseovering it

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0I noticed the 3150 but what do you mean by an error??

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0sorry i mean arrow***

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0the arrow on the left

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0ok, keep pressing F10 until you're back at where we are now

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0if 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)

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0you can also set breakpoints while your code is running

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0and if you want, double click the value of ValueAfterAYear in the auto's box, you can actually change the value if you want

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0things like this are so useful when trying to fix a logic problem

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0but if I change the value will the code change too, so that it corresponds to the changed value?

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0the actual code itself doesn't change, but within the program itself, the value changes, so it executes as per your changed value

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0you don't change the value mid program much, it's just a useful thing to know in case you ever need to do that

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0think you understand breakpoints now?

Christos
 3 years ago
Best ResponseYou've already chosen the best response.0Yea I will use them a lot with nested loops and such! You have no idea how this helps !! :)
Ask your own question
Sign UpFind more explanations on OpenStudy
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
 Engagement 19 Mad Hatter
 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.