I just started learning C++ and am trying to learn how to use scanf and printf for faster input and output. Here is the code I'm currently working on:
#include <stdio.h>
using namespace std;
int main() {
int time, record;
double down, loan;
while (scanf("%d %lf %lf %d", &time, &down, &loan, &record) != EOF) {
double value = down + loan;
double owed = loan;
double payment = owed/time;
// current simulated month and depreciation
int rday, c = 0;
double temp, dep;
bool found = false;
// finds value and owed after records
while (!found && record > 0) {
scanf("%d %lf", &rday, &temp);
// adjusts value and owed to day before listed on record
while (!found && c <= rday) {
if (c == rday) {
dep = temp;
}
value *= 1 - dep;
if (c > 0) {
owed -= payment;
}
c++;
// determines if found date
if (owed < value) {
found = true;
}
}
record--;
}
// finds point where owed < value
while (!found && value < owed) {
value *= 1 - dep;
owed -= payment;
c++;
}
if (c - 1 == 1) {
printf("%d month\n", c - 1);
}
else {
printf("%d months\n", c - 1);
}
}
return 0;
}
When I run this on Code::Blocks, it prints the correct answers, but the outermost while loop doesn't terminate even when I enter CTRL+Z
(I am using Windows). Here is my input:
30 500.0 15000.0 3
0 .10
1 .03
3 .002
12 500.0 9999.99 2
0 .05
2 .1
60 2400.0 30000.0 3
0 .2
1 .05
12 .025
-99 0 17000 1
Here is an image of the what happens:
I've tried changing the loop condition to scanf("%d %lf %lf %d", &time, &down, &loan, &record) == 4
, but the same problem happens. Could someone please explain what the issue with my code is?