#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. (e.g. 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
Related
This is for my into to C class, and I can't figure out why I'm getting this error:
while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {
warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int'
Code:
struct vitalInformation {
float temperature;
unsigned int systolicPressure;
unsigned int diastolicPressure;
};
struct activityInformation {
unsigned int stepCount;
unsigned int sleepHours;
};
union patientHealth{
struct vitalInformation vi;
struct activityInformation ai;
} ph[100];
int i = 0;
int menu(){
int option;
printf ("Please enter the number for the desired action (1, 2, 3):\n");
printf ("1 - Enter some patient vital information\n");
printf ("2 - Enter some patient activity information\n");
printf ("3 - Print summary information on the patient information and exit the program\n");
scanf ("%d", &option);
while (scanf("%d", &option) || option<1 || option>3) {
printf ("Please enter 1, 2, or 3\n\n");
printf ("Please enter the number for the desired action (1, 2, 3):\n");
printf ("1 - Enter some patient vital information\n");
printf ("2 - Enter some patient activity information\n)");
printf ("3 - Print summary information on the patient information and exit the program\n");
fflush (stdin);
scanf ("%d", &option);
}
return option;
}
void patientVitalInfo(int *countP, float *minT, float *maxT, int *minS, int *maxS, int *minD, int *maxD) {
printf ("Enter the temperature: ");
scanf ("%f", &ph[i].vi.temperature);
while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {
printf ("Please enter an integral unsigned number\n");
printf ("Enter the temperature: ");
fflush (stdin);
scanf ("%f", &ph[i].vi.temperature);
}
}
The error reported comes from your line
while (scanf ("%d", (int)ph[i].vi.temperature) < 0) {
which is casting whatever you get form ph[i].vi.temperature (in this case, a float) into an int, while scanf requires a pointer to an int.
Now, in your case, you seem to need the temperature as an int, while
ph[i].vi.temperature
holds a float, so you would rather use another int variable, say
int itemp;
scanf ("%d", &itemp);
for the input and then
ph[i].vi.temperature = (float) itemp;
for the casting.
Or, you could simply scanf ("%f", &ph[i].vi.temperature); and then keep the integral part.
I wouldn't know your needs nor the logic behind your code.
Note: I am not sure you are using the return value of scanf in a way matching your needs either.
In your case, scanf can return 0, 1 or EOF.
I'm writing a program that asks the user to input the high and low temperatures over the course of three days. The high temperature for each day has to be greater than the low, the high must not be greater than 41 and the low must not be less than negative -41.
I wrote a while statement following the inputs for the first day however, I get the error comparison between pointer and integer.
I figured it had something to do with me using a set integer so I tried just making a while statement that involved high being greater than low, which resulted in the program working, but I found the while loop was skipped entirely. Here's my code so far:
Edit: I'm beginning to understand where my while loop went wrong. I believe it was because I neglected to assign a value from the array to the high and low and I also neglected to have the code rerun if the user met the conditions for the while loop. Initially, I had wrote it so the high and low held no value and the while condition was trapped in an infinite loop because I did not give it something to execute following the conditions being met.
#include <stdio.h>
#define NUMS 3
int main (void)
{
int high[NUMS];
int low[NUMS];
int max = 40;
int min = -40;
printf ("---===IPC Temperatur Analyzer ===---\n");
printf ("Enter the high value for day 1: ");
scanf ("%d", &high);
printf ("Enter the low value for day 1: ");
scanf ("%d", &low);
while (high[0] > max || low[0] > min || high[0] < low[0]) {
printf("Try again\n");
printf ("Enter the high value for day 1: ");
scanf ("%d", &high[0]);
printf ("Enter the low value for day 1: ");
scanf ("%d", &low[0]);
}
printf ("Enter the high value for day 2: ");
scanf ("%d", &high[1]);
printf ("Enter the low value for day 2: ");
scanf ("%d", &low[1]);
printf ("Enter the high value for day 3: ");
scanf ("%d", &high[2]);
printf ("Enter the low value for day 3: ");
scanf ("%d", &low[2]);
return 0;
}
The high temperature for each day has to be greater than the low, the
high must not be greater than 41 and the low must not be less than
negative -41.
i have modified your code and written comment also to understand :
#include <stdio.h>
#define NUMS 3
int main (void)
{
int high[NUMS];
int low[NUMS];
const int MAX = 41;
const int MIN = -41;
printf ("---===IPC Temperatur Analyzer ===---\n");
printf ("Enter the high value for day 1: ");
scanf ("%d", &high[0]); //address of first element
printf ("Enter the low value for day 1: ");
scanf ("%d", &low[0]); //address of first element
/*Check for User Input Value*/
while (high[0] > MAX || low[0] < MIN || high[0] < low[0]) {
printf("Try again\n");
printf ("Enter the high value for day 1: ");
scanf ("%d", &high[0]);
printf ("Enter the low value for day 1: ");
scanf ("%d", &low[0]);
}
printf ("Enter the high value for day 2: ");
scanf ("%d", &high[1]);
printf ("Enter the low value for day 2: ");
scanf ("%d", &low[1]);
//TODO-:/*Check for User Input Value*/
printf ("Enter the high value for day 3: ");
scanf ("%d", &high[2]);
printf ("Enter the low value for day 3: ");
scanf ("%d", &low[2]);
//TODO-:/*Check for User Input Value*/
//TODO-:/*Print the all value*/
return 0;
}
Todo part you can complete by taking reference from other part of code.
Well that's my code and i get this errors
In function 'main':
41 1 [Error] 'scanf_result' undeclared (first use in this function)
41 1 [Note] each undeclared identifier is reported only once for each function it appears in
#include <stdio.h>
#include <stdlib.h>
int main ( int argc, char *argv[] )
{
int n;
float fl,fw,wh,ww,dh,dw;
float p,t;
float tl,tw,a;
float p1,p2,p3;
printf("What's the width of the floor?");
scanf ("%d", &fw);
printf("What's the length of the floor?");
scanf ("%d", &fl);
printf("What's the height of the wall?");
scanf ("%d", &wh);
printf("What's the width of the wall?");
scanf ("%d", &ww);
printf("What's the width of the door?");
scanf ("%d", &dw);
printf("What's the height of the door?");
scanf ("%d", &dh);
a=fw*fl+(wh*ww)*3-(dh*dw);
p1=a*22;
p2=a*23.80;
p3=a*14;
char line[100];
int answer;
answer = -1;
while (answer != 0)
{
printf ("\nWhat tiles do you want?:\n");
printf (" [1] 20sm X 30sm.");
printf (" [2] 30sm X 41,6sm");
printf (" [3] 25sm X 33sm");
printf ("\nWhat do you want to do? [0 for nothing] ");
fgets (line, sizeof(line), stdin);
scanf_result = scanf (line, "%d", &answer);
if ((scanf_result == 0) | (scanf_result == EOF))
{
printf ("\n *** 1 - 2 or 3! ***\n");
answer = -1;
}
switch (answer)
{
case 0:
break;
case 1:
printf(" Total price = %.2f lv \n",p1);
break;
case 2:
printf(" Total price = %.2f lv \n",p2);
break;
case 3:
printf(" Total price = %.2f lv \n",p3);
break;
default:
break;
}
}
system("PAUSE");
return 0;
}
As the error message clearly describes it, there is no declaration for scanf_result variable in your program.
Put a:
int scanf_result;
in your declaration section to fix the error.
scanf_result = scanf (line, "%d", &answer);
Your scanf() usage is wrong. you need sscanf() instead.
The scanf() function shall read from the standard input stream stdin. The sscanf() function shall read from the string line.
[Error] 'scanf_result' undeclared (first use in this function)
Error describes what you are missing.
declare scanf_result
sscanf() returns integer
declare as integer variable.
This question already has answers here:
Simple C scanf does not work? [duplicate]
(5 answers)
Closed 9 years ago.
I have written a simple program to exchange currency and able to buy a beer.
But there something in program and I don't know why, it auto skips the third input data -> end program.
Here my code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ex_rate_into_vnd = 20000; //! Exchange Rate
int beer = 7000; //! Local price of a beer
float in_c = 0; //! Input amount of money
float out_c = 2; //! Amount of currency to exchange !
float choice; //! Switch mode
char buy; //! Deal or not
//! Introduction
printf ("||---------------------------------------------------||\n");
printf ("|| Currency Exchange Machine beta ||\n");
printf ("||---------------------------------------------------||\n");
printf ("Please choose your option:\n");
printf("\t 1.Exchange VND to dollar\n");
printf("\t 2.Exchange Dollar to VND\n");
do
{
printf("Your choice: ",choice);
scanf("%f",&choice);
} while( choice != 1 && choice != 2);
printf ("Please enter amount of money:");
scanf("%f",&in_c);
if (choice == 1 )
{
out_c = in_c / ex_rate_into_vnd;
printf ("Your amount of money: %.2f",out_c);
}
else
{
out_c = in_c * ex_rate_into_vnd;
printf ("Your amount of money: %.0f",out_c);
}
//! End of Exchanging
printf ("\nWould you like to buy a beer (y/n) ?",buy);
scanf("%c", &buy);
if (buy == 'y')
{
if (out_c >= 7000)
{
out_c = out_c - 7000;
printf("Transactions success !\n");
printf("Your amount: %2.f",out_c);
}
}
printf ("\nWhy Stop ?");
return 0;
}
You have at least one \n between the latest float entry and the char you want to read. You need to get rid of that first.
See also all answers in getchar after scanf category
Change
scanf("%c", &buy);
to
scanf(" %c", &buy);
// ^space
Because the newline character is still in the input buffer after you enter a number and press ENTER in the second scanf.
Instead of scanf("%c", &buy);
1.use space before %c
scanf(" %c",&buy); //space before %c
^
this skips reading of white space (including newlines).
2.or Use getchar(); before scanf("%c", &buy); statement
getchar(); //this hold the newline
scanf("%c", &buy);
3.or use two times getchar();
getchar();
buy=getchar();
//here getchar returns int , it would be better if you declare buy with integer type.
In GCC usage of fflush(stdin); is discouaraged. please avoid using it.
I was wondering why you made 'choice' a float, not an int
Also, consider using a switch-case
that way, you won't have to do the whole do-while loop.
Also, in the line
printf ("\nWould you like to buy a beer (y/n) ?",buy);
Why did u add that?
Here is what I would have done :
printf("Your choice?\n>");
scanf("%d", &choice);
switch(choice)
{
case 1 :
{
out_c = in_c / ex_rate_into_vnd;
printf ("Your amount of money: %.2f",out_c);
}
case 2:
{
out_c = in_c * ex_rate_into_vnd;
printf ("Your amount of money: %.0f",out_c);
}
default :
printf("\nThere has been an error\n"):
reloadprogram(); /* Reloadprogram() is simply to make this go back to the asking thing :) */
}
}
EDIT: also, where it says if( <somevariable> >= 7000), change 7000 to beer, so that way, if u change beer, you won't have to change this :)
Put a fflush(stdin) to clear the input before the last scanf
The program doesn't skip the third input data, it just scans the newline you press after the second input. To fix this, type scanf("%*c%c", &buy); instead of scanf("%c", &buy);. This little %*c scans and ignores the the character read from the input.
you can remove the buy variable from the printf call, it's unneeded
printf ("\nWould you like to buy a beer (y/n) ?",buy);
And replace char buy by char buy[2]; . because a sting is always terminated by /0.
You can also add a memset (buy, 0, sizeof(buy)), to be sure that memory is reset before you start to use it.
i have written the following program however every time i run it, the for loops do not work until i enter another number. The for loops then run, using the second number entered. why is this happening? no one seems to be having this problem... here is the program:
#include <stdio.h>
#include <math.h>
int main(void)
{
float limit;
float count;
float series1, series2;
printf("Enter a limit for the series ");
scanf ("%f", &limit);
while (scanf ("%f", &limit) == 1)
{
for (series1 = 1, count = 2; count <= limit; count++)
series1 += 1.0/count;
printf ("\nThe sum of the first infinite series is %.4f", series1);
for (series2 = 1, count = 2; count <= limit; count++)
series2 += (1.0/count) * pow ((-1),(count - 1));
printf ("\nThe sum of the second infinite series is %.4f", series2);
printf("\n\nEnter a limit for the series (q to quit) ");
scanf ("%f", &limit);
}
return 0;
}
Your problem is right here:
scanf ("%f", &limit);
while (scanf ("%f", &limit) == 1)
The while loop is going to execute that scanf everytime it starts, so just lose the first scanf.
When you run the while loop while (scanf ("%f", &limit) == 1) it is running scanf ("%f", &limit) == 1 again, after you have already ran it. Try setting the first scanf to output a variable and run the variable in the while loop.