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.
Related
In case control 1 for loop is not executing. I don't understand why?
In case control 1 printf function is working but for loop is not. And turboc++ didn't get error and warning message after compiling.
Program I try :-
'''
#include<studio.h>
#include<conio.h>
void main()
{
clrscr();
int fun,inp,i;
printf ("Enter a number =
= ");
scanf ("%d",&inp);
printf ("\n");
printf ("Enter 1 for
reverse the number
\n");
printf ("ENTER = ");
scanf ("%d",&fun);
printf ("\n");
switch (fun)
{
case 1 :
printf ("\n Case 1 \n");
for (i=0;i>=inp;i++)
{
printf ("\n %d \n",inp);
inp=inp-1;
}
break;
}
getch();
}
'''
You may change the for loop to :
for (i=0; i<=inp; i++) // Note the comparison operator
{
printf ("\n %d \n",inp);
inp=inp-1;
}
Or a bit shorter with one less variable :
while (inp >= 0)
{
printf ("\n %d \n",inp);
inp--;
}
My code is below. I am using C language. I want to repeat the action from the start if the user types Y but I am confused how can I make that happen.
I tried to look for a solution but the results doesn't fit for my program.
#include <stdio.h>
int main() {
int A, B;
char Y, N, C;
printf ("Enter value 1: ");
scanf ("%i", &B);
printf ("\nEnter value 2: ");
scanf ("%i", &A);
printf ("= %i", A + B);
printf ("\n\nAdd again? Y or N\n");
scanf ("%c", &C);
if (C == Y) {
//This should contain the code that will repeat the:
printf ("Enter value 1: ");
scanf ("%i", &B);
printf ("\nEnter value 2:
} else if (C == N)
printf ("PROGRAM USE ENDED.");
else
printf ("Error.");
}
You should just wrap your code inside a for loop:
#include <stdio.h>
int main() {
int A, B;
char Y = 'Y', N = 'N', C;
for (;;) { // same as while(1)
printf("Enter value 1: ");
if (scanf("%i", &B) != 1)
break;
printf("\nEnter value 2: ");
if (scanf("%i", &A) != 1)
break;
printf("%i + %i = %i\n", A, B, A + B);
printf("\n\nAdd again? Y or N\n");
// note the initial space to skip the pending newline and other whitespace
if (scanf(" %c", &C) != 1 || C != Y)
break;
}
printf("PROGRAM USE ENDED.\n");
return 0;
}
There are a lot of errors in your program.
Syntax error: please resolve it on your own.
There is no need for Y and N to be declared as character, you can use them directly as they are not storing any value.
NOW, there is no need for continue you can use while loop.
I have resolved your problem. Please take a look
Also, you are using a lot of scanf so there is an input buffer, a simple solution to it is using getchar() which consumes the enter key spaces.
#include <stdio.h>
int main()
{
int A, B;
char C = 'Y';
while (C == 'Y')
{
printf("Enter value 1: ");
scanf("%i", &B);
printf("\nEnter value 2");
scanf("%i", &A);
printf("= %i\n", A + B);
getchar();
printf("\n\nAdd again? Y or N\n");
scanf("%c", &C);
}
if (C == 'N')
{
printf("PROGRAM USE ENDED.");
}
else
{
printf("Error.");
}
}
I'm making a program that takes data from various rockets (except their first stage engines) and calculates dV with a new, substitute engine.
Here is the code:
#include <stdio.h>
#include <math.h>
main () {
int choice;
do {
printf ("\n1-Register rocket data");
printf ("\n2-Change already registered data");
printf ("\n3-Delete rocket data");
printf ("\n4-Show registered rockets");
printf ("\n5-Register substitute engine data");
printf ("\n6-Calculate dV with substitute engine");
printf ("\n0-End");
scanf ("%d", &choice);
switch(choice) {
case 1:
dV();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
}
} while(choice != 0);
}
dV () {
int count, x, i;
float rockets[] , dV, wetMass, dryMass, Isp;
printf ("How many rockets will be registered?");
scanf ("%d", &x);
while (i == 1) {
for (count=1; count <= x; count++)
printf ("\n\nWet Mass(kg): ");
scanf ("%f", &wetMass);
fflush (stdin);
printf ("Dry Mass: ");
scanf ("%f", &dryMass);
fflush (stdin);
printf ("Specific Impulse(Seconds): ");
scanf ("%f", &Isp);
fflush (stdin);
printf ("\nType 1 to add a stage or 0 to end the process: ");
scanf ("%d", &i);
dV = (Isp * 9.8 * log(wetMass/dryMass) + dV);
dV = rockets[x++];
}
}
The error:
error: array size missing in 'rockets' (line 50)
Warnings:
warning: implicit declaration of function 'dV' [-Wimplicit-function-declaration} (line 22)
warning: return type defaults to 'int' (Wimplicit-int) (line 47)
First question: How do I solve these errors and warnings?
Second question: How can I make a vector with user-defined number of elements? The user has to be able to register as many rockets as they want. My plan is to calculate the dV for each rocket and store it in an element of the rocket[] vector after the substitute engine data is provided.
Third question: How can I change the value of an already defined element in a vector?
Fourth question: How can I assign vector elements progressively? By that I mean: First registered rocket data should go to rockets[0], the second to rockets[1], etc.
edit: I forgot to add this after line 55
printf ("\ntype 1 to add a stage or 2 to end the process: ");
scanf ("%f", &i);
To remove errors and warnings, do changes as below i.e. create float array of rockets after you fetched value of x and initialize i by 1 before using it in while and add void in return type of dV function if any return value not needed.
void dV () {
int count, x, i;
float dV, wetMass, dryMass, Isp;
printf ("How many rockets will be registered?");
scanf ("%d", &x);
float rockets[x];
i=1;
while (i == 1) {
.
.
}
Answer to your second question : either make rockets as global array or return it to main through dV function.
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.
#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