Hi everyone, could you help me loop this function, i want it to repeate when the
ID: 3826072 • Letter: H
Question
Hi everyone, could you help me loop this function, i want it to repeate when the user inputs Y, quit when they enter N, and give an error if they dont enter those numbers. Any help is appreciated. Using the C language.
#define _CRT_SECURE_NO_WARNINGS#include
float a, b;
int n;
char q,t;
main() {
do{
a = ' ';
b = ' ';
n = ' ';
printf("Enter the endpoints of the integral. ");
scanf("%f", &a);
scanf("%f", &b);
printf("Enter the number of trapezoids you want to use. ");
scanf("%d", &n);
badInput();
printf("using %i trapezoids, integral between %.5lf and %.5lf is %.5lf ", n, a, b, integrate(a, b, n));
printf("would you like to calculate another function?");
scanf(" %c", &q);
} while (q == "y" || q == "Y");
return 0;
Explanation / Answer
Hi I have added required code to validate user input.
Please let me know in case of any issue.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
float a, b;
int n;
char q,t;
int main() {
do{
a = ' ';
b = ' ';
n = ' ';
printf("Enter the endpoints of the integral. ");
scanf("%f", &a);
scanf("%f", &b);
printf("Enter the number of trapezoids you want to use. ");
scanf("%d", &n);
//badInput();
printf("using %i trapezoids, integral between %.5lf and %.5lf is %.5lf ", n, a, b, integrate(a, b, n));
printf("would you like to calculate another function?");
scanf(" %c", &q);
if(q != 'n' && q != 'N' && q != 'y' && q != 'Y'){
printf("Error: please enter Y/N ");
while(q != 'n' && q != 'N' && q != 'y' && q != 'Y'){
printf("would you like to calculate another function?");
scanf(" %c", &q);
}
}
} while (q == 'y' || q == 'Y');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.