I keep getting errors stating undeclared identifier and so on. I have tried to fix it but having troubles. I am new at this so please be kind.
// Jennifer Boswell Program 1 Create a target heart rate calculator.
#include "Program 1.h"
int main ()
{
/*Declare variables */
int y; /* int for year */
int c; /* int for calendar year */
int h; /* int for heart rate */
int i; /* int for individual */
int r; /* int for range */
/* prompt user for input */
printf("Enter year of birth:\n");
scanf("%y", &y);
printf("Enter current calendar year:\n");
scanf("%d", &c);
/* Calculate target heart rate */
i = c - y;
h = 220 - i;
r = 50 to 85% of h;
/* prompt user ouput */
if (y => c);
{
printf("Error, birth year cannot be greater than calendar year\n");
}
else (y <= c);
{
printf("\n\nYour age is\n". &i);
printf("\nYour heart rate is\n", &h);
printf("\nYour target heart rate range is\n", &r);
}
return 0; /* program complete without errors */
}
the errors:
arted: Project: Program1, Configuration: Debug Win32 ------
1>Compiling...
1>Program 1.cpp
1>c:\users\jennifer\documents\visual studio 2008\projects\program1\program1\program 1.cpp(18) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : see declaration of 'scanf'
1>c:\users\jennifer\documents\visual studio 2008\projects\program1\program1\program 1.cpp(21) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : see declaration of 'scanf'
1>c:\users\jennifer\documents\visual studio 2008\projects\program1\program1\program 1.cpp(27) : error C2146: syntax error : missing ';' before identifier 'to'
1>c:\users\jennifer\documents\visual studio 2008\projects\program1\program1\program 1.cpp(27) : error C2065: 'to' : undeclared identifier
1>c:\users\jennifer\documents\visual studio 2008\projects\program1\program1\program 1.cpp(27) : error C2143: syntax error : missing ';' before 'constant'
1>c:\users\jennifer\documents\visual studio 2008\projects\program1\program1\program 1.cpp(27) : error C2065: 'of' : undeclared identifier
1>c:\users\jennifer\documents\visual studio 2008\projects\program1\program1\program 1.cpp(27) : error C2146: syntax error : missing ';' before identifier 'h'
1>c:\users\jennifer\documents\visual studio 2008\projects\program1\program1\program 1.cpp(31) : error C2059: syntax error : '>'
1>c:\users\jennifer\documents\visual studio 2008\projects\program1\program1\program 1.cpp(35) : error C2181: illegal else without matching if
1>c:\users\jennifer\documents\visual studio 2008\projects\program1\program1\program 1.cpp(37) : error C2059: syntax error : '&'
1>c:\users\jennifer\documents\visual studio 2008\projects\program1\program1\program 1.cpp(38) : error C2228: left of '.printf' must have class/struct/union
1> type is 'const char [15]'
1>Build log was saved at "file://c:\Users\Jennifer\Documents\Visual Studio 2008\Projects\Program1\Program1\Debug\BuildLog.htm"
1>Program1 - 9 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Did you mean >= ?
And get rid of the ;
It terminates the statement.
Line 35:
1 2
else (y <= c);
Likewise, get rid of the ;
And you need an if.
You can't put an expression on an else.
Line 37:
printf("\n\nYour age is\n". &i);
You need an %d format specifier.
And you don't need to pass the address of i.
Line 38, 39 - ditto.
PLEASE USE CODE TAGS (the <> formatting button) when posting code. http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
So i fixed it and it gives me no errors but two warnings. Not sure what to do with the r = 50 to 85% of h; so i changed it and it gives me no errors. I added the warnings below as well.
<
// Jennifer Boswell Program 1 Create a target heart rate calculator.
#include "Program 1.h"
int main ()
{
/*Declare variables */
int y; /* int for year */
int c; /* int for calendar year */
int h; /* int for heart rate */
int i; /* int for individual */
int r; /* int for range */
/* prompt user for input */
printf("Enter year of birth:\n");
scanf("%y", &y);
printf("Enter current calendar year:\n");
scanf("%d", &c);
/* Calculate target heart rate */
i = c - y;
h = 220 - i;
r = 50, 85 % h;
/* prompt user ouput */
if (y >= c)
{
printf("Error, birth year cannot be greater than calendar year\n");
} /* end if */
if (y <= c)
{
printf("%d", "Your age is\n");
printf("%d", "Your heart rate is\n");
printf("%d", "Your target heart rate range is\n");
} /* end if */
return 0; /* program complete without errors */
}
>
warnings:
1>------ Build started: Project: Program1, Configuration: Debug Win32 ------
1>Compiling...
1>Program 1.cpp
1>c:\users\jennifer\documents\visual studio 2008\projects\cis 235-80 program 1 boswell j\program1\program1\program 1.cpp(18) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : see declaration of 'scanf'
1>c:\users\jennifer\documents\visual studio 2008\projects\cis 235-80 program 1 boswell j\program1\program1\program 1.cpp(21) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : see declaration of 'scanf'
1>Linking...
1>Embedding manifest...
1>Build log was saved at "file://c:\Users\Jennifer\Documents\Visual Studio 2008\Projects\CIS 235-80 Program 1 Boswell J\Program1\Program1\Debug\BuildLog.htm"
1>Program1 - 0 error(s), 2 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
You're getting those two warnings because Microsoft considers scanf to be unsafe.
You can ignore the warnings.
Line 27 still doesn't make sense. % is the modulo operator. You have to express percentages in C/C++ as decimals. i.e 0.85
You're also trying to express two values using a single variable. i.e. r
1 2 3 4 5
int min_target_heart_rate;
int max_target_heart_rate;
...
min_target_heart_rate = (float)h * 0.50; // need to cast in order to use floating point arithmetic
max_target_heart_rate = (float)h * 0.85;
Your printf statements are still incorrect.
The format argument must be a single argument.
1 2
printf("Your age is %d\n", i);
Your code is hard to read because of the use of single character variable names some of which are incorrectly commented. i.e. i for age.
PLEASE MAKE IT EASIER FOR PEOPLE TO HELP YOU BY USING CODE TAGS AS PREVIOUSLY REQUESTED.