The code allows you to choose between star and delta resistive network conversions. There is also an exit option. I wanted to validate the values for R_a, R_b, R_c etc. however, I have run into some trouble with my do while loop. The lower limit is 1000 and the upper is 1000000.
I intend to have the program carry on if the input is within range and prompt for another input from the user if it is not. However, as of now, the program continues if the value is within range, but also continues after giving a warning when it is not - when I want it to loop back to the first input prompt.
Once correct, I will add the loop to all inputs.
If anyone is able to fix/find the issue, it would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
printf("\n\n\t\tDelta and Star Converter\n\n\n");
int choice, num, i;
unsigned long int fact;
while(1)
{
printf("1. Star \n");
printf("2. Delta\n");
printf("0. Exit\n\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:;
float R_a=0,R_b=0,R_c=0,R_ab,R_bc,R_ac;
printf("Please enter the value of the Star connected resistors:\n");
do {
printf("R_a = ");
scanf("%f",&R_a);
if (R_a > 1000000) {
printf("Please");
} else if (R_a < 1000) {
printf("Number to low\n");
}else {
}
}while(R_a = -0);
printf("R_b = ");
scanf("%f",&R_b);
printf("R_c = ");
scanf("%f",&R_c);
R_ab=R_a+R_b+(R_a*R_b)/R_c;
R_bc=R_b+R_c+(R_b*R_c)/R_a;
R_ac=R_a+R_c+(R_a*R_c)/R_b;
printf("the equivalent Delta values are: \n");
printf("R_ab = %.2f Ohms\n",R_ab);
printf("R_bc = %.2f Ohms\n",R_bc);
printf("R_ac = %.2f Ohms\n",R_ac);
break;
case 2:;
printf("Please enter the values of the Delta connected resistors:\n");
printf("R_ab = ");
scanf("%f",&R_ab);
printf("R_bc = ");
scanf("%f",&R_bc);
printf("R_ac = ");
scanf("%f",&R_ac);
R_a = (R_ab*R_ac)/(R_ab + R_bc + R_ac);
R_b = (R_ab*R_bc)/(R_ab + R_bc + R_ac);
R_c = (R_ac*R_bc)/(R_ab + R_bc + R_ac);
printf("the equivalent Star values are: \n");
printf("R_a = %.2f Ohms\n",R_a);
printf("R_b = %.2f Ohms\n",R_b);
printf("R_c = %.2f Ohms\n",R_c);
break;
case 0:
printf("\n\nAdios!!\n\n\n");
exit(0); // terminates the complete program execution
}
while (0) ; }
printf("\n\n\t\t\tThank you!\n\n\n");
return 0;
}
while(R_a = -0)
= is the assignment operator. This assigns -0 to R_a, and evaluates to the same falsy value, ending the loop.
Change the do ... while to an infinite loop, and break the loop when the value is in range.
while (1) {
printf("R_a = ");
if (1 != scanf("%f", &R_a))
exit(EXIT_FAILURE);
if (R_a > 1000000) {
fprintf(stderr, "Number too high.\n");
} else if (R_a < 1000) {
fprintf(stderr, "Number to low.\n");
} else {
break;
}
}
Related
I am a beginner and am not sure on how to go about inserting these into my code. The code allows you to choose between star and delta resistive network conversions. There is also an exit option.
I want to add in some validation to the users input, where if the values are not within range, an error code will ask them to re-enter a value that is. The same goes for a non-numeric input.
If someone could show me how I would go about adding just one of these limits to my code so I can try the other two myself, it would be greatly appreciated.
I have tried using a do while loop, but it rejected all inputs instead of the desired outcome.
I read somewhere that I should be using flush but I am not on how to do this.
`
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
printf("\n\n\t\tDelta and Star Converter\n\n\n");
int choice, num, i;
unsigned long int fact;
while(1)
{
printf("1. Star \n");
printf("2. Delta\n");
printf("0. Exit\n\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:;
float R_a=0,R_b=0,R_c=0,R_ab,R_bc,R_ac;
printf("Please enter the value of the Star connected resistors:\n");
printf("R_a = ");
scanf("%f",&R_a);
printf("R_b = ");
scanf("%f",&R_b);
printf("R_c = ");
scanf("%f",&R_c);
R_ab=R_a+R_b+(R_a*R_b)/R_c;
R_bc=R_b+R_c+(R_b*R_c)/R_a;
R_ac=R_a+R_c+(R_a*R_c)/R_b;
printf("the equivalent Delta values are: \n");
printf("R_ab = %.2f Ohms\n",R_ab);
printf("R_bc = %.2f Ohms\n",R_bc);
printf("R_ac = %.2f Ohms\n",R_ac);
break;
case 2:;
printf("Please enter the values of the Delta connected resistors:\n");
printf("R_ab = ");
scanf("%f",&R_ab);
printf("R_bc = ");
scanf("%f",&R_bc);
printf("R_ac = ");
scanf("%f",&R_ac);
R_a = (R_ab*R_ac)/(R_ab + R_bc + R_ac);
R_b = (R_ab*R_bc)/(R_ab + R_bc + R_ac);
R_c = (R_ac*R_bc)/(R_ab + R_bc + R_ac);
printf("the equivalent Star values are: \n");
printf("R_a = %.2f Ohms\n",R_a);
printf("R_b = %.2f Ohms\n",R_b);
printf("R_c = %.2f Ohms\n",R_c);
break;
case 0:
printf("\n\nAdios!!\n\n\n");
exit(0); // terminates the complete program execution
}
}
printf("\n\n\t\t\tThank you!\n\n\n");
return 0;
}
`
Right after you scan the input, you can add a while-loop to ask for a new number, and keep on asking until the user entered a number in the range.
You could make a function to check if a number is in the range of answers that you want to support and check the input like this:
while(notInRange(choice, lowerBound, upperBound)) {
scanf("%d", &choice);
}
Trying to make a GPA calculator.
Here is my code:
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
float fgpa, grade, n;
char reply;
n = 1;
grade = 1;
printf("--- GPA Calculator ---");
while(fgpa < 1 || fgpa > 4)
{
printf("\n\nEnter your current GPA: ");
scanf("%f", &fgpa);
if(fgpa < 1 || fgpa > 4)
printf("Invalid Input! Please re-enter value.");
}
printf("\nUsing the following table to convert grade to points\n\nA+ = 4.0\nA = 4.0\nB+ = 3.5\nB = 3\nC+ = 2.5\nC = 2.0\nD+ = 1.5\nD = 1\nF = 0\n");
while(grade > 0, n++)
{
printf("\nEnter points for module %.0f, enter \"0\" if there are no more additional modules: ", n);
scanf("%f", &grade);
printf("%f", grade);
fgpa = (fgpa + grade) / n;
}
fgpa = fgpa* n / (n-1);
n--;
printf("\n\nNumber of modules taken: %.0f\nFinal GPA: %.1f", n, fgpa);
return 0;
}
I've tried using if(grade = 0) break; but its still not breaking the loop even when the grade is correctly read 0.
picture of 0 being read correctly but loop still continuing
There are multiple problems in the code:
fgpa is uninitialized so the first test in the loop has undefined behavior.
you should also test the return value of scanf() to detect invalid or missing input.
while (grade > 0, n++) is incorrect too: you should instead always read the next grade and test its value and break from the loop before incrementing n.
Your averaging method seems incorrect too: you do not give the same weight to every module.
It seems more appropriate for your purpose to use for ever loops (for (;;)), unconditionally read input, check for scanf() success and test the input values explicitly before proceeding with the computations.
Here is a modified version:
#include <stdio.h>
// flush the rest of the input line, return EOF at end of file
int flush(void) {
int c;
while ((c = getchar()) != EOF && c != \n')
continue;
return c;
}
int main() {
float fgpa;
float grade;
int n = 1;
char reply;
printf("--- GPA Calculator ---");
for (;;) {
printf("\n\nEnter your current GPA: ");
if (scanf("%f", &fgpa) == 1) {
if (fgpa >= 1 && fgpa <= 4)
break;
}
} else {
if (flush() == EOF) {
fprintf(stderr, "unexpected end of file\n");
return 1;
}
}
printf("Invalid Input! Please re-enter value.\n");
}
printf("\nUsing the following table to convert grade to points\n\n"
"A+ = 4.0\nA = 4.0\nB+ = 3.5\nB = 3\nC+ = 2.5\n"
"C = 2.0\nD+ = 1.5\nD = 1\nF = 0\n");
for (;;) {
printf("\nEnter points for module %d, enter \"0\" if there are no more additional modules: ", n);
if (scanf("%f", &grade) == 1) {
if (grade <= 0)
break;
printf("%f\n", grade);
fgpa = fgpa + grade;
n = n + 1;
} else {
if (flush() == EOF)
break;
printf("Invalid Input! Please re-enter value.\n");
}
}
fgpa = fgpa / n;
printf("\n\nNumber of modules taken: %d\nFinal GPA: %.3f\n", n, fgpa);
return 0;
}
I have a converter, but it converts all the units that are in the program.how can i make that the user who opens the project to choose what type of unit to convert?
#include<stdio.h>
#include<conio.h>
int main()
{
float m, f, l, g, cm, inch;
printf("Type meter : ");
scanf("%f",&m);
f = 3.2808399 * m;
printf("feets: %f",f);
printf("\nType gallons : ");
scanf("%f",&g);
l = 3.78541178 * g;
printf("litres: %f",l);
printf("\ninches : ");
scanf("%f", &inch);
cm = 2.54 * inch;
printf("cm: %f", cm);
return 0;
}
This code below is definitely not the best one in terms of complexity, portability, optimisation (Memory/Time) and many other aspects of programming, but it should get you going.
I have added comments to explain the code. Almost all of the code with all those printfs is self-explanatory.
#include<stdio.h>
int main(void)
{
// We need just 3 variables
// This one is for getting the user option
int choice = 0;
// We need these to float variables for user input and an output
float Input = 0.0, Output = 0.0;
// Following code till `while(1)` is optional.
printf("\nThis is a converter with a fixed set of functions.");
printf("\nNote: This converter does support floating point inputs.");
printf("\nNote: Floating point inputs and outputs are truncated to 2 digits after decimal place.");
printf("\nNote: Press any key to acknowledge!");
getchar();
// To get user input multiple times, you'll need to loop
while(1)
{
printf("\n\nFollowing functions are supported, enter a suitable choice form the list below.");
printf("\nPress `1` for Converting Metres to Feet.");
printf("\nPress `2` for Converting Gallons to Litres.");
printf("\nPress `3` for Converting Inches to Centimetres.");
printf("\nPress `0` for Exiting the program.");
printf("\nEnter your Option : ");
scanf("%d", &choice);
// Lets implement a switch-case statement to get the job done
switch(choice)
{
case 1:
printf("Enter input value (in Metres) : ");
scanf("%f",&Input);
Output = 3.2808399 * Input;
printf("%0.2f Metres is equal to %0.2f Feets", Input, Output);
break;
case 2:
printf("Enter input value (in Gallons) : ");
scanf("%f",&Input);
Output = 3.78541178 * Input;
printf("%0.2f Gallons is equal to %0.2f Litres", Input, Output);
break;
case 3:
printf("Enter input value (in Inches) : ");
scanf("%f",&Input);
Output = 2.54 * Input;
printf("%0.2f Inches is equal to %0.2f Centimetres", Input, Output);
break;
case 0:
printf("Thank you. The program will exit now!\n\n");
return 0;
break;
// This default case should take care of the invalid set of choices entered by user
default:
printf("Option you entered is either invalid or is not supported as of now!");
}
}
return 0;
}
Another way to implement this could be using an if-else if-else ladder.
So you can remove swtich-case statements from the code below and replace it with the following:
if(choice == 0)
{
printf("Thank you. The program will exit now!\n\n");
return 0;
}
else if(choice == 1)
{
printf("Enter input value (in Metres) : ");
scanf("%f",&Input);
Output = 3.2808399 * Input;
printf("%0.2f Metres is equal to %0.2f Feets", Input, Output);
}
else if(choice == 2)
{
printf("Enter input value (in Gallons) : ");
scanf("%f",&Input);
Output = 3.78541178 * Input;
printf("%0.2f Gallons is equal to %0.2f Litres", Input, Output);
}
else if(choice == 3)
{
printf("Enter input value (in Inches) : ");
scanf("%f",&Input);
Output = 2.54 * Input;
printf("%0.2f Inches is equal to %0.2f Centimetres", Input, Output);
}
else
{
// This should take care of the invalid set of choices entered by user
printf("Option you entered is either invalid or is not supported as of now!");
}
char unit;
printf("Which unit do you want to convert (input:m,g,i): ");
scanf("%c",&unit);
if(unit == 'm') ...
else if( if(unit == 'g'))....
else if( if(unit == 'i'))....
else printf("wrong input ! \n");
I am a bit new to c programming. For my project I have been tasked with developing a simple calculator that asks the user to input an option then that option declares what operation is to be performed (i.e. if the user enters 1, this corresponds to selecting the addition option which then allows the user to add two numbers they choose. I have the majority of the code worked out, but the catch is that I need the calculator to toggle between int and double variables. When the user enters 5, the calculator should now work with integers then if the user hits 5 again, the calculator switches back to doubles and vice versa so long as you want to switch back and forth. The calculator automatically works with doubles. So, more specifically, if I wanted to use integer variables, I would enter 5, then lets say I wanted to switch back to doubles, I should enter five and receive the message "Calculator now works with doubles." So here is my code thus far:
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
int main()
{
int integermode, doublemode, m, a, b, sum2, difference2, product2,
quotient2;
double i, j, sum1, difference1, product1, quotient1;
printf("This program implements a calculator.");
while(m !=6) {
printf("Options:\n");
printf("1 - addition\n2 - subtraction\n3 - multiplication\n4 - division\n5 - toggle calculator type\n6 - exit program\n");
printf("Please enter your option: ");
scanf("%d", &m);
if(m > 6) {
printf("Invalid option.\n");
printf("Options:\n");
printf("1 - addition\n2 - subtraction\n3 - multiplication\n4 - division\n5 - toggle calculator type\n6 - exit program\n");
printf("Please enter your option: ");
scanf("%d", &m);
}
switch (m) {
case 1:
if (integermode == true) {
printf("Enter first term: ");
scanf("%d", &a);
printf("Enter second term: ");
scanf("%d", &b);
printf("The sum is: %d\n", a+b);
break;
}
if (integermode == false) {
printf("Enter first term: ");
scanf("%lf", &i);
printf("Enter second term: ");
scanf("%lf", &j);
printf("The sum is: %.15lf\n", i+j);
}
break;
case 2:
if (integermode == true) {
printf("Enter first term: ");
scanf("%d", &a);
printf("Enter second term: ");
scanf("%d", &b);
printf("The difference is: %d\n", a-b);
break;
}
if (integermode == false) {
printf("Enter first term: ");
scanf("%lf", &i);
printf("Enter second term: ");
scanf("%lf", &j);
printf("The difference is: %.15lf\n", i-j);
}
break;
case 3:
if (integermode == true) {
printf("Enter first term: ");
scanf("%d", &a);
printf("Enter second term: ");
scanf("%d", &b);
printf("The product is: %d\n", a*b);
break;
}
if (integermode == false) {
printf("Enter first term: ");
scanf("%lf", &i);
printf("Enter second term: ");
scanf("%lf", &j);
printf("The product is: %.15lf\n", i*j);
}
break;
case 4:
if (integermode == true) {
printf("Enter first term: ");
scanf("%d", &a);
printf("Enter second term: ");
scanf("%d", &b);
if (b != 0) printf("The quotient is: %d\n", a/b);
if (b == 0) printf("Cannot divide by zero!\n");
break;
}
if (integermode == false) {
printf("Enter first term: ");
scanf("%lf", &i);
printf("Enter second term: ");
scanf("%lf", &j);
if(j != 0) printf("The quotient is: %.15lf\n", i/j);
if(j == 0) printf("Cannot divide by zero!\n");
break;
}
case 5:
if (m = 5) {
integermode = true;
printf("Calculator now works with integers.\n");
}
if (m != 5) integermode = false;
}
}
return 0;
}
General improvements
First off, you should notice your main function is massive for what it needs to accomplish, and takes a ton of indent levels (when properly indented). That is mostly because you've got a ton of duplicated functionality. You could easily extract the code that asks for the input numbers into a function and reuse it, changing only the operation performed in each case. Or probably even better, you could ask for the input numbers as a part of the main while loop, and then perform a different operation with them depending on the operation mode you're in.
You're not using <math.h>, so you can leave it out.
Another thing I've noticed is you don't initialize the m variable before using it in the while loop. That could potentially cause a lot of problems, so you should initialize it to some value that isn't a valid mode, like for example 0 or -1.
You're also using two consecutive if statements when if/else could be used.
if (integermode == true) {
.....
}
if (integermode == false) {
.....
}
is the same as
if (integermode == true) {
.....
} else {
.....
}
but harder to read and less efficient, as you're doing two comparisons instead of one.
Implementation of double/integer modes
About the functionality you want to implement, you could very well define an enumeration with the possible modes:
typedef enum _datamode {
INTEGER_MODE,
DOUBLE_MODE
} data_mode;
and then have a variable holding the current mode:
data_mode datamode = INTEGER_MODE /* Initializing this to the default mode */
then use integers or doubles during the calculation and output depending on the current value of the variable. Of course, this could be done in a shorter way with an integer instead of an enum and a typedef, but I find this to be a more verbose way.
For the inputs and the result, you could use the union C type, which reserves memory for the biggest type you specify inside it and lets you store any of them in it. For example:
union io {
int i;
double d;
} a, b, result;
declares three variables that have the io union as type. That way, you can use them as integers or doubles as you like (as long as you don't mix types up and, for example, store an integer and read it as a double)
Full working implementation
The following would be my implementation of the program you have to do. It could be accomplished in a much shorter way (take into account the switches dealing with enums take a fair bit of the total space) but I think this is way more elegant and verbose.
I've respected your print statements and the flow of your application, even though I think they could be better (maybe this is something your assignment imposes on you?)
Please take this as an opportunity to learn and don't just copy-paste the code.
#include <stdio.h>
#include <stdlib.h>
typedef enum _datamode {
INTEGER_MODE,
DOUBLE_MODE
} data_mode;
typedef enum _operationmode {
ADDITION,
SUBSTRACTION,
MULTIPLICATION,
DIVISION,
TOGGLE,
EXIT
} operation_mode;
operation_mode ask_mode () {
int input = -1;
while (1) {
printf("Options:\n");
printf("1 - addition\n2 - subtraction\n3 - multiplication\n4 - division\n5 - toggle calculator type\n6 - exit program\n");
printf("Please enter your option: ");
scanf("%i", &input);
if (input < 1 || input > 6) {
printf("Invalid option.\n");
} else {
switch (input) {
case 1:
return ADDITION;
case 2:
return SUBSTRACTION;
case 3:
return MULTIPLICATION;
case 4:
return DIVISION;
case 5:
return TOGGLE;
case 6:
return EXIT;
default:
exit(EXIT_FAILURE); /* Error must've occurred in order for a different value to be present */
}
}
}
}
union _io {
int i;
double d;
};
void get_inputs_integer (int *a, int *b) {
printf("Enter first term: ");
scanf("%i", a);
printf("Enter second term: ");
scanf("%i", b);
}
void get_inputs_double (double *a, double *b) {
printf("Enter first term: ");
scanf("%lf", a);
printf("Enter second term: ");
scanf("%lf", b);
}
int main (int argc, char **argv) {
union _io operand1, operand2, result;
operation_mode o_mode;
data_mode d_mode = INTEGER_MODE;
printf("This program implements a calculator.");
do {
o_mode = ask_mode();
if (o_mode == TOGGLE) {
if (d_mode == INTEGER_MODE) {
d_mode = DOUBLE_MODE;
printf("Calculator now on double mode\n");
} else {
d_mode = INTEGER_MODE;
printf("Calculator now on integer mode\n");
}
} else if (o_mode != EXIT) {
if (d_mode == INTEGER_MODE) {
get_inputs_integer(&operand1.i, &operand2.i);
switch (o_mode) {
case ADDITION:
result.i = operand1.i + operand2.i;
break;
case SUBSTRACTION:
result.i = operand1.i - operand2.i;
break;
case MULTIPLICATION:
result.i = operand1.i * operand2.i;
break;
case DIVISION:
result.i = operand1.i / operand2.i;
break;
default:
exit(EXIT_FAILURE); /* Error must've occurred in order for a different value to be present */
}
printf("The result is %i\n", result.i);
} else {
get_inputs_double(&operand1.d, &operand2.d);
switch (o_mode) {
case ADDITION:
result.d = operand1.d + operand2.d;
break;
case SUBSTRACTION:
result.d = operand1.d - operand2.d;
break;
case MULTIPLICATION:
result.d = operand1.d * operand2.d;
break;
case DIVISION:
result.d = operand1.d / operand2.d;
break;
default:
exit(EXIT_FAILURE); /* Error must've occurred in order for a different value to be present */
}
printf("The result is %lf\n", result.d);
}
}
} while (o_mode != EXIT);
}
In my program I'm messing around with, it simply asks for how many tests one has written and then returns an average. However I've modified it a bit so that it asks if the marks entered are correct.
Problem 1: It doesn't let you input your marks for all your tests
Problem 2: If the marks are wrong it starts over but keep the previous inputs in it's memory? How do I fix the?
Here's the code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
//int variables for grade
unsigned int counter; //number of grades to be entered next
int grade;
int total;
float average;
// user input
int userInput; // amount of tests
int yesNo;
//amount of test passed
unsigned int pass = 0;
unsigned int fail = 0;
int doCount = 1;
//unsigned int test;
//---------------------------------------------------------------------------------------------------//
//standards for program to abide to
total = 0; //Total amount of test to be set to zero, until while statement
counter = 1; //Loop counter to start from one
//---------------------------------------------------------------------------------------------------//
printf ("Please enter amount of test you've written so far: ");
scanf ("%d", &userInput);
//printf ("%d", userInput);
//---------------------------------------------------------------------------------------------------//
do {
//Body of calculations of program
for(counter = 0; counter <= userInput; ++counter) { //for loop that correlates to userInput for amount of passes and test marks
printf ("Please enter percentage mark: "); //prompt for test mark
scanf("%d", &grade);
total = total + grade;
counter = counter + 1;
if (grade >= 40) { //if statement for pass or fail
pass = pass + 1;
} else {
fail = fail + 1;
}
}//end of for loop
printf ("Are the grades entered correct? (1 = yes, 2 = no): "); // user input for yesNo - are inputs correct
scanf ("%d", &yesNo);
if (yesNo == 2) {
} else {
average = ((float)total / userInput); //Getting average for tests so far
//if statement to clarify if you're passing
if (average < 40) {
printf ("\nYou are below sub minimum!\n");
printf ("Your overall average is: %.2f %\n", average);
printf ("Passed: %d\n", pass);
printf ("Failed: %d", fail);
} else if (average >= 75){
printf ("\nYou have a distinction agregate!\n");
printf ("Your overall average is: %.2f %\n", average);
printf ("Passed: %d\n", pass);
printf ("Failed: %d", fail);
} else {
printf ("\nYour overall average is: %.2f %\n", average);
printf ("Passed: %d\n", pass);
printf ("Failed: %d", fail);
}
doCount = 2;
}
} while (doCount == 1);
average = ((float)total / userInput); //Getting average for tests so far
//---------------------------------------------------------------------------------------------------//
getch ();
return 0;
}
In your do while loop, when you come around for your second pass you need to reset your variables. Specifically the total variable should be reset to zero. You do it for the first time outside the do while loop but once it's in the loop for the second pass it doesn't get reset to 0.
As for not reading all test inputs, if it asks for 9 but you need 10 then it likely is a problem with the for loop. I typically use counter++ and not ++counter as it increments the counter after the operation and not before the operation. That may or may not be the reason as I did not run your code, but it is worth looking at.
I've edited your code and commented the changes:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
//int variables for grade
unsigned int counter; //number of grades to be entered next
int grade;
int total;
float average;
// user input
int userInput; // amount of tests
int yesNo;
//amount of test passed
unsigned int pass = 0;
unsigned int fail = 0;
int doCount = 1;
//unsigned int test;
//---------------------------------------------------------------------------------------------------//
//standards for program to abide to
total = 0; //Total amount of test to be set to zero, until while statement
counter = 0; //Loop counter to start from zero, It's always better to start from zero
//---------------------------------------------------------------------------------------------------//
printf("Please enter amount of test you've written so far: ");
scanf("%d", &userInput);
//printf ("%d", userInput);
//---------------------------------------------------------------------------------------------------//
do {
//Body of calculations of program
total = 0; //You need to reset total pass and fail
pass = 0;
fail = 0;
for (counter = 0; counter < userInput; ++counter) { //for loop that correlates to userInput for amount of passes and test marks
printf("Please enter percentage mark: "); //prompt for test mark
scanf("%d", &grade);
total = total + grade;
//counter = counter + 1; You DON't need that
if (grade >= 40) { //if statement for pass or fail
pass = pass + 1;
}
else {
fail = fail + 1;
}
}//end of for loop
printf("Are the grades entered correct? (1 = yes, 2 = no): "); // user input for yesNo - are inputs correct
scanf("%d", &yesNo);
if (yesNo == 2) {
}
else {
average = ((float)total / userInput); //Getting average for tests so far
//if statement to clarify if you're passing
if (average < 40) {
printf("\nYou are below sub minimum!\n");
printf("Your overall average is: %.2f %\n", average);
printf("Passed: %d\n", pass);
printf("Failed: %d", fail);
}
else if (average >= 75) {
printf("\nYou have a distinction agregate!\n");
printf("Your overall average is: %.2f %\n", average);
printf("Passed: %d\n", pass);
printf("Failed: %d", fail);
}
else {
printf("\nYour overall average is: %.2f %\n", average);
printf("Passed: %d\n", pass);
printf("Failed: %d", fail);
}
doCount = 2;
}
} while (doCount == 1);
average = ((float)total / userInput); //Getting average for tests so far
//---------------------------------------------------------------------------------------------------//
getch();
return 0;
}