Please help with while loops!

In this program I am trying to find the roots of any quadratic function the user inputs and then if the user wishes to see a table of values they can also do that, however I cannot get past the first validation using a while loop. Every time I try it says that Visual studio has stopped working...

Help Please?
Here is my code...
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  #include <stdio.h>
#include <math.h>


int main(void)
{

	double a, b, c, d, x, x2, y;
	double rl, img;
	char choice;
	int min, max;
	int t;

	puts("A quadratic formula is in the form of ax^2 + bx + c = 0");
	puts("Please enter a: ");
	scanf_s("%f", &a);
	while(a > 10000000 || a < -1000000)
	{
		puts("Range exceeded. Please enter a number between -1,000,000 and 1,000,000 for a.");
		scanf_s("%f", a);
	} 
	puts("Please enter b: ");
	scanf_s("%f", &b);
	while(b > 10000000 || b < -1000000)
	{
		puts("Range exceeded. Please enter a number between -1,000,000 and 1,000,000 for b.");
		scanf_s("%f", b);
	}
	puts("Please enter c: ");
	scanf_s("%f", &c);
	while(c > 10000000 || c < -1000000)
	{
		puts("Range exceeded. Please enter a number between -1,000,000 and 1,000,000 for c.");
		scanf_s("%f", c);
	}


	d=b*b-4*a*c;

	if(d > 0)
	{
		x=(-b+sqrt(d))/(2*a);
		x2=(-b-sqrt(d))/(2*a);
		printf("Roots: %f, %f\n", x, x2);
	}

	else if(d < 0)
	{
		rl= -b/(2*a);
		img=sqrt(-d)/(2*a);
		printf("Roots: %.3f+%.3fi, %.3f+%.3fi\n", rl, img, rl, img);
	}
	else
	{
		x=-b/(2*a);
		printf("Root: %f\n", x);
	}

	puts("Would you like to have a table of values displayed?");
	puts("Enter Y for yes, and N for no.");
	scanf_s("%c", &choice);

	if(choice == 'y' || choice == 'Y')
	{
		puts("Enter the minimum x value for your table.");
		scanf_s("%d", &min);
		while (min >=100 || min <= 100)
			puts("Please enter a value between -100 and 100");
		puts("Enter the maximum x value for your table.");
		scanf_s("%d", &max);
		while (max >=100 || max <= 100)
			puts("Please enter a value between -100 and 100");
		for(t = min;t <= max;t++)
		{
			y=a*t*t+b*t+c;
			printf("x     |y");
			printf("---------------");
			printf("%d     %f", t,y);

		}

	}
	
	
	else if(choice == 'n' || choice == 'N')
	{
		puts("Goodbye");

	}

	else
	{
		while(choice!='y'||choice!='Y'||choice!='n'||choice!='N')
		{
			puts("Error. Please enter a Y or an N: ");
			scanf_s("%c", choice);
		}
	}

	return 0;
	
}
I don't see a purpose of using scanf_s(), you should use simple scanf() instead...
and a, b and c are doubles, so try to scan them with "%lf" format
scanf("%lf",&a);
line 61 is skipped because choice automatically eats newline character, that we leave in the buffer while scanning a, b and c
something like this could be helpful
1
2
3
4
5
puts("Enter Y for yes, and N for no.");
do
{
        scanf("%c", &choice); 
} while(isspace(choice));  // don't forget to include <ctype.h>  or <cctype> library 

http://www.cplusplus.com/reference/cctype/isspace/

now let's look at
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
if(choice == 'y' || choice == 'Y')
{
	puts("Enter the minimum x value for your table.");
	scanf("%d", &min);
	
        while (min >=100 || min <= -100)  // subtraction mark is forgotten
        {
			puts("Please enter a value between -100 and 100");  
        // if min is from the interval, program will stuck in infinite loop
        // so we need to scan min again till it's not proper 
                         scanf("%d",&min);
        }
		
         puts("Enter the maximum x value for your table.");
	 scanf("%d", &max);
		
        while (max >=100 || max <= -100)  // subtraction mark forgotten
        {
			puts("Please enter a value between -100 and 100");
         // same as in the lines above
                         scanf("%d",&max);
         }

        ...
}




1
2
3
4
5
while(choice != 'y' || choice!='Y' || choice!='n' || choice!='N')
{
	puts("Error. Please enter a Y or an N: ");
	scanf("%c", &choice); // ampersand forgotten
}

condition will always be true, since even if you type, lets say, y, part choice != 'Y' is true, so the whole if condition will be true
replace it with while(choice != 'y' && choice!='Y' && choice!='n' && choice!='N') - this means,
that y, Y, n nor N was not typed



Last edited on
Ahh Thank you so much.
I didn't realize I needed to use "%lf" for doubles.
I should be able to finish now.
Thanks again.
Topic archived. No new replies allowed.