Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m creating a program that calculates quadratic roots. I\'m trying to read a d

ID: 3528211 • Letter: I

Question

I'm creating a program that calculates quadratic roots. I'm trying to read a double using getchar() then print it to test. I keep getting errors. Here is the section of my code that needs work: /********************************************************* * Bradley Corwin * CSC230, Spring 2012 * HW2, Due January 30, 2012 * * pgm01 - This program calculates the 2 roots of a * quadratic equation with the quadritic formula *********************************************************/ #include #include #define GUESS_THRESHOLD 0.000000001 #define EXIT_VALID 0 #define EXIT_BADINPUT 1 #define EXIT_DISCRIMINATE_NEGATIVE 2 #define EXIT_DISCRIMINATE_ZERO 3 int main(); double myreaddouble(); int main() { double a, b, c; double result; printf("This program solves a quadratic equation using the quadratic formula. "); printf("For the form ax^2 + bx + c = 0 please enter the following: "); printf("a="); a = myreaddouble(); printf("b="); b = myreaddouble(); printf("c="); c = myreaddouble(); // test a, b, and c printf("a b c %lf %lf %lf ", a, b, c); return 0; } double myreaddouble() { int d = 0; // returns the sign int e = 0; // returns if decimal has been read double x = 0; // the actual double int y = 0; // current number 0-9 char a = ' '; // read getchar() into here int i = 0; // iteration a = getchar(); while(a != ' ') { if(a == '-') { if(d == -1) { printf("Invalid input (2 -'s). "); exit(1); } d = -1; } else { d = 1; } if(a == '.') { e = 1; i = -1; //i resets to 0 at the end of this iteration } else { y = a - 0; printf("%d", y); // test y if((y < 0) || (y > 9)) { printf("Invalid input (not a number). "); exit(1); } if(e == 1) { x = x + (y * ((1/10) * i)); } else { x = x + (y * (10 * i)); } } i++; a = getchar(); } return x; }

Explanation / Answer

You make mistake to check value of Y in your function. Here you read using getchar(), this will give you ASCII value of inputed char, also using exit(1) you are exist from entire application not only from function. try below code I modified that also check in my local system #include #include #include #define GUESS_THRESHOLD 0.000000001 #define EXIT_VALID 0 #define EXIT_BADINPUT 1 #define EXIT_DISCRIMINATE_NEGATIVE 2 #define EXIT_DISCRIMINATE_ZERO 3 int main(); double myreaddouble(); int main() { clrscr(); double a, b, c; double result; printf("This program solves a quadratic equation using the quadratic formula. "); printf("For the form ax^2 + bx + c = 0 please enter the following: "); printf("a="); a = myreaddouble(); printf("b="); b = myreaddouble(); printf("c="); c = myreaddouble(); // test a, b, and c printf("a b c %lf %lf %lf ", a, b, c); getch(); return 0; } double myreaddouble() { int d = 0; // returns the sign int e = 0; // returns if decimal has been read double int x = 0; // the actual double int y = 0; // current number 0-9 char a = ' '; // read getchar() into here int i = 0; // iteration a = getchar(); while(a != ' ') { if(a == '-') { if(d == -1) { printf("Invalid input (2 -'s). "); exit(1); } d = -1; } else { d = 1; } if(a == '.') { e = 1; i = -1; //i resets to 0 at the end of this iteration } else { y = a - 0; printf("%d", y); // test y if((y > 56) && (y < 47)) { printf("Invalid input (not a number). "); exit(0); } if(e == 1) { x = x + (y * ((1/10) * i)); } else { x = x + (y * (10 * i)); } } i++; a = getchar(); } return x; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote