Hi!

for (year = 1; year <= 15; year++)
    {
	
   cost = (interest_cost + monthly_instalment);
   totalCost+=cost;
   //after how many years the totalCost > price

     if(totalCost>price)
      printf(" %d ", year);
}

question:after how many years the totalCost > price?
my problem:
if the price is equal to 10 $ ;
and the cost is 3$ every year and after 4 years totalCost > price.
the program must print out 4.
But my program prints out 4, 5, 6,,,,,15.
How can I change it?

Recommended Answers

All 7 Replies

If you want it to stop once the cost exceeds the price, use a break statement to exit the loop, or redefine your loop.

Break statement:

for (year = 1; year <= 15; year++)
    {
	
   cost = (interest_cost + monthly_instalment);
   totalCost+=cost;
   //after how many years the totalCost > price

     if(totalCost>price)
        break; //exits for loop
}
      printf(" %d ", year);

Or, if you modify the termination condition...

for (year = 1; (year <= 15)&&(totalcost<=price); year++)
    {
	
   cost = (interest_cost + monthly_instalment);
   totalCost+=cost;
   //after how many years the totalCost > price
}
      printf(" %d ", year);

or, maybe you want the loop to run its full course.
In that case, a flag (in the form of a short or int) would let you only print the statement once.

int exceed_flag =0;//Initialize to zero, because at start, you haven't exceeded the price.

for (year = 1; year <= 15; year++)
    {
	
   cost = (interest_cost + monthly_instalment);
   totalCost+=cost;
   //after how many years the totalCost > price

     if(totalCost>price&&!exceed_flag)
     { 
      exceed_flag = 1;     //This locks out the if statement from happening again, as !1 is a logical zero.
      printf(" %d ", year);
      }
}

Hi Drowzee !
Thank you very much for your help.
peter

No problem.
I'm sure there's another way (there usually are several ways to do anything in C/C++, but some of them are more correct than others), but I don't know enough to try getting fancier.

Hi again!
I did not understand the last part of your answer.
I mean exceed_flag.
How dose it work?
If you have time please help me whit this.
peter

I've a little time.

Think of it like a gate. When it's open, you can pass inside the fence (the 'if' statement, in this case). When it's closed, you can't.
What the exceed_flag variable does is close the gate when a certain criteria is met.

To best understand it, your program was misbehaving because it was doing exactly what you told it. Every time it went around the loop past year four, the totalcost kept getting larger than the price, correct? It would never decrease, so it would always print the year.

You only want the first year, so you need a way to keep the program from executing that line of code more than once. This is where the exceed_flag comes in.

You set the flag to zero at the start, and add the && !exceed_flag to your if statement. The if now reads as:

If the total cost is greater than price AND the exceed_flag is not non-zero, enter this section of the code.

The ! operator is a logical 'not'. !0 = 1 (True) and !1 = 0( False) where the standard is that 1 is true and 0 is false.

Once inside the if, (the fence), exceed_flag is set to 1 (true), and the year is printed.
After that, every time that the loop comes to the if, the first part (total cost is greater than price) is true, but !exceed_flag is false.

The logical and operator, &&, is only true if both of the arguments are true.
!exceed_flag is false, so the program can't enter the if statement again, unless exceed_flag was set back to zero somehow.

And now, I've a bus to catch.
Good luck!

Thank you agian.
thank you very much.
peter

No problem. Just keep in mind that writing a program is more than just correct syntax, it's also how you approach the problem.

That's why any number of professors and coders agree that the first thing you need is a plan that's defined enough that you can go back to it later and remember what you're trying to do and how you want to do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.