Hello Cerdoken,
As I was scrolling back through your post of "Sep 19, 2018 at 3:07pm" i noticed some things I did not see earlier. I will need to look it over better as I was more concerned with you question than the whole program. Look at my following program and I think you will find answers and the differences between the two.
Yoy could follow the "scanf" with the if statement or wait until after all the input to do the if statements. My understanding of the instructions is why I put all the if statement for range checking after all the input. Mostly because of the error message, but it will work either way, so do not think that you have to change the program, but do watch your indenting.
In my IDE I had to setup the code this way for it to work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include<stdio.h>
#include <conio.h> // <--- Used for the "getch()" just before "return".
#include<stdlib.h>
#include <stdbool.h> // <--- For using the "bool" variable type that I did and to define "false" and "true".
//#pragma warning(disable : 4996) // <--- Needed for my compiler.
int main()
{
const double WEIGHT1 = 0.2;
const double WEIGHT2 = 0.4;
float temp = 0.0;
double Assign1 = 90.0, Assign2 = 95.0, Assign3 = 81.0, Assign4 = 96.0, Assign5 = 80.0, Assign6 = 99.0;
double Test1 = 96.0, Test2 = 99.0;
double Exam = 100.0;
double result = 0.0;
double weightedResult = 0.0;
char grade = '\0';
_Bool errorCondition = false;
printf("Score of first assignment = ");
scanf("%f", &temp);
Assign1 = (double)temp;
//printf("Score of second assignment = ");
//scanf("%f", &temp);
//Assign2 = (double)temp;
//printf("Score of third assignment = ");
//scanf("%f", &temp);
//Assign3 = (double)temp;
//printf("Score of fourth assignment = ");
//scanf("%f", &temp);
//Assign4 = (double)temp;
//printf("Score of fifth assignment = ");
//scanf("%f", &temp);
//Assign5 = (double)temp;
//printf("Score of sixth assignment = ");
//scanf("%f", &temp);
//Assign6 = (double)temp;
//printf("Score of first test = ");
//scanf("%f", &temp);
//Test1 = (double)temp;
//printf("Score of second test = ");
//scanf("%f", &Test2);
//Test2 = (double)temp;
//printf("Score of exam = ");
//scanf("%f", &temp);
//Exam = (double)temp;
if ((Assign1 <= 0 || Assign1 >= 100) && !errorCondition)
{
printf("\n A score of %0.0f is out of range.\n", Assign1); // <--- Put your error message here.
errorCondition = true;
}
// <--- Continue for the others.
weightedResult = ((Assign1 + Assign2 + Assign3 + Assign4 + Assign5 + Assign6) * WEIGHT1) + ((Test1 + Test2 + Exam) * WEIGHT2);
result = weightedResult / 100.0;
printf("\n Weighted Result is: %0.2f\n", weightedResult);
printf("\n Score = %0.2f ", result);
printf("\n\n Press any key to continue. ");
getch();
return 0;
}
|
The if statements you have in the program now you can highlight and cut them from the program, but first have another file ready to paste them into for later use because they will work and there is no need to retype them later. Just fix them.
Starting at the top lines 1 - 4. Order of the include files is generally not a problem, but my quirk is that I like to an order to the includes. It helps me to remember what is needed. The last header file , or the file(s) that come after the blank line is what is extra for this given program.
As you can see by the comment for "conio.h" what it is fore. Look at the two lines before the "return" statement. This is better than using "system("pause");" which is a potential security problem. It does not matter what language you use, C or C++, it is bes not to use system.
In C++ "bool" is built into the IDE and you just use it, but for this program I had to include the "stdbool.h" header file and use "_Bool" because that is the way it is defined in the header file.
Line 7 the chances are that you will not have to use this line and that is why it is commented out. I have the feeling that your compiler is much older than mine and that is why there are some thing I can not use unless I use that line in the program.
Now inside m
"main" the first two lines are defined as constants. For what they ar they should not be changed anywhere in the program, just used. The capital letters help to remind you that they are constants.
While I am thinking about it normal variables sar better started with a lower case letter and if a variable name should contain two or more words use camel case as you did with "weightedResult".
Notice that all the variables from "Assign1" down to "Exam" all have different numbers. Once I had tested all the inputs I did this to speed things up and that is why all the inputs except the first one are commented out.
Hint: You could use a for loop to enter the assignments and along with an array You could not only shorten the amount of variables you use, but shorten the code you have written.
Line 24 the bool variable is initialized as false to make the if statements that follow the input work. This way even if all six "assign?" are out of range only one error message will print.
Lines 62 - 66 will need to be duplicated for the other five "Assign?" variables. This is where an array would be handy. If you have learned about arrays.
Line 70 I added some ()s. Not really needed, but it better conveys the intent of the formula.
Line 71 I change "100" to "100.0" . Again not necessary, but it better conveys the intent. Although this should be a constant variable. I am not sure what to call it and that is why I have not changed it yet. The "100.0" is known as a magic number and should be avoided if at all possible and replaced by a constant variable.
Lines 73 and 74 and look and compare the "%f" with line 64. The way I set that up is that line 64 will only print the whole number and no decimal point or numbers to the right of the decimal point. Where lines 73 and 74 will print a decimal point and two numbers to the right.
Lines 77 and 78 are used to replace "system("pause");". They work much the same as "system("pause");" with the "getch()" allowing any key to be pressed to continue.
This will finish parts "A" and "B" which actually combines the second part of "A" and "B" in one if statement.
Hope that helps,
Andy