Hi Everyone,
I am attempting to write a program on my own, using the If...Else If Else Loop.
I would like the program to read the user input of a score to be inputted in the form 999,999 with a limit being 100,000. The program then outputs the score as an award based on a table within the program. Example: x < 24,999 receives no award, but 25,000 < x < 49,999 receives a bronze award, etc.
So far, I am only able to run the very first IF of the loop. What am I not writing correctly, or what have I left out, in order for the program to run correctly?
Any help or advice you offer is always greatly appreciated.
/*This program reads the User score, and then returns an award for the score according to a table written in the program.
Written by: Ashley Julin
Date: 12/31/2013
*/
#include <stdio.h>
//Function Declarations
void getScore (double* num);
void award (double num);
int main (void)
{
//Local Declarations
double num;
//Statements
getScore (&num);
award (num);
return 0;
}//main
void getScore (double* num)
{
//Local Declaration
//Statement
printf("\n\nHello!\n");
printf("Please enter your score up to 100,000 in the format 999,999: ");
scanf_s("%lf", &num);
return;
}//getScore
void award (double num)
{
//Local Declarations
double medal;
//Statements
medal = num;
if (medal < 49,999)
{
printf("\n\nSorry...Your score is too low for an award.\n");
printf("Keep Playing!\n\n");
}
elseif (medal > 50,000 && medal < 74,999)
{
printf("\n\nCongratulations! Your award is the Bronze Medal!\n\n");
}
elseif (medal > 75,000 && medal < 99,999)
{
printf("\n\nCongratulations! Your award is the Silver Medal!\n\n");
}
else
{
printf("\n\nCongratulations! Your award is the Gold Medal!\n\n");
}
return;
}//award
, The comma symbol is an operator in C++. The statement if (medal < 49,999) would not behave the way you are expecting.
Below is what I changed to fix it.
void getScore (double* num)
{
//Local Declaration
//Statement
printf("\n\nHello!\n");
printf("Please enter your score up to 100,000 in the format 999999: ");
scanf("%lf", num); //got rid of the extra ampersand and the _s
return;
}//getScore
void award (double num)
{
//Local Declarations
double medal;
//Statements
medal = num;
//got rid of commas and added two equal signs
if (medal < 49999)
{
printf("\n\nSorry...Your score is too low for an award.\n");
printf("Keep Playing!\n\n");
}
elseif (medal >= 50000 && medal < 74999)
{
printf("\n\nCongratulations! Your award is the Bronze Medal!\n\n");
}
elseif (medal >= 75000 && medal < 99999)
{
printf("\n\nCongratulations! Your award is the Silver Medal!\n\n");
}
else
{
printf("\n\nCongratulations! Your award is the Gold Medal!\n\n");
}
return;
}//award
Edit: I had assumed that medal was of type int. See Chervil's fix below.
It's best to avoid duplicated checks because it creates extra work whenever there is a need to change the code, it will require changing in multiple places instead of just one. But also, as in this case it introduces the possibility of gaps between ranges.
Output from code posted by kevinkjt2000. Note that a score of 49999.01 is sufficient for a gold medal.
Hello!
Please enter your score up to 100,000 in the format 999999: 49999.01
Congratulations! Your award is the Gold Medal!
Try something like this, where each if statement specifies just one number, the other part of the range is taken care of by the preceding if statements.
void award (double medal)
{
if (medal < 50000)
{
printf("\n\nSorry...Your score is too low for an award.\n");
printf("Keep Playing!\n\n");
}
elseif (medal < 75000)
{
printf("\n\nCongratulations! Your award is the Bronze Medal!\n\n");
}
elseif (medal < 100000)
{
printf("\n\nCongratulations! Your award is the Silver Medal!\n\n");
}
else
{
printf("\n\nCongratulations! Your award is the Gold Medal!\n\n");
}
}
Thank you both for your responses. :)
I have adjusted the program per your points. I am so glad that you pointed out my error in using the comma. I have also taken and simplified the If...Else If...Else loop. I understand now how I need to think about writing mathematical limits in C.
With the changes I find that I am still only running the first If statement. Any number I input is read as less than 50000.
Kevin, you will see that I kept the _s in my scanf statement. Visual Studio 12 on my bootcamped Windows 8 requires that I write my scanf statements with a _s. I did take out the extra ampersand. ;)
I've just started reading about arrays. Should I re-write this using an array?
Thanks again for your help.
/*This program reads the User score, and then returns an award for the score according to a table written in the program.
Written by: Ashley Julin
Date: 12/31/2013
*/
#include <stdio.h>
//Function Declarations
void getScore (double* num);
void award (double num);
int main (void)
{
//Local Declarations
double num;
//Statements
getScore (&num);
award (num);
return 0;
}//main
void getScore (double* num)
{
//Local Declaration
//Statement
printf("\n\nHello!\n");
printf("Please enter your score in the format 999999: ");
scanf_s("%lf", num);
return;
}//getScore
void award (double num)
{
//Local Declarations
double medal;
//Statements
medal = num;
if (medal < 50000)
{
printf("\n\nSorry...Your score is too low for an award.\n");
printf("Keep Playing!\n\n");
}
else if (medal < 75000)
{
printf("\n\nCongratulations! Your award is the Bronze Medal!\n\n");
}
else if (medal < 100000)
{
printf("\n\nCongratulations! Your award is the Silver Medal!\n\n");
}
else
{
printf("\n\nCongratulations! Your award is the Gold Medal!\n\n");
}
return;
}//award
With the changes I find that I am still only running the first If statement. Any number I input is read as less than 50000.
I'm not using that specific compiler, so I'm not familiar with the scanf_s() which is Microsoft-specific. Possibly the specifier "%lf" may not be working. I don't know whether this is the correct reference for your compiler, but there is no mention of type double here, only float. http://msdn.microsoft.com/en-us/library/6ttkkkhh.aspx
Perhaps someone else might know about scanf_s() with double.
I do suggest you add a printf() statement to output the value of num to confirm whether or not it is the value which was input. If it doesn't work, an alternative would be to get the input as a character string (you'd have to define a suitable buffer to store the input) and then convert it to a number using atof().
You have to use a top down approach to solve this. So start with the larger numbers then work you way down to the smaller numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void award (double medal)
{
if (medal >= 99999)
cout << "\n\nCongratulations you won the Gold Medal!\n\n";
elseif (medal >= 75000)
cout << "\n\nCongratulations! Your award is the Silver Medal!\n\n";
elseif (medal >= 50000)
cout << "\n\nCongratulations! Your award is the Bronze Medal!\n\n";
elseif (medal >= 25000)
cout << "\n\nCongratulations! Your award is the Rusty-Plate Medal!\n\n";
else
cout << "\n\nSorry...Your score is too low for an award.\n"
<< "Keep Playing!\n\n";
return;
}//award