#include <stdio.h>
#include <conio.h>
int main(void)
{
int year;
float principal, amount, inrate, period, value;
printf ("Please enter principal");
scanf ("%f", principal);
amount = principal;
printf ("Please enter interest rate");
scanf ("%f", inrate);
year = 0;
printf ("Please enter period");
scanf ("%f", period);
while(year <= period)
{printf ("%d %f\n", year, amount);
value = amount + amount*inrate;
year = year + 1;
amount = value;
}
getch();
return 0;
}
I tried running this code but I have no output at all. There are 0 warnings and messages. Frankly, I don't know if the code will serve the intended purpose without being able to run it! Please help.
I tried running this code but I have no output at all
Really? I got 6 warnings and a segfault! what compiler are you using?
||=== Build: Debug in test (compiler: GNU GCC Compiler) ===|
main.cpp||In function 'int main()':|
main.cpp|8|warning: format '%f' expects argument of type 'float*', but argument 2 has type 'double' [-Wformat]|
main.cpp|11|warning: format '%f' expects argument of type 'float*', but argument 2 has type 'double' [-Wformat]|
main.cpp|14|warning: format '%f' expects argument of type 'float*', but argument 2 has type 'double' [-Wformat]|
main.cpp|8|warning: 'principal' is used uninitialized in this function [-Wuninitialized]|
main.cpp|11|warning: 'inrate' is used uninitialized in this function [-Wuninitialized]|
main.cpp|14|warning: 'period' is used uninitialized in this function [-Wuninitialized]|
||=== Build finished: 0 error(s), 6 warning(s) (0 minute(s), 0 second(s)) ===|
The code looks like some sort of interest calculator ( https://en.wikipedia.org/wiki/Interest )
try that code:
#include <stdio.h>
#include <conio.h>
int main(void)
{
int year;
float principal, amount, inrate, period, value;
printf ("Please enter principal ");
scanf ("%f", &principal);
amount = principal;
printf ("Please enter interest rate ");
scanf ("%f", &inrate);
year = 0;
printf ("Please enter period ");
scanf ("%f", &period);
while(year <= period)
{
printf ("%d %f\n", year, amount);
value = amount + amount*inrate;
year = year + 1;
amount = value;
}
getch();
return 0;
}
scanf reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. So if you want to save something in the variable with scanf, you should give pointer as argument with &.
After adding the &
address-of before the variable arguments in the scanf()
calls, it works. But I didn't check the arithmetic.
#include <stdio.h>
#include <conio.h>
int main(void)
{
int year;
float principal, amount, inrate, period, value;
printf ("Please enter principal ");
scanf ("%f", &principal); // <-- added &
amount = principal;
printf ("Please enter interest rate ");
scanf ("%f", &inrate); // <-- added &
year = 0;
printf ("Please enter period ");
scanf ("%f", &period); // <-- added &
while(year <= period) {
printf ("%d %f\n", year, amount);
value = amount + amount*inrate;
year = year + 1;
amount = value;
}
getch();
return 0;
}
The problems you are having are twofold. The first, scanf
requires a pointer to store values. (eg scanf ("%f", principal);
should be scanf ("%f", &principal);
)
Another issue to be aware of is reading values with scanf
will leave a newline '\\n'
in the input buffer stdin
each time you press [Enter]
. scanf
will read the number you enter, but leave the newline in stdin
. The next time you call scanf
it sees the newline (value: 0xa
hex, 10
) in stdin
and reads that as the next value.
Note: in this case, %f
will skip the newline, so it is not necessary. However, be aware that decimals or strings read by scanf
will be effected. Always keep this in mind when using scanf
.
If faced with scanf
seeming to skip over expected input, a simple solution is the flush (empty) the input buffer. (an example of how to handle this is provided in function flush_stdin
below). Simply call flush_stdin
after each call to scanf
where this is a potential problem.
#include <stdio.h>
// #include <conio.h>
void flush_stdin ()
{
int c = 0;
while ((c = getchar()) != '\n' && c != EOF);
}
int main(void)
{
int year = 0; /* Always INITIALIZE your variables */
float principal, amount, inrate, period, value;
principal = amount = inrate = period = value = 0;
printf ("Please enter principal: ");
scanf ("%f", &principal);
amount = principal;
printf ("Please enter interest rate: ");
scanf ("%f", &inrate);
year = 0;
printf ("Please enter period: ");
scanf ("%f", &period);
while(year <= period)
{
printf ("%3d %10.2f\n", year, amount);
value = amount + amount*inrate;
year = year + 1;
amount = value;
}
// getch();
return 0;
}
Output
$ ./bin/scanf_noop
Please enter principal: 123.45
Please enter interest rate: .05
Please enter period: 24
0 123.45
1 129.62
2 136.10
3 142.91
4 150.05
5 157.56
6 165.43
7 173.71
8 182.39
9 191.51
10 201.09
11 211.14
12 221.70
13 232.78
14 244.42
15 256.64
16 269.48
17 282.95
18 297.10
19 311.95
20 327.55
21 343.93
22 361.12
23 379.18
24 398.14
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.